From be7cd49f4f07b74b806f460e05ac581fdd71bcdd Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Wed, 16 Apr 2025 08:12:49 -0700 Subject: [PATCH] fix - use constants for caching azure storage client --- litellm/constants.py | 3 +++ .../integrations/azure_storage/azure_storage.py | 16 ++++++++-------- litellm/llms/custom_httpx/http_handler.py | 2 +- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/litellm/constants.py b/litellm/constants.py index 0f35520b2e..f25c7c71e4 100644 --- a/litellm/constants.py +++ b/litellm/constants.py @@ -21,6 +21,9 @@ DEFAULT_MAX_TOKENS = 256 # used when providers need a default MAX_SIZE_PER_ITEM_IN_MEMORY_CACHE_IN_KB = 1024 # 1MB = 1024KB SINGLE_DEPLOYMENT_TRAFFIC_FAILURE_THRESHOLD = 1000 # Minimum number of requests to consider "reasonable traffic". Used for single-deployment cooldown logic. +########## Networking constants ############################################################## +_DEFAULT_TTL_FOR_HTTPX_CLIENTS = 3600 # 1 hour, re-use the same httpx client for 1 hour + ########### v2 Architecture constants for managing writing updates to the database ########### REDIS_UPDATE_BUFFER_KEY = "litellm_spend_update_buffer" REDIS_DAILY_SPEND_UPDATE_BUFFER_KEY = "litellm_daily_spend_update_buffer" diff --git a/litellm/integrations/azure_storage/azure_storage.py b/litellm/integrations/azure_storage/azure_storage.py index 52666c1470..6ffb1e542f 100644 --- a/litellm/integrations/azure_storage/azure_storage.py +++ b/litellm/integrations/azure_storage/azure_storage.py @@ -7,7 +7,7 @@ from datetime import datetime, timedelta from typing import List, Optional from litellm._logging import verbose_logger -from litellm.constants import AZURE_STORAGE_MSFT_VERSION +from litellm.constants import _DEFAULT_TTL_FOR_HTTPX_CLIENTS, AZURE_STORAGE_MSFT_VERSION from litellm.integrations.custom_batch_logger import CustomBatchLogger from litellm.llms.azure.common_utils import get_azure_ad_token_from_entra_id from litellm.llms.custom_httpx.http_handler import ( @@ -54,12 +54,12 @@ class AzureBlobStorageLogger(CustomBatchLogger): self._service_client_timeout: Optional[float] = None # Internal variables used for Token based authentication - self.azure_auth_token: Optional[ - str - ] = None # the Azure AD token to use for Azure Storage API requests - self.token_expiry: Optional[ - datetime - ] = None # the expiry time of the currentAzure AD token + self.azure_auth_token: Optional[str] = ( + None # the Azure AD token to use for Azure Storage API requests + ) + self.token_expiry: Optional[datetime] = ( + None # the expiry time of the currentAzure AD token + ) asyncio.create_task(self.periodic_flush()) self.flush_lock = asyncio.Lock() @@ -344,7 +344,7 @@ class AzureBlobStorageLogger(CustomBatchLogger): account_url=f"https://{self.azure_storage_account_name}.dfs.core.windows.net", credential=self.azure_storage_account_key, ) - self._service_client_timeout = time.time() + 3600 + self._service_client_timeout = time.time() + _DEFAULT_TTL_FOR_HTTPX_CLIENTS return self._service_client async def upload_to_azure_data_lake_with_azure_account_key( diff --git a/litellm/llms/custom_httpx/http_handler.py b/litellm/llms/custom_httpx/http_handler.py index f1aa5627dc..627dd8c9f9 100644 --- a/litellm/llms/custom_httpx/http_handler.py +++ b/litellm/llms/custom_httpx/http_handler.py @@ -8,6 +8,7 @@ import httpx from httpx import USE_CLIENT_DEFAULT, AsyncHTTPTransport, HTTPTransport import litellm +from litellm.constants import _DEFAULT_TTL_FOR_HTTPX_CLIENTS from litellm.litellm_core_utils.logging_utils import track_llm_api_timing from litellm.types.llms.custom_http import * @@ -31,7 +32,6 @@ headers = { # https://www.python-httpx.org/advanced/timeouts _DEFAULT_TIMEOUT = httpx.Timeout(timeout=5.0, connect=5.0) -_DEFAULT_TTL_FOR_HTTPX_CLIENTS = 3600 # 1 hour, re-use the same httpx client for 1 hour def mask_sensitive_info(error_message):