feat(gateway): monotonic clock helper (1879)
This commit is contained in:
0
ingestor/connection/__init__.py
Normal file
0
ingestor/connection/__init__.py
Normal file
19
ingestor/connection/clock.py
Normal file
19
ingestor/connection/clock.py
Normal file
@@ -0,0 +1,19 @@
|
||||
import time
|
||||
|
||||
|
||||
class MonotonicClock:
|
||||
"""Single monotonic reference for latency/timeout math (1879 §2).
|
||||
|
||||
Wall-clock (`now_wall`) is only for human-facing timestamps; all elapsed/
|
||||
timeout math must use `elapsed()` so NTP re-sync can't produce negatives.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self._start = time.monotonic()
|
||||
|
||||
def elapsed(self) -> float:
|
||||
return time.monotonic() - self._start
|
||||
|
||||
@staticmethod
|
||||
def now_wall() -> float:
|
||||
return time.time()
|
||||
0
tests/unit/connection/__init__.py
Normal file
0
tests/unit/connection/__init__.py
Normal file
8
tests/unit/connection/test_clock.py
Normal file
8
tests/unit/connection/test_clock.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from ingestor.connection.clock import MonotonicClock
|
||||
|
||||
|
||||
def test_elapsed_is_non_negative_and_monotonic():
|
||||
clk = MonotonicClock()
|
||||
a = clk.elapsed()
|
||||
b = clk.elapsed()
|
||||
assert a >= 0 and b >= a # never negative even if wall clock jumps back
|
||||
Reference in New Issue
Block a user