1- import base64
1+ import asyncio
22import numpy as np
3+ from dataclasses import dataclass
34from lightllm .server .sampling_params import SamplingParams
45from lightllm .server .multimodal_params import MultimodalParams
56from lightllm .server .httpserver .manager import HttpServerManager
1415_g_health_req_id_gen .generate_id ()
1516
1617
18+ @dataclass
19+ class HealthObj :
20+ _is_health : bool = True
21+ _is_health_checking : bool = False
22+
23+ def begin_check (self ):
24+ self ._is_health_checking = True
25+
26+ def end_check (self ):
27+ self ._is_health_checking = False
28+
29+ def set_unhealth (self ):
30+ self ._is_health = False
31+
32+ def set_health (self ):
33+ self ._is_health = True
34+
35+ def is_health (self ):
36+ return self ._is_health
37+
38+ def is_checking (self ):
39+ return self ._is_health_checking
40+
41+
42+ health_obj = HealthObj ()
43+
44+
1745async def health_check (args , httpserver_manager : HttpServerManager , request : Request ):
46+ if health_obj .is_checking ():
47+ return health_obj .is_health ()
48+ health_obj .begin_check ()
1849 try :
19-
2050 request_dict = {"inputs" : "你好!" , "parameters" : {"do_sample" : True , "temperature" : 0.8 , "max_new_tokens" : 2 }}
2151 if args .run_mode == "prefill" :
2252 request_dict ["parameters" ]["max_new_tokens" ] = 1
23-
2453 prompt = request_dict .pop ("inputs" )
2554 sample_params_dict = request_dict ["parameters" ]
2655 sampling_params = SamplingParams (** sample_params_dict )
@@ -29,11 +58,21 @@ async def health_check(args, httpserver_manager: HttpServerManager, request: Req
2958 sampling_params .group_request_id = - _g_health_req_id_gen .generate_id () # health monitor 的 id 是负的
3059 multimodal_params_dict = request_dict .get ("multimodal_params" , {})
3160 multimodal_params = MultimodalParams (** multimodal_params_dict )
32-
3361 results_generator = httpserver_manager .generate (prompt , sampling_params , multimodal_params , request )
34- async for _ , _ , _ , _ in results_generator :
35- pass
36- return True
62+
63+ async def check_timeout (results_generator ):
64+ async for _ , _ , _ , _ in results_generator :
65+ pass
66+
67+ try :
68+ await asyncio .wait_for (check_timeout (results_generator ), timeout = 88 )
69+ health_obj .set_health ()
70+ except asyncio .TimeoutError :
71+ health_obj .set_unhealth ()
72+ return health_obj .is_health ()
3773 except Exception as e :
3874 logger .exception (str (e ))
39- return False
75+ health_obj .set_unhealth ()
76+ return health_obj .is_health ()
77+ finally :
78+ health_obj .end_check ()
0 commit comments