fix(锁与查询组): #12 熔断器状态转换加锁;#10 问答人名匹配改 JSON_CONTAINS 精确匹配(防 LIKE 子串/特殊字符误匹配);#4 OracleDB 复合写加锁;#6 每消费者独立 VideoProcessor(消除 adapter.timeout 共享竞争);#15 done 视频文件被覆盖(mtime>processed_at)自动重置重分析

This commit is contained in:
ericwyuan
2026-08-21 14:24:12 +08:00
parent 034dca92b2
commit 4e85a98944
4 changed files with 68 additions and 34 deletions

View File

@@ -8,6 +8,7 @@
"""
from collections import deque
import time
import threading
class CircuitBreaker:
@@ -21,27 +22,31 @@ class CircuitBreaker:
self.cooldown = cooldown
self.state = 'CLOSED'
self.last_failure = None
self._lock = threading.Lock() # 状态转换加锁,防多线程竞争
def record_failure(self):
if not self.enabled:
return
self.failures.append(time.time())
if len(self.failures) >= self.threshold:
self.state = 'OPEN'
self.last_failure = time.time()
with self._lock:
self.failures.append(time.time())
if len(self.failures) >= self.threshold:
self.state = 'OPEN'
self.last_failure = time.time()
def record_success(self):
if not self.enabled:
return
self.failures.clear()
self.state = 'CLOSED'
with self._lock:
self.failures.clear()
self.state = 'CLOSED'
def is_open(self):
if not self.enabled:
return False
if self.state == 'OPEN' and self.last_failure and time.time() - self.last_failure > self.cooldown:
self.state = 'HALF_OPEN'
return self.state == 'OPEN'
with self._lock:
if self.state == 'OPEN' and self.last_failure and time.time() - self.last_failure > self.cooldown:
self.state = 'HALF_OPEN'
return self.state == 'OPEN'
def __repr__(self):
return f"CircuitBreaker(state={self.state}, enabled={self.enabled})"