abinazebinoy commited on
Commit
45f51d2
·
1 Parent(s): c7c0765

fix(H-06): add lockless pre-check in get_driver() -- prevents lock contention serializing all requests under load

Browse files
Files changed (1) hide show
  1. api/dependencies.py +9 -2
api/dependencies.py CHANGED
@@ -39,9 +39,16 @@ def get_driver():
39
 
40
  now = time.monotonic()
41
 
 
 
 
 
 
 
 
42
  with _driver_lock:
43
- # Driver healthy and verified recently -- return immediately without
44
- # calling verify_connectivity() again (BUG-20 key fix)
45
  if _driver is not None and (now - _last_verified_at) < _VERIFY_TTL:
46
  return _driver
47
 
 
39
 
40
  now = time.monotonic()
41
 
42
+ # H-06 FIX: lockless fast path -- if driver is valid and TTL fresh,
43
+ # return immediately without acquiring the lock. The lock is only needed
44
+ # for reconnection (rare). Under 50 concurrent requests this prevents
45
+ # all threads serializing behind the lock for a trivial pointer check.
46
+ if _driver is not None and (now - _last_verified_at) < _VERIFY_TTL:
47
+ return _driver
48
+
49
  with _driver_lock:
50
+ # Re-check inside lock in case another thread just reconnected
51
+ now = time.monotonic()
52
  if _driver is not None and (now - _last_verified_at) < _VERIFY_TTL:
53
  return _driver
54