adding retry
+import os
+import time
import logging
import platform
import stat
@@ -71,6 +73,11 @@ class TwistcliIntegration(ABC):
{"Authorization": bc_api_key},
)
+ # adding retry mechanism for avoiding repeated unexpected Gateway Timeout from the server
+ request_max_tries = int(os.getenv('REQUEST_MAX_TRIES', 3))
+ sleep_between_request_tries = float(os.getenv('SLEEP_BETWEEN_REQUEST_TRIES', 1))
+
+ for i in range(request_max_tries):
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(resolver=aiohttp.AsyncResolver())) as session:
async with session.post(
url=f'{bc_integration.api_url}{self.vulnerabilities_base_path}/results', headers=headers, json=payload
@@ -80,6 +87,9 @@ class TwistcliIntegration(ABC):
if response.ok:
logging.info(f"Successfully send report for package file {file_path}")
return 0
+ elif i != request_max_tries - 1:
+ time.sleep(sleep_between_request_tries * (i + 1))
+ continue
else:
logging.error(f"Failed to send report for package file {file_path}")
logging.error(f"Status code: {response.status}, Reason: {response.reason}, Content: {content}")
