bunsane 0.2.10 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/CHANGELOG.md +318 -0
  2. package/CLAUDE.md +20 -0
  3. package/config/cache.config.ts +12 -2
  4. package/core/App.ts +300 -69
  5. package/core/ApplicationLifecycle.ts +68 -4
  6. package/core/Entity.ts +525 -256
  7. package/core/EntityHookManager.ts +88 -21
  8. package/core/EntityManager.ts +12 -3
  9. package/core/Logger.ts +4 -0
  10. package/core/RequestContext.ts +4 -1
  11. package/core/SchedulerManager.ts +105 -22
  12. package/core/cache/CacheFactory.ts +3 -1
  13. package/core/cache/CacheManager.ts +72 -17
  14. package/core/cache/RedisCache.ts +38 -3
  15. package/core/components/BaseComponent.ts +12 -2
  16. package/core/decorators/EntityHooks.ts +24 -12
  17. package/core/middleware/RateLimit.ts +105 -0
  18. package/core/middleware/index.ts +1 -0
  19. package/core/remote/OutboxWorker.ts +42 -35
  20. package/core/scheduler/DistributedLock.ts +22 -7
  21. package/database/PreparedStatementCache.ts +5 -13
  22. package/gql/builders/ResolverBuilder.ts +4 -4
  23. package/gql/complexityLimit.ts +95 -0
  24. package/gql/index.ts +15 -3
  25. package/gql/visitors/ResolverGeneratorVisitor.ts +16 -2
  26. package/package.json +1 -1
  27. package/query/ComponentInclusionNode.ts +18 -11
  28. package/query/OrNode.ts +2 -4
  29. package/query/Query.ts +42 -31
  30. package/query/SqlIdentifier.ts +105 -0
  31. package/query/builders/FullTextSearchBuilder.ts +19 -6
  32. package/service/ServiceRegistry.ts +28 -9
  33. package/service/index.ts +4 -2
  34. package/storage/LocalStorageProvider.ts +12 -3
  35. package/storage/S3StorageProvider.ts +6 -6
  36. package/tests/e2e/http.test.ts +6 -2
  37. package/tests/integration/entity/Entity.saveTimeout.test.ts +110 -0
  38. package/tests/unit/cache/CacheManager.test.ts +20 -0
  39. package/tests/unit/entity/Entity.components.test.ts +73 -0
  40. package/tests/unit/entity/Entity.drainSideEffects.test.ts +51 -0
  41. package/tests/unit/entity/Entity.reload.test.ts +63 -0
  42. package/tests/unit/entity/Entity.requireComponents.test.ts +72 -0
  43. package/tests/unit/query/Query.emptyString.test.ts +69 -0
  44. package/tests/unit/query/Query.test.ts +6 -4
  45. package/tests/unit/scheduler/SchedulerManager.timeBased.test.ts +95 -0
  46. package/tests/unit/storage/S3StorageProvider.test.ts +6 -10
  47. package/upload/FileValidator.ts +9 -6
package/CHANGELOG.md ADDED
@@ -0,0 +1,318 @@
1
+ # Changelog
2
+
3
+ All notable changes to bunsane are documented here.
4
+
5
+ ## Unreleased
6
+
7
+ ### Added (HR-Screening ticket batch — BUNSANE-002..006)
8
+
9
+ - **`@ScheduledTask` allows entity-less time-based tasks.** Previously
10
+ `SchedulerManager.registerTask` rejected tasks without `query` or
11
+ `componentTarget`, contradicting documented "runs every hour" examples.
12
+ Time-based tasks now register successfully and invoke the handler with
13
+ no entity argument on each tick. Existing entity-targeted tasks
14
+ unchanged. Ticket BUNSANE-002.
15
+
16
+ - **`Entity.requireComponents(ctors)` hydrator.** Batched-load helper
17
+ that ensures the given component constructors are present on the
18
+ in-memory `componentList`. Required before `set` / `save` flows that
19
+ may trigger `@ComponentTargetHook` — hook matching reads
20
+ `componentList()` (in-memory only), so tag components must be loaded
21
+ first for the hook to fire. Ticket BUNSANE-003.
22
+
23
+ - **`ServiceRegistry` class named-exported.** `service/ServiceRegistry.ts`
24
+ now exports the class as named alongside the existing default-instance
25
+ export. Available via `service/index.ts` as `ServiceRegistryClass` for
26
+ type/subclass use; existing `ServiceRegistry` import remains the
27
+ singleton instance for backward compatibility. Ticket BUNSANE-004.
28
+
29
+ - **`CacheManager.invalidateEntities(ids: string[])`.** Batched helper
30
+ that invalidates both the entity-existence cache and all component
31
+ caches for a list of IDs. Call after a raw-SQL write (`db.unsafe`)
32
+ that bypasses `Entity.set` / `Entity.save`. Ticket BUNSANE-005.
33
+
34
+ - **`Entity.reload(opts?)` refresher.** Discards in-memory component
35
+ state and re-hydrates from the `components` table. Preserves entity
36
+ identity — callers holding a reference see fresh data on the same
37
+ instance. Use after raw-SQL writes or when a sibling `Entity`
38
+ instance with the same id mutated persisted data. Ticket BUNSANE-006.
39
+
40
+ - **Empty-string filter values supported.** `Query.filter(field, op, '')`
41
+ and the downstream SQL emit path (`ComponentInclusionNode`,
42
+ `PreparedStatementCache.execute`, `Query.doExec` / `doCount` /
43
+ `doAggregate` param validators) previously rejected empty /
44
+ whitespace-only values with "would cause PostgreSQL UUID parsing errors".
45
+ JSONB text extraction (`c.data->>'field'`) returns text, so `= ''` /
46
+ `!= ''` / `LIKE ''` are legitimate for text fields. The UUID-cast path
47
+ is gated by a value-side regex that an empty string cannot match, so
48
+ unsafe casts never fire. `findById('')` still throws — entity IDs
49
+ remain UUID-typed.
50
+
51
+ - **`Entity.drainPendingSideEffects(timeoutMs)`.** Drainable tracking
52
+ for post-commit work scheduled via `queueMicrotask` from `save()`
53
+ (cache invalidation + lifecycle hooks). Wired into `App.shutdown`
54
+ after `drainPendingCacheOps`. Tests under PGlite can call this in
55
+ `beforeAll` to settle prior-file background work before asserting.
56
+ Partial mitigation for BUNSANE-001 (Bun SQL / PGlite visibility race
57
+ — see `CLAUDE.md` PGlite section for full context).
58
+
59
+ ### Fixed (PR E — outbox, cache, query hardening)
60
+
61
+ - **OutboxWorker publishes to Redis concurrently and marks rows in bulk.**
62
+ Previously `processBatch` awaited each `publisher.xadd` serially inside
63
+ the PG transaction, holding `FOR UPDATE` row locks for up to N ×
64
+ `commandTimeout` when Redis was slow. Now uses `Promise.allSettled` to
65
+ publish the whole batch in parallel — worst-case lock hold drops to a
66
+ single xadd timeout. Followed by a single bulk `UPDATE … WHERE id IN
67
+ …` instead of N serial updates. Tickets H-DB-1 (partial — full fix
68
+ needs claim-via-column redesign so Redis latency is outside the PG
69
+ transaction entirely) and H-DB-3.
70
+
71
+ - **`Entity.save` pre-flights `ComponentRegistry.getReadyPromise` outside
72
+ the transaction.** Previously `doSave` awaited registry readiness from
73
+ inside `executeSave`, so a slow DDL (partition creation) would keep a PG
74
+ transaction idle. Pre-flight loop in `save()` awaits readiness before
75
+ opening the transaction; `doSave` now only asserts readiness and throws
76
+ if a caller bypassed `save()`. Ticket H-DB-4.
77
+
78
+ - **Entity.set / Entity.remove fire-and-forget cache ops now drainable on
79
+ shutdown.** Previously `setImmediate(async () => { … })` was untracked,
80
+ so SIGTERM could abandon in-flight cache writes. `Entity.pendingCacheOps`
81
+ is a drainable `Set<Promise<void>>`, and `Entity.drainPendingCacheOps`
82
+ is awaited by `App.shutdown` between HTTP drain and cache disconnect.
83
+ Ticket H-CACHE-1.
84
+
85
+ - **`CacheManager.shutdownProvider` descends into `MultiLevelCache` layers.**
86
+ Previously only checked the top-level provider for `disconnect` /
87
+ `stopCleanup` methods, so a MultiLevelCache deployment left its inner
88
+ MemoryCache cleanup timer and Redis connection alive forever. Now
89
+ dispatches to `getL1Cache()` and `getL2Cache()` when available. Ticket
90
+ H-CACHE-2.
91
+
92
+ - **`setComponentWriteThrough` preserves `createdAt` across updates.**
93
+ Previously every write-through stamped `createdAt: new Date()`,
94
+ corrupting the timeline across consecutive updates. Now peeks the
95
+ existing cache entry and preserves its `createdAt` when present; only
96
+ `updatedAt` is stamped fresh. Full fix (BaseComponent tracking
97
+ timestamps natively) deferred. Ticket H-CACHE-3.
98
+
99
+ - **Default query limit applied when `.take()` is omitted.** `Query.exec()`
100
+ now applies a framework-level default LIMIT
101
+ (env `BUNSANE_DEFAULT_QUERY_LIMIT`, default 10000, 0 to disable) and
102
+ emits a warning so runaway queries are visible. Ticket H-QUERY-1.
103
+
104
+ - **OrNode debug `console.log` traces removed from the production path.**
105
+ Ticket H-QUERY-2.
106
+
107
+ - **`unregisterDecoratedHooks` now actually unregisters.** Previously a
108
+ no-op stub that warned to stderr. Hook IDs returned from each
109
+ registration are stored in a `WeakMap<instance, string[]>` and passed
110
+ to `EntityHookManager.removeHook` on tear-down. Enables per-instance
111
+ cleanup in tests and service destruction. Ticket H-HOOK-3.
112
+
113
+ ### Fixed (PR D — scheduler + hook concurrency hardening)
114
+
115
+ - **Entity.add / Entity.set / Entity.remove hook calls no longer leak
116
+ unhandled rejections.** `EntityHookManager.executeHooks` is async, but
117
+ the three mutating methods previously invoked it without `await` and the
118
+ surrounding `try/catch` captured only synchronous throws. A hook
119
+ declared `async` that rejected escaped as an unhandled rejection. `set`
120
+ now `await`s consistently; `add` and `remove` remain synchronous (to
121
+ preserve their fluent-chain / boolean signatures) and attach a
122
+ `.catch` to the returned promise so rejections are logged rather than
123
+ escaping. Ticket H-HOOK-1.
124
+
125
+ - **Hook timeout timers no longer leak and late rejections no longer
126
+ escape.** All four timeout race sites in `EntityHookManager` (sync path,
127
+ async-parallel path, sync-batch path, async-batch path) now capture the
128
+ `setTimeout` handle and `clearTimeout` on normal completion, and
129
+ attach a detached `.catch` to the hook callback promise so a rejection
130
+ that arrives after the race has been decided is logged rather than
131
+ emitted as an unhandled rejection. Tickets H-HOOK-2 / H-MEM-2.
132
+
133
+ - **SchedulerManager task interval no longer burns lock attempts for a
134
+ still-running task.** `doExecuteTask` now skips early if
135
+ `taskInfo.isRunning` is true, avoiding a wasted PG advisory-lock
136
+ round-trip every tick when execution outlasts the interval. Increments
137
+ `skippedExecutions`. Ticket H-SCHED-1.
138
+
139
+ - **Scheduled-task retry timer is now tracked and cleared on stop.**
140
+ `handleTaskFailure` previously scheduled retries with a bare
141
+ `setTimeout` whose handle was never stored, so `stop()` could not
142
+ clear it and the retry fired post-shutdown against a closed DB pool.
143
+ The retry handle is now registered in `intervals` under
144
+ `<taskId>:retry:<n>` and self-deletes once fired. The retry callback
145
+ also checks `isRunning` before executing. Tickets H-SCHED-2 /
146
+ H-SCHED-3.
147
+
148
+ - **DistributedLock re-entry now reports overlap instead of success.**
149
+ `tryAcquire` previously returned `acquired: true` when the instance
150
+ already held the lock for `taskId`, which meant retry + interval could
151
+ both enter `executeTask` concurrently. Now returns
152
+ `acquired: false` so the second caller skips — defense-in-depth on
153
+ top of the caller-side `isRunning` guard. Ticket H-SCHED-4.
154
+
155
+ - **`executeWithTimeout` no longer leaks late rejections.** A scheduled
156
+ task that rejects after its wrapper timed out previously produced an
157
+ unhandled rejection (the wrapper was already settled). The wrapper now
158
+ uses a `settled` flag and logs late rejections instead of propagating.
159
+ Ticket H-SCHED-5.
160
+
161
+ - **DistributedLock `reservePromise` nulls on reject.** Previously, if
162
+ `db.reserve()` rejected (pool exhausted, shutdown mid-call), the
163
+ rejected promise was cached in `reservePromise` forever and every
164
+ subsequent `ensureReserved` received the same rejection. Now nulls the
165
+ promise in the reject handler so future callers retry a fresh reserve.
166
+ Ticket H-DB-2.
167
+
168
+ - **`App.waitForAppReady` no longer polls indefinitely.** Replaced the
169
+ 100ms `setInterval` with a one-shot phase listener and default 60s
170
+ timeout. A boot failure that never reaches `APPLICATION_READY` now
171
+ surfaces as a rejection instead of leaking a timer for process
172
+ lifetime. Ticket H-MEM-1.
173
+
174
+ ### Security
175
+
176
+ - **SQL injection hardening across Query layer.** Identifiers (component
177
+ table names, JSON field paths, ORDER BY properties, text-search language)
178
+ interpolated into SQL via `db.unsafe(...)` or template literals are now
179
+ validated against strict allow-lists before use. Added `query/SqlIdentifier.ts`
180
+ with `assertIdentifier`, `assertComponentTableName`, `assertFieldPath`,
181
+ `assertTsLanguage`. Applied at `Query.estimatedCount`, `Query.doAggregate`,
182
+ `ComponentInclusionNode` sort expressions (3 sites), and
183
+ `FullTextSearchBuilder` (3 sites + factory). Throws `InvalidIdentifierError`
184
+ on unsafe input. Ticket C08.
185
+
186
+ - **GraphQL depth limit hard minimum enforced.** Previously `maxDepth: 0`
187
+ or `undefined` silently disabled the depth-limit guard, allowing CPU/memory
188
+ DoS via deeply nested queries. Now `createYogaInstance` enforces a hard
189
+ floor of 15 regardless of input; callers can raise but cannot disable.
190
+ Ticket C06.
191
+
192
+ - **Request AbortSignal now propagates into Yoga and REST handlers.** The
193
+ 30s wall-clock timer previously only logged a warning; the signal was
194
+ never forwarded downstream. Request timeouts (and client disconnects) now
195
+ cancel in-flight resolvers, DB queries, and external calls. Uses
196
+ `AbortSignal.any` (Bun/Node 20+) with a manual combiner fallback.
197
+ Ticket C05.
198
+
199
+ ### Fixed
200
+
201
+ - **Sync lifecycle hooks now awaited, preventing unhandled rejections.**
202
+ `EntityHookManager.executeHooks` previously discarded the return value of
203
+ `hook.callback(event)` on the sync path when no timeout was configured.
204
+ A hook mistakenly declared `async: false` but implemented as an
205
+ `async function` would silently throw unhandled rejections, crashing the
206
+ process under strict mode. Sync path now awaits consistently. Ticket C13.
207
+
208
+ - **`createRequestContextPlugin` auto-applied by default.** Previously
209
+ opt-in (and the export was commented out of the root barrel), so any app
210
+ using `@BelongsTo` / `@HasMany` relations silently fell into N+1 query
211
+ patterns. `App` now prepends the plugin to Yoga plugins by default. Opt
212
+ out via `App.disableRequestContextPlugin()` if supplying your own
213
+ DataLoader layer. Ticket C07.
214
+
215
+ - **Redis cache no longer causes unbounded heap growth when Redis is
216
+ unreachable.** `enableOfflineQueue` now defaults to `false` so commands
217
+ fail fast and the caller's `try/catch` treats it as a cache miss instead
218
+ of queuing indefinitely. Can be overridden per-deployment via
219
+ `REDIS_ENABLE_OFFLINE_QUEUE=true` when you accept the memory risk.
220
+ Ticket C02.
221
+
222
+ - **Redis reconnect storm capped.** `retryStrategy` now returns `null`
223
+ after `maxReconnectAttempts` (default 20) so a permanently unreachable
224
+ Redis cannot spin forever, saturating logs and keeping the ioredis
225
+ state machine busy. Configurable via `REDIS_MAX_RECONNECT_ATTEMPTS`.
226
+ Default inter-attempt delay also raised from `times * 50` to
227
+ `times * 200` (capped at 2s) for a gentler back-off. Ticket C03.
228
+
229
+ - **`App.init()` now awaits `CacheManager.initialize()`.** Previously only
230
+ `getInstance()` was called so pub/sub cross-instance invalidation was
231
+ never set up and any app-supplied cache config was silently ignored.
232
+ Added `App.setCacheConfig(config)` so callers can supply a partial
233
+ config that is merged with `defaultCacheConfig` and passed to
234
+ `initialize()`. Ticket C04.
235
+
236
+ - **`Entity.doDelete` no longer leaks `idle in transaction` backends on timeout.**
237
+ Same AbortController + in-flight query cancellation pattern as `Entity.save`.
238
+ Post-commit cache invalidation and lifecycle hooks moved out of the save
239
+ budget via `queueMicrotask`. Ticket C01.
240
+
241
+ - **`SYSTEM_READY` phase errors are no longer swallowed silently.**
242
+ Previously a schema-build, REST-registration, or scheduler-init failure was
243
+ caught and only logged, leaving the app stuck at `isReady=false` with
244
+ `/health/ready` returning 503 forever and k8s rollouts blocked indefinitely.
245
+ Now marks the app unready, logs at fatal level, and exits so the orchestrator
246
+ can restart. In tests, rethrows instead of exiting. Ticket C09.
247
+
248
+ - **HTTP server drain is now awaited before tearing down dependencies.**
249
+ `server.stop(false)` previously initiated drain but was not awaited, so the
250
+ scheduler / cache / DB pool closed while requests were still executing,
251
+ causing cascade failures in the final seconds of shutdown. Shutdown now
252
+ polls pending requests (bounded by `shutdownGracePeriod`) before force-close,
253
+ then stops each subsystem in order. Ticket C10.
254
+
255
+ - **ApplicationLifecycle phase listeners are now captured and removed on
256
+ shutdown.** Five singletons (`App`, `EntityManager`, `EntityHookManager`,
257
+ `SchedulerManager`, `ServiceRegistry`) previously registered listeners
258
+ without storing refs, so each `init()` call (common in tests) stacked
259
+ listeners on the singleton `EventTarget`, permanently leaking memory and
260
+ firing duplicate phase handlers. Each now captures the listener reference
261
+ and exposes a `dispose()` / `disposeLifecycleIntegration()` method called
262
+ from `App.shutdown()`. `init()` paths are also idempotent. Ticket C11.
263
+
264
+ - **`ApplicationLifecycle.waitForPhase` replaced 100ms busy-loop with a
265
+ listener-based Promise.** Previously a `while (currentPhase !== phase)`
266
+ loop polling every 100ms; if the target phase was never reached (see
267
+ SYSTEM_READY fix above) every caller hung forever. Now attaches a one-shot
268
+ phase listener + `timeoutMs` (default 30s). Rejects with a descriptive
269
+ error on timeout. Ticket C12.
270
+
271
+ - **`SchedulerManager.stop()` now awaits in-flight tasks before returning.**
272
+ Previously cleared timers and returned immediately; any task mid-execution
273
+ continued running against a DB pool that was about to close in
274
+ `App.shutdown()`. Now tracks each `executeTask` promise in a Set, and
275
+ `stop(drainTimeoutMs = 15_000)` awaits `Promise.allSettled` bounded by the
276
+ timeout. Scheduler listener also disposed. Ticket C14.
277
+
278
+ - **Process-level error handlers (`unhandledRejection`, `uncaughtException`)
279
+ and signal handlers (`SIGTERM`, `SIGINT`) now registered at the top of
280
+ `App.init()` instead of only in `start()`.** Previously any rejection
281
+ during boot (DB prep, component registration, cache init) was silently
282
+ discarded by the runtime. Signal handlers now use `process.once` so a
283
+ double SIGTERM cannot fire two concurrent shutdown paths. Ticket C15.
284
+
285
+ - **`Entity.save` no longer leaks `idle in transaction` backends on timeout.**
286
+ The previous implementation wrapped `db.transaction(...)` in a JS `setTimeout`
287
+ and rejected the outer promise when the timer fired, but the underlying Bun
288
+ SQL transaction continued on the server with no `COMMIT`/`ROLLBACK` ever
289
+ sent. Under pgbouncer `transaction` pool mode this pinned backend sessions
290
+ permanently, exhausting the pool and cascading into further save timeouts.
291
+
292
+ `Entity.save` now threads an `AbortSignal` into `doSave`. When the wall-clock
293
+ timer fires the signal is aborted, the in-flight `SQL.Query` is cancelled
294
+ via `.cancel()`, and the cancellation propagates out of the transaction
295
+ callback, triggering Bun SQL's automatic `ROLLBACK` and releasing the
296
+ pooled connection. The `DB_STATEMENT_TIMEOUT` env var (already supported
297
+ in `database/index.ts`) acts as a PostgreSQL-side backstop.
298
+
299
+ See `docs` / handoff dated 2026-04-18 for incident details.
300
+
301
+ ### Changed
302
+
303
+ - **Post-commit side effects (cache invalidation, lifecycle hooks) no longer
304
+ block `Entity.save`.** `handleCacheAfterSave` and `EntityHookManager.executeHooks`
305
+ are now queued via `queueMicrotask` after the transaction commits. Save
306
+ resolves as soon as the DB write is durable; cache or hook latency cannot
307
+ consume the save budget or surface as save failures. Errors are logged
308
+ and swallowed (matching prior error-handling behavior).
309
+
310
+ ### Added
311
+
312
+ - **`DB_SAVE_PROFILE=true` env var** — when set, `Entity.save` logs per-phase
313
+ timings (`db`, `cache`, `hooks`, `total`) at info level. Off by default.
314
+
315
+ - **Integration tests** in `tests/integration/entity/Entity.saveTimeout.test.ts`
316
+ covering: aborted save leaves no partial rows, pool stays healthy after
317
+ repeated aborts, backwards-compatible signal-less `doSave`, non-blocking
318
+ post-commit work.
package/CLAUDE.md CHANGED
@@ -159,6 +159,26 @@ The wrapper script:
159
159
  - `?|` and `?&` operators not supported (use `@>` / `<@` instead)
160
160
  - `CREATE INDEX CONCURRENTLY` not supported
161
161
  - Single connection only (`POSTGRES_MAX_CONNECTIONS=1`)
162
+ - **Known Bun SQL + PGlite visibility race**: under a single-connection
163
+ pool with background work from a prior test file, `await entity.save()`
164
+ may resolve ≥1ms before the `INSERT INTO entity_components` row is
165
+ visible to a subsequent `db.unsafe('SELECT ...')` on the same driver.
166
+ The per-component partition INSERT (e.g. `components_<compname>`) in
167
+ the same transaction is visible immediately; only the flat
168
+ `entity_components` row lags. This causes multi-component Query
169
+ INTERSECTs to return 0 rows when run immediately after save.
170
+
171
+ **Mitigation (not a full fix):**
172
+ 1. Shut down application-owned background workers (AI processors,
173
+ outbox workers, hook-driven save chains) in `afterAll` of each test
174
+ file — prevents cross-file bleed that amplifies the race.
175
+ 2. Call `await Entity.drainPendingSideEffects()` in `beforeAll` / test
176
+ `setup` to settle hook-triggered post-commit work (helpful but
177
+ insufficient on its own).
178
+ 3. Skip the affected test or poll with a short `setTimeout(1)`.
179
+
180
+ Root cause lives in the Bun SQL adapter's ACK handling, not bunsane.
181
+ File upstream with a minimal repro if you hit this.
162
182
 
163
183
  ## Directory Structure
164
184
 
@@ -14,9 +14,13 @@ export interface CacheConfig {
14
14
  password?: string;
15
15
  db?: number;
16
16
  keyPrefix?: string;
17
- retryStrategy?: (times: number) => number | void;
17
+ retryStrategy?: (times: number) => number | null | void;
18
18
  connectTimeout?: number;
19
19
  commandTimeout?: number;
20
+ /** Give up after this many reconnect attempts. Default 20. */
21
+ maxReconnectAttempts?: number;
22
+ /** Queue commands while offline. Default false (fail-fast). */
23
+ enableOfflineQueue?: boolean;
20
24
  };
21
25
 
22
26
  entity?: {
@@ -53,8 +57,14 @@ export const defaultCacheConfig: CacheConfig = {
53
57
  password: process.env.REDIS_PASSWORD,
54
58
  db: parseInt(process.env.REDIS_DB || '0'),
55
59
  keyPrefix: process.env.REDIS_KEY_PREFIX || 'bunsane:',
60
+ // Cap reconnect attempts so a permanently unreachable Redis doesn't
61
+ // spin forever (C03). Tune via REDIS_MAX_RECONNECT_ATTEMPTS.
62
+ maxReconnectAttempts: parseInt(process.env.REDIS_MAX_RECONNECT_ATTEMPTS || '20'),
63
+ // Fail-fast on outage instead of unbounded offline queue (C02).
64
+ // Override only if caller accepts the memory risk.
65
+ enableOfflineQueue: process.env.REDIS_ENABLE_OFFLINE_QUEUE === 'true',
56
66
  retryStrategy: (times: number) => {
57
- const delay = Math.min(times * 50, 2000);
67
+ const delay = Math.min(times * 200, 2000);
58
68
  return delay;
59
69
  }
60
70
  },