(organizationId: string)
| 147 | }; |
| 148 | |
| 149 | const decide = (organizationId: string): Effect.Effect<GateDecision> => |
| 150 | Effect.suspend(() => { |
| 151 | const nowMs = Date.now(); |
| 152 | const cached = cache.get(organizationId); |
| 153 | if (cached && cached.expiresAtMs > nowMs) { |
| 154 | return Effect.succeed(toDecision(organizationId, cached.allowed)); |
| 155 | } |
| 156 | return checkBalance(organizationId).pipe( |
| 157 | Effect.timeoutOrElse({ |
| 158 | duration: `${timeoutMs} millis`, |
| 159 | orElse: () => Effect.fail(new GateCheckTimeoutError({ timeoutMs })), |
| 160 | }), |
| 161 | Effect.map(({ allowed }) => { |
| 162 | writeCache(organizationId, allowed, nowMs); |
| 163 | return toDecision(organizationId, allowed); |
| 164 | }), |
| 165 | // FAIL OPEN: Autumn errors, timeouts, and missing customers/features |
| 166 | // must never block executions. Reported like `trackExecution` so a |
| 167 | // billing outage still pages; the error outcome is never cached. |
| 168 | Effect.catch((error: unknown) => |
| 169 | Effect.gen(function* () { |
| 170 | yield* Effect.sync(() => { |
| 171 | console.warn("[billing] execution balance check failed open:", error); |
| 172 | }); |
| 173 | yield* captureCauseEffect(error); |
| 174 | return { blocked: false } as const satisfies GateDecision; |
| 175 | }), |
| 176 | ), |
| 177 | ); |
| 178 | }); |
| 179 | |
| 180 | return { |
| 181 | /** Wrap an engine so the balance gate runs before each new execution. */ |
no test coverage detected