refactor: converge the patterns multiple authors left divergent
The codebase was written by several agents and had the same concept done more
than one way. This makes it read as one voice, with no behaviour change.
Dedup, each to a single canonical form:
- INSTANCE_COLUMNS: the 13-column instances SELECT list existed as _COLUMNS in
instances.py and reconcile.py (byte-identical) and inlined a third time in
the worker. One exported constant now.
- Settings.runtime_dsn: the three entrypoints each chose between str(pg_dsn)
and pg_dsn.unicode_string(). One property.
- yaml_tempfile: helm._ValuesFile and k8s._ManifestFile were the same
write-yaml-to-a-temp-dir context manager. One helper in adapters/tempyaml.py.
- services/_runtime.py: sleep_or_stop and install_stop_signals were copied
between the worker and reconciler loops. One module, so shutdown behaviour
cannot drift between them.
- k8s.ensure_namespace used MANAGED_BY_LABEL/VALUE from helm.py instead of a
hardcoded literal, so the managed-by label has one definition.
- SvcforgeError is now the root of every svcforge exception (CatalogError,
IllegalTransition, BadWindow, HandlerError), keeping each stdlib base in the
MRO, so `except SvcforgeError` means what errors.py says it does.
- ERROR_MAX_CHARS replaces the repeated `[-2000:]` truncation feeding the same
error columns.
- the reconciler reads settings.metrics_port like the worker, dropping its
duplicate DEFAULT_METRICS_PORT and redundant --metrics-port option; the
SVCFORGE_METRICS_PORT env override still applies through pydantic.
Two smaller correctness/consistency fixes:
- RateLimitResult.retry_after_s computed its delta against datetime.now(UTC)
while the limiter runs on an injectable clock, so it was meaningless under a
FakeClock and drifted by request latency in production. It now carries a
checked_at from the same clock as reset_at.
- handle_provision's notifier.send is wrapped like the reconciler's: a flaky
webhook after the READY CAS would fail the task, and the retry would hit the
READY early-return and drop the notification, turning a good provision into a
failed one.
This commit is contained in:
+7
-1
@@ -145,7 +145,12 @@ class FakeRateLimiter:
|
||||
# Fails OPEN, exactly like the real one. A limiter that refused here would make
|
||||
# "Redis is down" indistinguishable from "you are over quota".
|
||||
return RateLimitResult(
|
||||
allowed=True, limit=self.limit, remaining=self.limit, reset_at=reset_at, degraded=True
|
||||
allowed=True,
|
||||
limit=self.limit,
|
||||
remaining=self.limit,
|
||||
reset_at=reset_at,
|
||||
checked_at=self.clock.now(),
|
||||
degraded=True,
|
||||
)
|
||||
key = f"rl:{team}:{window}"
|
||||
n = self.counts.get(key, 0) + 1
|
||||
@@ -155,6 +160,7 @@ class FakeRateLimiter:
|
||||
limit=self.limit,
|
||||
remaining=max(0, self.limit - n),
|
||||
reset_at=reset_at,
|
||||
checked_at=self.clock.now(),
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -434,7 +434,7 @@ async def test_the_budget_metric_is_exposed_and_labelled_by_op() -> None:
|
||||
"""`curl -s localhost:8000/metrics | grep svcforge_redis_commands_total`."""
|
||||
limiter = FakeRateLimiter(limit=10, window_s=60, clock=FakeClock(start=_T0))
|
||||
await limiter.check("acme") # the fake does not touch the real counter
|
||||
RateLimitResult(allowed=True, limit=10, remaining=9, reset_at=_T0)
|
||||
RateLimitResult(allowed=True, limit=10, remaining=9, reset_at=_T0, checked_at=_T0)
|
||||
|
||||
text = generate_latest(REGISTRY).decode()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user