Skip to content

Commit

Permalink
improved slow file transfer handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Loki-Afro committed Sep 2, 2021
1 parent dc6ba9d commit ca98bfd
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions antivirus_service/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ def scan(self, download_uri, access_token):
if access_token:
headers['Authorization'] = 'Bearer %s' % access_token

sleep_time = 2
# we try to download the file multiple times, just for the case,
# that the scan request was triggered before the file upload
# has been finished
last_exception_message = ''
count = 5
count = 3
for i in range(1, count):
try:
r = requests.get(download_uri, stream=True, headers=headers)
# defining stream and timeout = just specifying the initial socket connect timeout, that's what we want
r = requests.get(download_uri, stream=True, headers=headers, timeout=10)
if r.status_code != 200:
raise Exception('Bad status code: {0}'.format(r.status_code))
else:
Expand All @@ -59,10 +61,10 @@ def scan(self, download_uri, access_token):
last_exception_message = str(e)
logging.info('file could not downloaded: for {0} time'.format(i))
if i < count:
time.sleep(2**i)
time.sleep(sleep_time)
else:
raise Exception('File could not downloaded: {0} - after {1} seconds'.format(last_exception_message, 2**(i+1) - 1))

raise Exception('File could not downloaded: {0} - after {1} seconds'.format(last_exception_message,
sleep_time * i))
return self.clamd.scan_file(r)

def callback(self, callback_uri, access_token, scan_result, signature):
Expand Down Expand Up @@ -128,7 +130,7 @@ def scan_url(self, url):
logging.info('Start scan')

api_key = self.config['virustotal']['api_key']

params = {'apikey': api_key, 'url': url}
response = requests.post('https://www.virustotal.com/vtapi/v2/url/scan', data=params)
scan_response = response.json()
Expand Down

0 comments on commit ca98bfd

Please sign in to comment.