|
| 1 | +import { $replica } from "~/db.server"; |
| 2 | +import { env } from "~/env.server"; |
| 3 | +import { logger } from "~/services/logger.server"; |
| 4 | +import { BoundedTtlCache } from "~/services/realtime/boundedTtlCache"; |
| 5 | +import { singleton } from "~/utils/singleton"; |
| 6 | +import { FEATURE_FLAG } from "~/v3/featureFlags"; |
| 7 | + |
| 8 | +/** |
| 9 | + * Which coordinator mints a NEW waitpoint. Consulted at the mint and never again: every |
| 10 | + * later operation routes by id shape. A flip therefore changes only where the NEXT |
| 11 | + * waitpoint is born, which is why this needs no flip-grace machinery. |
| 12 | + */ |
| 13 | +export type WaitpointMintKind = "legacy" | "store"; |
| 14 | + |
| 15 | +/** The flag's vocabulary, deliberately not the coordinator's. */ |
| 16 | +type WaitpointSystemFlag = "legacy" | "redis"; |
| 17 | + |
| 18 | +type MintKindDeps = { |
| 19 | + globalDefault: WaitpointSystemFlag; |
| 20 | + /** Undefined when the org has no override. Must not hit the DB when given org flags. */ |
| 21 | + flag: ( |
| 22 | + orgId: string, |
| 23 | + orgFeatureFlags: unknown | undefined |
| 24 | + ) => Promise<WaitpointSystemFlag | undefined>; |
| 25 | +}; |
| 26 | + |
| 27 | +// PURE CORE — no env import; the tests drive this directly. |
| 28 | +export async function computeWaitpointMintKind( |
| 29 | + environment: { organizationId: string; id: string; orgFeatureFlags?: unknown }, |
| 30 | + deps: MintKindDeps |
| 31 | +): Promise<WaitpointMintKind> { |
| 32 | + try { |
| 33 | + const perOrg = await deps.flag(environment.organizationId, environment.orgFeatureFlags); |
| 34 | + return (perOrg ?? deps.globalDefault) === "redis" ? "store" : "legacy"; |
| 35 | + } catch (error) { |
| 36 | + // Fail safe, as computeRunIdMintKind does: a flag-read failure degrades to the old |
| 37 | + // path rather than becoming a trigger-path outage. |
| 38 | + logger.error("[waitpointMintKind] flag read failed; minting legacy (fail-safe)", { error }); |
| 39 | + return "legacy"; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +const mintCache = singleton( |
| 44 | + "waitpointMintCache", |
| 45 | + () => |
| 46 | + new BoundedTtlCache<WaitpointSystemFlag | null>( |
| 47 | + env.WAITPOINT_MINT_FLAG_CACHE_TTL_MS, |
| 48 | + env.WAITPOINT_MINT_FLAG_CACHE_MAX_ENTRIES |
| 49 | + ) |
| 50 | +); |
| 51 | + |
| 52 | +// ENV-BOUND wrapper — the only place env and $replica are read. |
| 53 | +export async function resolveWaitpointMintKind(environment: { |
| 54 | + organizationId: string; |
| 55 | + id: string; |
| 56 | + /** Pass environment.organization.featureFlags from the call site. */ |
| 57 | + orgFeatureFlags?: unknown; |
| 58 | +}): Promise<WaitpointMintKind> { |
| 59 | + return computeWaitpointMintKind(environment, { |
| 60 | + globalDefault: env.WAITPOINT_SYSTEM_DEFAULT, |
| 61 | + flag: async (orgId, orgFeatureFlags) => { |
| 62 | + // null is a cached "this org has no override", which must stay distinct from a miss: |
| 63 | + // BoundedTtlCache reports a stored undefined as a miss, so never store undefined. |
| 64 | + const cached = mintCache.get(orgId); |
| 65 | + if (cached !== undefined) { |
| 66 | + return cached ?? undefined; |
| 67 | + } |
| 68 | + |
| 69 | + // Hot-path pass-through: only read the replica when the caller passed no org flags. |
| 70 | + const overrides = |
| 71 | + orgFeatureFlags !== undefined |
| 72 | + ? orgFeatureFlags |
| 73 | + : ( |
| 74 | + await $replica.organization.findFirst({ |
| 75 | + where: { id: orgId }, |
| 76 | + select: { featureFlags: true }, |
| 77 | + }) |
| 78 | + )?.featureFlags; |
| 79 | + |
| 80 | + const value = (overrides as Record<string, unknown> | null | undefined)?.[ |
| 81 | + FEATURE_FLAG.waitpointSystem |
| 82 | + ]; |
| 83 | + const resolved = value === "redis" || value === "legacy" ? value : null; |
| 84 | + |
| 85 | + mintCache.set(orgId, resolved); |
| 86 | + return resolved ?? undefined; |
| 87 | + }, |
| 88 | + }); |
| 89 | +} |
0 commit comments