diff --git a/CHANGELOG.md b/CHANGELOG.md index c021ca63a..c39c1cbbf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ Semver applies from 1.0.0. A breaking change to a documented API needs a major ### Changed +- `cache.defaultTtlMs` now sets the lease of a value cached with no `ttlMs` on the `lru` and `redis` rungs the boot builds. It was validated and documented and read by nothing; a boot-built Redis rung held such values 300 s, now the configured 60 s by default. A rung supplied through `cacheTiers` keeps its own default (#1260). - A fix line that ends `then: x …` prints `bunx x …`, as does the gate's stderr pointer and `x errors explain`; a shipped guard's fix re-runs its own step (`x verify --only boundaries`), never the whole gate. - Fixes no longer send a worker to a whole run: `X_TEST_NO_FILES` suggests `x test --affected --dirty`, `X_TYPECHECK_FAILED` names the file to edit and `x verify --only typecheck`, and an unknown command whose nearest match runs every suite suggests its `--help`. - A ledger wait says to leave the command running (`bunx x heavy status`), and reads `admission in progress` while no holder is known. diff --git a/packages/cli/src/runtime-cache-limits.test.ts b/packages/cli/src/runtime-cache-limits.test.ts index f19685ebd..1238656f5 100644 --- a/packages/cli/src/runtime-cache-limits.test.ts +++ b/packages/cli/src/runtime-cache-limits.test.ts @@ -111,3 +111,32 @@ describe('unit · a supplied redis rung with no client of its own', () => { expect(defaultRedisWarnings()).toEqual([]); }); }); + +describe('cache.defaultTtlMs', () => { + const leaseOf = async (tiers: readonly CacheTierName[], supplied: readonly CacheTier[]) => { + release = startCacheTiers({ + env: {}, + purge: noopPurgeDriver(), + transport: new InProcessTransport(), + tiers, + supplied, + defaultTtlMs: 5_000, + }); + const before = Date.now(); + await rung('lru').set('k', 1); + const entry = await rung('lru').get('k'); + return (entry?.expiresAt ?? expect.unreachable('the value was not held')) - before; + }; + + test('is the lease of a value set with no ttlMs on the rung the boot builds', async () => { + const lease = await leaseOf(['request-memo', 'lru'], []); + // The tier's own default is 60 s; jitter shaves at most 5 % off 5 s. + expect(lease).toBeLessThanOrEqual(5_000 + 50); + expect(lease).toBeGreaterThan(4_000); + }); + + test('leaves a supplied rung with the lease it was built with', async () => { + const lease = await leaseOf(['request-memo', 'lru'], [lruTier({ defaultTtlMs: 120_000 })]); + expect(lease).toBeGreaterThan(100_000); + }); +}); diff --git a/packages/cli/src/runtime-cache.ts b/packages/cli/src/runtime-cache.ts index 7e6169681..b6caf6f9a 100644 --- a/packages/cli/src/runtime-cache.ts +++ b/packages/cli/src/runtime-cache.ts @@ -74,10 +74,17 @@ export interface CacheTiersOptions { * a convention to remember. `cacheTiersOf` is what a boot holding the loaded config calls for it. */ readonly tiers: readonly CacheTierName[]; + /** + * `config.cache.defaultTtlMs`: the lease of a value set with no `ttlMs`, on the `lru` and `redis` + * rungs the boot builds. Omitted, each rung keeps its own default. A supplied rung keeps whatever + * it was built with: the replacement is the whole rung. + */ + readonly defaultTtlMs?: number | undefined; /** * `runtime.ts`'s `cacheTiers`: each replaces the rung of its own `name`, and it is the ONE way * to set a rung's limits (`lruTier({ maxBytes })`, `redisTier({ maxValueBytes, jitterFraction })`): - * `buildTier` passes none, and a config key would be a second spelling of this replacement. A + * `buildTier` passes none but `defaultTtlMs`, and a config key would be a second spelling of + * this replacement. A * supplied `redis` tier holding its own `client` needs no `REDIS_URL`; one without is * `warnDefaultRedis`'s. A supplied tier `tiers` does not name is logged (`cache.tier.unnamed`) and * never registered: the declaration decides the ladder. @@ -145,6 +152,10 @@ const lazyRedis = (url: string, client: (url: string) => RedisLike): RedisLike = send: (command, args) => client(url).send(command, args), }); +/** The boot's default lease, or nothing so the rung keeps its own. */ +const ttlOf = (options: CacheTiersOptions): { defaultTtlMs?: number } => + options.defaultTtlMs === undefined ? {} : { defaultTtlMs: options.defaultTtlMs }; + /** * One rung, or a refusal. A process that cannot build what it was configured to build must not * start — `assertRateLimitScope`'s rule (`@ultimat3/http`), applied to the ladder: a fleet reading @@ -160,7 +171,7 @@ function buildTier(name: CacheTierName, options: CacheTiersOptions): CacheTier { case 'request-memo': return memoTier(); case 'lru': - return lruTier(); + return lruTier(ttlOf(options)); case 'redis': { // `REDIS_URL` then `VALKEY_URL`, out of the boot's env: core's own order (`redisUrlSource`), // so the URL checked here is the URL the client dials. @@ -173,7 +184,10 @@ function buildTier(name: CacheTierName, options: CacheTiersOptions): CacheTier { fix: 'REDIS_URL=redis://localhost:6379 # in .env — or drop the redis tier from cache.tiers in app.config.ts', }); } - return redisTier({ client: lazyRedis(source.url, options.redisClient ?? redisClient) }); + return redisTier({ + client: lazyRedis(source.url, options.redisClient ?? redisClient), + ...ttlOf(options), + }); } case 'cdn': // A noop tier would put a `cdn` line in every invalidation report claiming keys an edge that diff --git a/packages/cli/src/runtime-services.ts b/packages/cli/src/runtime-services.ts index 82e0023f8..2bf8b659f 100644 --- a/packages/cli/src/runtime-services.ts +++ b/packages/cli/src/runtime-services.ts @@ -423,7 +423,14 @@ export async function startServices( // and one asking for a shared tier got whatever `REDIS_URL` happened to say. const tiers = cacheTiersOf(config); started.push( - startCacheTiers({ env, purge, transport, tiers, supplied: overrides?.cacheTiers }), + startCacheTiers({ + env, + purge, + transport, + tiers, + defaultTtlMs: config?.cache.defaultTtlMs, + supplied: overrides?.cacheTiers, + }), ); return { diff --git a/wiki/Configuration.md b/wiki/Configuration.md index 3c608ae2e..0543196f5 100644 --- a/wiki/Configuration.md +++ b/wiki/Configuration.md @@ -226,7 +226,7 @@ its config into a variable first. | field | type | default | notes | |---|---|---|---| -| `cache.defaultTtlMs` | `number` | `60000` | milliseconds, not a duration string | +| `cache.defaultTtlMs` | `number` | `60000` | milliseconds, not a duration string. The lease of a value set with no `ttlMs`, on the `lru` and `redis` rungs the boot builds; a rung supplied in `runtime.ts`'s `cacheTiers` keeps its own | | `cache.tiers` | `CacheTierName[]` | `['request-memo', 'lru']` | `'request-memo' \| 'lru' \| 'redis' \| 'cdn'`; order is fixed regardless of listing order, an EMPTY list is refused (`X_CONFIG_INVALID`), and a rung the environment cannot supply refuses the boot | | ~~`cache.driver`~~ | — | — | **Deleted in 9.0.0.** Refused at boot since 25.0.0 (`X_CONFIG_INVALID`, naming the replacement). It was the second way to ask for Redis and the losing one: the ladder is built from `cache.tiers`, so `driver: 'redis'` beside `tiers: ['request-memo', 'lru']` asked for a rung nothing built. Name `redis` in `tiers` | | ~~`cache.urlEnv`~~ | — | — | **Deleted in 9.0.0** and refused at boot since 25.0.0, `database.urlEnv`'s defect verbatim: the Redis tier reads the literal `REDIS_URL`, so `urlEnv: 'MY_REDIS'` made nothing read `MY_REDIS` |