Added timeout to requests calls

This commit is contained in:
Justin Warren 2023-01-10 13:06:53 +11:00
parent dfaf578356
commit 23b8833a67
No known key found for this signature in database
1 changed files with 10 additions and 3 deletions

View File

@ -45,6 +45,9 @@ REJECT_REPORTS_DEFAULT = {
'suspend': True,
}
# Wait at most this long for a remote server to respond
REQUEST_TIMEOUT=30
def sync_blocklists(conf: dict):
"""Sync instance blocklists from remote sources.
@ -239,7 +242,7 @@ def fetch_instance_blocklist(host: str, token: str=None, admin: bool=False,
link = True
while link:
response = requests.get(url, headers=headers)
response = requests.get(url, headers=headers, timeout=REQUEST_TIMEOUT)
if response.status_code != 200:
log.error(f"Cannot fetch remote blocklist: {response.content}")
raise ValueError("Unable to fetch domain block list: %s", response)
@ -282,6 +285,7 @@ def delete_block(token: str, host: str, id: int):
response = requests.delete(url,
headers=requests_headers(token),
timeout=REQUEST_TIMEOUT
)
if response.status_code != 200:
if response.status_code == 404:
@ -316,6 +320,7 @@ def fetch_instance_follows(token: str, host: str, domain: str) -> int:
response = requests.post(url,
headers=requests_headers(token),
json=data,
timeout=REQUEST_TIMEOUT
)
if response.status_code != 200:
if response.status_code == 403:
@ -380,7 +385,8 @@ def update_known_block(token: str, host: str, blockdict: dict):
response = requests.put(url,
headers=requests_headers(token),
data=blockdata
data=blockdata,
timeout=REQUEST_TIMEOUT
)
if response.status_code != 200:
raise ValueError(f"Something went wrong: {response.status_code}: {response.content}")
@ -395,7 +401,8 @@ def add_block(token: str, host: str, blockdata: dict):
response = requests.post(url,
headers=requests_headers(token),
data=blockdata
data=blockdata,
timeout=REQUEST_TIMEOUT
)
if response.status_code == 422:
# A stricter block already exists. Probably for the base domain.