Repository navigation
feat(server): analytics and limits as Effect service packages - #84
Conversation
Code review — PR 84 / issue 73Reviewed StandardsChecked: Effect namespace imports, one-file services with inline interfaces, Hard — namespace at a service boundary
import { Limits, MAX_TRACKED_KEYS, layer } from "./Limits.js";
// ...
it.layer(layer)("Limits", (it) => {
Judgement — glossary / Mysterious Name
Judgement — Feature EnvyCONTEXT Shutdown flush is “the PostHog layer’s finalizer”. Construction is Not findings: SpecDone when: yes. No limiter or analytics implementation remains in Checklist that landed: Retry-After + fail-closed tested on the service ( (a) Missing / partialNone that fail Done when. Spec asked for a thin (b) Not asked for
(c) Implemented but wrong / fragileSpec: “the interface must allow a Redis-backed layer later with no consumer change” and per-call Related weakening, not in the ticket’s wording but in “today’s semantics”: the old config rejected a non-http(s) Implementation methodSound, with one leftover cleverness in Limits that is heavier than a 10k in-memory store needs. Splitting The
Config selection is clean: Do not merge PostHog into Analytics. Do not add Redis until a replica exists. YAGNICut it. The insertion-order prune in Keep it. Redis-later is the good kind: one Beautiful. Default-off in one expression: export const layer = Layer.unwrap(
Effect.map(Config.option(PostHogClient.apiKey), (key) =>
Option.isNone(key) ? layerNoop : layerPostHog.pipe(Layer.provide(PostHogClient.layer))
)
);Closed event names, consumer-prefixed keys, shutdown TestClock Verdict: mostly-lean, with the Map-as-queue prune the one thing that isn’t. Standards: 1 hard finding, 2 judgement calls; worst is the namespace import in |
|
Went through the review. Pushed the follow-ups as one commit. Fixed
Not changing, with reasons
|
Analytics and the fixed-window rate limiter were modules in apps/server with two consumers each once auth and patches exist, so they port first. packages/analytics: the Analytics service (track never fails), a PostHog layer over a PostHogClient service whose finalizer flushes and gives up after 3 s, a no-op layer, and a layer that picks by PATCHY_POSTHOG_API_KEY read as a Redacted Config. packages/limits: the Limits service mirroring RateLimiter.consume (key, limit, window per call; remaining and a Retry-After back), in memory, failing closed at 10k tracked keys. The Fastify app consumes both through one ManagedRuntime built in start.ts; the old modules, their tests and the posthog fields in @patchy/config are gone. CONTEXT.md for each package; language-service rules at error.
Limits: drop the insertion-order prune, which only held when every window was the same length; a key's own expired window is replaced on the spot and the store is scanned end to end only when it is full. Rename Bucket to Window to match the glossary; the test imports the service module as a namespace. Analytics: PATCHY_POSTHOG_HOST must again be an http(s) URL, failing startup on a typo as the old config did.
c9c05c4 to
4d73388
Compare
What changed
Analytics and the fixed-window rate limiter lived in
apps/serveras plain modules, butauthandpatcheswill both need them once they port, so they had to move onto Effect first. This PR turns each into a service package and leaves the Fastify app consuming them through one runtime seam.packages/analytics: anAnalyticsservice whosetracknever fails; a PostHog layer over aPostHogClientservice, with a finalizer that flushes and gives up after 3 s; a no-op layer; and alayerthat picks between them byPATCHY_POSTHOG_API_KEY, read as aRedactedEffectConfig. A test injects a broken backend by providing anotherPostHogClientlayer; the flush bound is tested withTestClock.packages/limits: aLimitsservice mirroringRateLimiter.consume(key, limit and window per call; remaining and aRetry-Afterback). In memory, fails closed at 10k tracked keys, window expiry and the cap tested through the service withTestClock. A Redis-backed store later is a layer swap.apps/server: oneManagedRuntimebuilt instart.ts, two adapters inruntime.ts(consume,track);analytics.ts,rate-limit.tsand their tests are gone, the emitter tests run against a recording layer, and theposthog*fields leave@patchy/config.CONTEXT.mdfor both packages, language-service rules promoted toerror.One semantic nuance worth knowing: the four per-minute limits now share one store (keys are prefixed by limit name), so the 10k fail-closed cap is across all of them rather than per limiter. That matches the
RateLimitershape the issue asked for and only matters under a flood.Stacked on #83 (
feat/sql-package); merge that first.Closes #73
How it was verified
pnpm testpasses locally (alsopnpm lint,pnpm typecheck,pnpm format:check)/healthzanswers, a protected route is limited and 401s as before, SIGINT shuts down cleanly through the analytics finalizer.Release impact
Environment variables and every wire response are unchanged; internal refactor.
Built by Claude Fable 5 via Claude Code.