@@ -56,7 +56,8 @@ async def requester(api_url: str, timeout: int = 30):
56
56
class ServerMonitor :
57
57
58
58
def __init__ (self ):
59
- self .response_time_thresh_hold : int = 900 # 900 milliseconds
59
+ # 900 milliseconds
60
+ self .response_time_thresh_hold : int = 900
60
61
self .healthy_server_urls : list [str ] = config_instance ().API_SERVERS .SERVERS_LIST .split ("," )
61
62
# Define the health check endpoint for each server
62
63
self ._server_monitor_endpoint = '/_ah/warmup'
@@ -67,8 +68,7 @@ async def check_health(self, api_url: str) -> tuple[str, bool]:
67
68
health_check_url = f"{ api_url } { self ._server_monitor_endpoint } "
68
69
request_logger .info (f"Server Health Probe: { health_check_url } " )
69
70
try :
70
- async with httpx .AsyncClient () as client :
71
- response = await async_client .get (url = health_check_url )
71
+ response = await async_client .get (url = health_check_url )
72
72
if response .status_code == 200 :
73
73
request_logger .info (f"server still healthy : { api_url } " )
74
74
return api_url , True
@@ -79,29 +79,28 @@ async def check_health(self, api_url: str) -> tuple[str, bool]:
79
79
80
80
except (ConnectionError , TimeoutError ):
81
81
return api_url , False
82
- except httpx .HTTPError as http_err :
82
+ except httpx .HTTPError :
83
83
return api_url , False
84
84
85
85
# Sort the healthy servers by their response time
86
86
async def measure_response_time (self , api_url : str ) -> tuple [str , float | None ]:
87
87
try :
88
88
check_url : str = f"{ api_url } { self ._server_monitor_endpoint } "
89
89
request_logger .info (f"Server Health Probe: { check_url } " )
90
- async with httpx .AsyncClient () as client :
91
- start_time = time .perf_counter ()
92
- response = await async_client .get (url = check_url )
93
- if response .status_code == 200 :
94
- elapsed_time = int ((time .perf_counter () - start_time ) * 1000 )
95
- request_logger .info (f"server : { api_url } latency : { elapsed_time } " )
96
- return api_url , elapsed_time
97
- else :
98
- request_logger .info (f"server : { api_url } Not healthy" )
99
- request_logger .info (f"Response : { response .text } " )
100
- return api_url , None
90
+ start_time = time .perf_counter ()
91
+ response = await async_client .get (url = check_url )
92
+ if response .status_code == 200 :
93
+ elapsed_time = int ((time .perf_counter () - start_time ) * 1000 )
94
+ request_logger .info (f"server : { api_url } latency : { elapsed_time } " )
95
+ return api_url , elapsed_time
96
+ else :
97
+ request_logger .info (f"server : { api_url } Not healthy" )
98
+ request_logger .info (f"Response : { response .text } " )
99
+ return api_url , None
101
100
102
101
except (ConnectionError , TimeoutError ):
103
102
return api_url , None
104
- except httpx .HTTPError as http_err :
103
+ except httpx .HTTPError :
105
104
return api_url , None
106
105
107
106
async def sort_api_servers_by_health (self ) -> None :
@@ -117,4 +116,3 @@ async def sort_api_servers_by_health(self) -> None:
117
116
sorted_response_times = sorted (response_time_results , key = lambda x : x [1 ])
118
117
within_threshold = [api_url for api_url , response_time in sorted_response_times if response_time < self .response_time_thresh_hold ]
119
118
self .healthy_server_urls = within_threshold if within_threshold else [config_instance ().API_SERVERS .SLAVE_API_SERVER ]
120
-
0 commit comments