1
- import base64
1
+ import asyncio
2
2
import numpy as np
3
+ from dataclasses import dataclass
3
4
from lightllm .server .sampling_params import SamplingParams
4
5
from lightllm .server .multimodal_params import MultimodalParams
5
6
from lightllm .server .httpserver .manager import HttpServerManager
14
15
_g_health_req_id_gen .generate_id ()
15
16
16
17
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
+
17
45
async 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 ()
18
49
try :
19
-
20
50
request_dict = {"inputs" : "你好!" , "parameters" : {"do_sample" : True , "temperature" : 0.8 , "max_new_tokens" : 2 }}
21
51
if args .run_mode == "prefill" :
22
52
request_dict ["parameters" ]["max_new_tokens" ] = 1
23
-
24
53
prompt = request_dict .pop ("inputs" )
25
54
sample_params_dict = request_dict ["parameters" ]
26
55
sampling_params = SamplingParams (** sample_params_dict )
@@ -29,11 +58,21 @@ async def health_check(args, httpserver_manager: HttpServerManager, request: Req
29
58
sampling_params .group_request_id = - _g_health_req_id_gen .generate_id () # health monitor 的 id 是负的
30
59
multimodal_params_dict = request_dict .get ("multimodal_params" , {})
31
60
multimodal_params = MultimodalParams (** multimodal_params_dict )
32
-
33
61
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 ()
37
73
except Exception as e :
38
74
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