fix: roll back to unique session per request
This commit is contained in:
parent
1f5a88b888
commit
5a78ca97bd
115
custom_routes.py
115
custom_routes.py
@ -28,27 +28,27 @@ from aiohttp import web, ClientSession, ClientError, ClientTimeout
|
||||
import atexit
|
||||
|
||||
# Global session
|
||||
client_session = None
|
||||
# client_session = None
|
||||
|
||||
# def create_client_session():
|
||||
# global client_session
|
||||
# if client_session is None:
|
||||
# client_session = aiohttp.ClientSession()
|
||||
|
||||
async def ensure_client_session():
|
||||
global client_session
|
||||
if client_session is None:
|
||||
client_session = aiohttp.ClientSession()
|
||||
# async def ensure_client_session():
|
||||
# global client_session
|
||||
# if client_session is None:
|
||||
# client_session = aiohttp.ClientSession()
|
||||
|
||||
async def cleanup():
|
||||
global client_session
|
||||
if client_session:
|
||||
await client_session.close()
|
||||
# async def cleanup():
|
||||
# global client_session
|
||||
# if client_session:
|
||||
# await client_session.close()
|
||||
|
||||
def exit_handler():
|
||||
print("Exiting the application. Initiating cleanup...")
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.run_until_complete(cleanup())
|
||||
# loop = asyncio.get_event_loop()
|
||||
# loop.run_until_complete(cleanup())
|
||||
|
||||
atexit.register(exit_handler)
|
||||
|
||||
@ -60,62 +60,63 @@ print(f"max_retries: {max_retries}, retry_delay_multiplier: {retry_delay_multipl
|
||||
import time
|
||||
|
||||
async def async_request_with_retry(method, url, disable_timeout=False, token=None, **kwargs):
|
||||
global client_session
|
||||
await ensure_client_session()
|
||||
retry_delay = 1 # Start with 1 second delay
|
||||
initial_timeout = 5 # 5 seconds timeout for the initial connection
|
||||
# global client_session
|
||||
# await ensure_client_session()
|
||||
async with aiohttp.ClientSession() as client_session:
|
||||
retry_delay = 1 # Start with 1 second delay
|
||||
initial_timeout = 5 # 5 seconds timeout for the initial connection
|
||||
|
||||
start_time = time.time()
|
||||
for attempt in range(max_retries):
|
||||
try:
|
||||
if not disable_timeout:
|
||||
timeout = ClientTimeout(total=None, connect=initial_timeout)
|
||||
kwargs['timeout'] = timeout
|
||||
start_time = time.time()
|
||||
for attempt in range(max_retries):
|
||||
try:
|
||||
if not disable_timeout:
|
||||
timeout = ClientTimeout(total=None, connect=initial_timeout)
|
||||
kwargs['timeout'] = timeout
|
||||
|
||||
if token is not None:
|
||||
if 'headers' not in kwargs:
|
||||
kwargs['headers'] = {}
|
||||
kwargs['headers']['Authorization'] = f"Bearer {token}"
|
||||
if token is not None:
|
||||
if 'headers' not in kwargs:
|
||||
kwargs['headers'] = {}
|
||||
kwargs['headers']['Authorization'] = f"Bearer {token}"
|
||||
|
||||
request_start = time.time()
|
||||
async with client_session.request(method, url, **kwargs) as response:
|
||||
request_end = time.time()
|
||||
logger.info(f"Request attempt {attempt + 1} took {request_end - request_start:.2f} seconds")
|
||||
request_start = time.time()
|
||||
async with client_session.request(method, url, **kwargs) as response:
|
||||
request_end = time.time()
|
||||
logger.info(f"Request attempt {attempt + 1} took {request_end - request_start:.2f} seconds")
|
||||
|
||||
if response.status != 200:
|
||||
error_body = await response.text()
|
||||
logger.error(f"Request failed with status {response.status} and body {error_body}")
|
||||
# raise Exception(f"Request failed with status {response.status}")
|
||||
if response.status != 200:
|
||||
error_body = await response.text()
|
||||
logger.error(f"Request failed with status {response.status} and body {error_body}")
|
||||
# raise Exception(f"Request failed with status {response.status}")
|
||||
|
||||
response.raise_for_status()
|
||||
if method.upper() == 'GET':
|
||||
await response.read()
|
||||
response.raise_for_status()
|
||||
if method.upper() == 'GET':
|
||||
await response.read()
|
||||
|
||||
total_time = time.time() - start_time
|
||||
logger.info(f"Request succeeded after {total_time:.2f} seconds (attempt {attempt + 1}/{max_retries})")
|
||||
return response
|
||||
except asyncio.TimeoutError:
|
||||
logger.warning(f"Request timed out after {initial_timeout} seconds (attempt {attempt + 1}/{max_retries})")
|
||||
except ClientError as e:
|
||||
end_time = time.time()
|
||||
logger.error(f"Request failed (attempt {attempt + 1}/{max_retries}): {e}")
|
||||
logger.error(f"Time taken for failed attempt: {end_time - request_start:.2f} seconds")
|
||||
logger.error(f"Total time elapsed: {end_time - start_time:.2f} seconds")
|
||||
total_time = time.time() - start_time
|
||||
logger.info(f"Request succeeded after {total_time:.2f} seconds (attempt {attempt + 1}/{max_retries})")
|
||||
return response
|
||||
except asyncio.TimeoutError:
|
||||
logger.warning(f"Request timed out after {initial_timeout} seconds (attempt {attempt + 1}/{max_retries})")
|
||||
except ClientError as e:
|
||||
end_time = time.time()
|
||||
logger.error(f"Request failed (attempt {attempt + 1}/{max_retries}): {e}")
|
||||
logger.error(f"Time taken for failed attempt: {end_time - request_start:.2f} seconds")
|
||||
logger.error(f"Total time elapsed: {end_time - start_time:.2f} seconds")
|
||||
|
||||
# Log the response body for ClientError as well
|
||||
if hasattr(e, 'response') and e.response is not None:
|
||||
error_body = await e.response.text()
|
||||
logger.error(f"Error response body: {error_body}")
|
||||
# Log the response body for ClientError as well
|
||||
if hasattr(e, 'response') and e.response is not None:
|
||||
error_body = await e.response.text()
|
||||
logger.error(f"Error response body: {error_body}")
|
||||
|
||||
if attempt == max_retries - 1:
|
||||
logger.error(f"Request failed after {max_retries} attempts: {e}")
|
||||
raise
|
||||
if attempt == max_retries - 1:
|
||||
logger.error(f"Request failed after {max_retries} attempts: {e}")
|
||||
raise
|
||||
|
||||
await asyncio.sleep(retry_delay)
|
||||
retry_delay *= retry_delay_multiplier
|
||||
await asyncio.sleep(retry_delay)
|
||||
retry_delay *= retry_delay_multiplier
|
||||
|
||||
total_time = time.time() - start_time
|
||||
raise Exception(f"Request failed after {max_retries} attempts and {total_time:.2f} seconds")
|
||||
total_time = time.time() - start_time
|
||||
raise Exception(f"Request failed after {max_retries} attempts and {total_time:.2f} seconds")
|
||||
|
||||
from logging import basicConfig, getLogger
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user