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