Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 29 additions & 0 deletions packages/cli/src/runtime-cache-limits.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number>('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);
});
});
20 changes: 17 additions & 3 deletions packages/cli/src/runtime-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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
Expand Down
9 changes: 8 additions & 1 deletion packages/cli/src/runtime-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion wiki/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand Down
Loading