-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_manager.py
More file actions
444 lines (351 loc) · 14 KB
/
request_manager.py
File metadata and controls
444 lines (351 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
"""
request_manager.py - 请求生命周期管理器
职责:
- 请求队列管理(FIFO)
- 并发控制(全局锁)
- 请求状态追踪
- 取消信号管理
- 客户端断开检测
"""
import asyncio
import threading
import time
import uuid
import logging
from enum import Enum
from dataclasses import dataclass, field
from typing import Optional, Dict, Callable, Any
from collections import OrderedDict
logger = logging.getLogger('request_manager')
# ================= 请求状态 =================
class RequestStatus(Enum):
"""请求状态枚举"""
QUEUED = "queued" # 在队列中等待
RUNNING = "running" # 正在执行
COMPLETED = "completed" # 正常完成
CANCELLED = "cancelled" # 被取消
FAILED = "failed" # 执行失败
# ================= 请求上下文 =================
@dataclass
class RequestContext:
"""
请求上下文 - 贯穿请求整个生命周期
核心职责:
1. 唯一标识请求
2. 追踪请求状态
3. 传递取消信号
"""
request_id: str
status: RequestStatus = RequestStatus.QUEUED
created_at: float = field(default_factory=time.time)
started_at: Optional[float] = None
finished_at: Optional[float] = None
# 取消控制
_cancel_flag: bool = field(default=False, repr=False)
cancel_reason: Optional[str] = None
# 线程安全
_lock: threading.Lock = field(default_factory=threading.Lock, repr=False)
def should_stop(self) -> bool:
"""
检查是否应该停止(线程安全)
供 browser_core 在循环中调用
"""
with self._lock:
return self._cancel_flag
def request_cancel(self, reason: str = "unknown"):
"""
请求取消(设置标志,不立即生效)
实际停止由 browser_core 在检查点执行
"""
with self._lock:
if self._cancel_flag:
return # 已经取消过
self._cancel_flag = True
self.cancel_reason = reason
if self.status == RequestStatus.RUNNING:
self.status = RequestStatus.CANCELLED
logger.info(f"请求 [{self.request_id}] 收到取消信号 (原因: {reason})")
def mark_running(self):
"""标记为运行中"""
with self._lock:
self.status = RequestStatus.RUNNING
self.started_at = time.time()
def mark_completed(self):
"""标记为完成"""
with self._lock:
if self.status == RequestStatus.RUNNING:
self.status = RequestStatus.COMPLETED
self.finished_at = time.time()
def mark_failed(self, reason: str = None):
"""标记为失败"""
with self._lock:
self.status = RequestStatus.FAILED
self.finished_at = time.time()
if reason:
self.cancel_reason = reason
def get_duration(self) -> float:
"""获取执行时长"""
end = self.finished_at or time.time()
start = self.started_at or self.created_at
return end - start
def is_terminal(self) -> bool:
"""是否已结束(不可再变化)"""
return self.status in (
RequestStatus.COMPLETED,
RequestStatus.CANCELLED,
RequestStatus.FAILED
)
# ================= 请求管理器 =================
class RequestManager:
"""
请求管理器 - 单例模式
核心功能:
1. FIFO 队列:先来先服务
2. 全局锁:同时只执行一个请求
3. 状态追踪:每个请求有完整生命周期
4. 取消传递:可靠地取消正在执行的请求
使用方式:
ctx = request_manager.create_request()
try:
acquired = await request_manager.acquire(ctx)
if acquired:
# 执行工作
pass
finally:
request_manager.release(ctx)
"""
_instance: Optional['RequestManager'] = None
_instance_lock = threading.Lock()
def __new__(cls):
if cls._instance is None:
with cls._instance_lock:
if cls._instance is None:
instance = super().__new__(cls)
instance._initialized = False
cls._instance = instance
return cls._instance
def __init__(self):
if self._initialized:
return
# 执行锁(threading.Lock,通过 asyncio.to_thread 包装)
self._exec_lock = threading.Lock()
# 请求追踪(有序字典,FIFO)
self._requests: OrderedDict[str, RequestContext] = OrderedDict()
self._requests_lock = threading.Lock()
# 当前执行的请求
self._current_request_id: Optional[str] = None
# 配置
self._max_queue_size = 20 # 最大排队数
self._max_history = 100 # 最大历史记录
self._lock_timeout = 300.0 # 锁超时(秒)
self._initialized = True
logger.info("RequestManager 初始化完成")
# ================= 请求创建 =================
def create_request(self) -> RequestContext:
"""
创建新请求
Returns:
RequestContext 实例
"""
request_id = self._generate_id()
ctx = RequestContext(request_id=request_id)
with self._requests_lock:
self._requests[request_id] = ctx
self._cleanup_old_requests()
logger.info(f"请求 [{request_id}] 已创建")
return ctx
def _generate_id(self) -> str:
"""生成唯一请求 ID"""
timestamp = int(time.time() * 1000) % 100000
unique = uuid.uuid4().hex[:4]
return f"{timestamp:05d}-{unique}"
def _cleanup_old_requests(self):
"""清理旧请求记录"""
while len(self._requests) > self._max_history:
oldest_id, oldest_ctx = next(iter(self._requests.items()))
if oldest_ctx.is_terminal():
del self._requests[oldest_id]
else:
break # 不删除未完成的请求
# ================= 锁管理 =================
async def acquire(self, ctx: RequestContext,
timeout: float = None) -> bool:
"""
异步获取执行锁
Args:
ctx: 请求上下文
timeout: 超时时间(秒),None 使用默认值
Returns:
是否成功获取锁
"""
timeout = timeout or self._lock_timeout
# 检查队列大小
queue_size = self._get_queue_size()
if queue_size >= self._max_queue_size:
logger.warning(f"请求 [{ctx.request_id}] 被拒绝:队列已满 ({queue_size})")
ctx.mark_failed("queue_full")
return False
# 打印等待日志
if self._current_request_id:
logger.info(f"请求 [{ctx.request_id}] 等待中,"
f"当前执行: [{self._current_request_id}]")
# 在线程池中等待锁
try:
acquired = await asyncio.wait_for(
asyncio.to_thread(self._sync_acquire, timeout),
timeout=timeout + 1 # 额外 1 秒缓冲
)
if acquired:
self._current_request_id = ctx.request_id
ctx.mark_running()
logger.info(f"请求 [{ctx.request_id}] 开始执行")
return True
else:
logger.warning(f"请求 [{ctx.request_id}] 获取锁超时")
ctx.mark_failed("lock_timeout")
return False
except asyncio.TimeoutError:
logger.warning(f"请求 [{ctx.request_id}] 等待超时")
ctx.mark_failed("acquire_timeout")
return False
except asyncio.CancelledError:
logger.info(f"请求 [{ctx.request_id}] 在等待时被取消")
ctx.request_cancel("cancelled_while_waiting")
raise
def _sync_acquire(self, timeout: float) -> bool:
"""同步获取锁(在线程池中执行)"""
return self._exec_lock.acquire(timeout=timeout)
def release(self, ctx: RequestContext, success: bool = True):
"""
释放执行锁
Args:
ctx: 请求上下文
success: 是否成功完成
"""
try:
# 释放锁
if self._exec_lock.locked():
self._exec_lock.release()
# 更新状态
if self._current_request_id == ctx.request_id:
self._current_request_id = None
# 设置最终状态(如果还未设置)
if ctx.status == RequestStatus.RUNNING:
if success:
ctx.mark_completed()
else:
ctx.mark_failed()
# 日志
duration = ctx.get_duration()
logger.info(f"请求 [{ctx.request_id}] 结束 "
f"(状态: {ctx.status.value}, 耗时: {duration:.2f}s)")
except RuntimeError as e:
logger.warning(f"释放锁异常: {e}")
# ================= 取消控制 =================
def cancel_request(self, request_id: str,
reason: str = "manual") -> bool:
"""
取消指定请求
Args:
request_id: 请求 ID
reason: 取消原因
Returns:
是否成功发送取消信号
"""
with self._requests_lock:
ctx = self._requests.get(request_id)
if not ctx:
logger.debug(f"请求 [{request_id}] 不存在")
return False
if ctx.is_terminal():
logger.debug(f"请求 [{request_id}] 已结束")
return False
ctx.request_cancel(reason)
return True
def cancel_current(self, reason: str = "cancel_current") -> bool:
"""取消当前正在执行的请求"""
current_id = self._current_request_id
if current_id:
return self.cancel_request(current_id, reason)
return False
# ================= 状态查询 =================
def get_request(self, request_id: str) -> Optional[RequestContext]:
"""获取请求上下文"""
with self._requests_lock:
return self._requests.get(request_id)
def is_locked(self) -> bool:
"""检查锁是否被占用"""
return self._exec_lock.locked()
def get_current_request_id(self) -> Optional[str]:
"""获取当前执行的请求 ID"""
return self._current_request_id
def _get_queue_size(self) -> int:
"""获取等待中的请求数量"""
with self._requests_lock:
return sum(
1 for ctx in self._requests.values()
if ctx.status == RequestStatus.QUEUED
)
def get_status(self) -> Dict[str, Any]:
"""获取管理器状态"""
with self._requests_lock:
status_counts = {}
for ctx in self._requests.values():
s = ctx.status.value
status_counts[s] = status_counts.get(s, 0) + 1
return {
"is_locked": self.is_locked(),
"current_request": self._current_request_id,
"queue_size": status_counts.get("queued", 0),
"total_tracked": len(self._requests),
"status_counts": status_counts
}
# ================= 紧急操作 =================
def force_release(self) -> bool:
"""
强制释放锁(紧急情况使用)
Returns:
是否执行了释放
"""
logger.warning("⚠️ 执行强制释放锁")
released = False
# 取消当前请求
if self._current_request_id:
ctx = self.get_request(self._current_request_id)
if ctx:
ctx.request_cancel("force_release")
self._current_request_id = None
# 强制释放锁
try:
if self._exec_lock.locked():
self._exec_lock.release()
released = True
except RuntimeError:
pass
return released
# ================= 全局单例 =================
request_manager = RequestManager()
# ================= 辅助函数 =================
async def watch_client_disconnect(request, ctx: RequestContext,
check_interval: float = 0.5):
"""
监控客户端连接状态
在后台运行,检测到断开时设置取消标志
Args:
request: FastAPI Request 对象
ctx: 请求上下文
check_interval: 检查间隔(秒)
"""
try:
while not ctx.is_terminal():
# Starlette 的断开检测
if await request.is_disconnected():
ctx.request_cancel("client_disconnected")
logger.info(f"请求 [{ctx.request_id}] 客户端已断开")
break
await asyncio.sleep(check_interval)
except asyncio.CancelledError:
# 正常取消(请求结束时)
pass
except Exception as e:
logger.debug(f"断开检测异常: {e}")