Files
simstudioai--sim/apps/sim/lib/concurrency/singleflight.ts
T
wehub-resource-sync d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

72 lines
2.3 KiB
TypeScript

const inflight = new Map<string, Promise<unknown>>()
/**
* Default deadline for a coalesced producer to settle. Joiners share the
* producer's promise, so without a deadline a single hung producer wedges
* every future caller for that key until process restart.
*/
const DEFAULT_SETTLE_TIMEOUT_MS = 30_000
/**
* Thrown to all awaiters when a coalesced producer fails to settle within
* its deadline. The entry is evicted first, so the next caller mints a
* fresh producer instead of joining the wedged one.
*/
export class CoalesceSettleTimeoutError extends Error {
constructor(key: string, timeoutMs: number) {
super(`Coalesced producer for "${key}" did not settle within ${timeoutMs}ms`)
this.name = 'CoalesceSettleTimeoutError'
}
}
/**
* Deduplicates concurrent async work by key within this process: the first
* caller runs `fn`, every concurrent caller for the same key shares its
* promise. The entry is evicted when the producer settles (either way) or
* when the settle deadline fires, whichever comes first. The underlying
* `fn` is not cancelled on timeout — it keeps running detached, but no new
* caller will join it.
*/
export function coalesceLocally<T>(
key: string,
fn: () => Promise<T>,
settleTimeoutMs: number = DEFAULT_SETTLE_TIMEOUT_MS
): Promise<T> {
const existing = inflight.get(key) as Promise<T> | undefined
if (existing) return existing
let timer: ReturnType<typeof setTimeout> | undefined
const evict = () => {
if (inflight.get(key) === guarded) inflight.delete(key)
}
const guarded: Promise<T> = Promise.race([
(async () => {
try {
// Defer fn() to a microtask so a synchronous throw surfaces as a
// rejection after `guarded` and the timer are initialized. Calling it
// inline would run the finally below during construction, touching
// `guarded` in its temporal dead zone and masking fn's real error.
return await Promise.resolve().then(fn)
} finally {
clearTimeout(timer)
evict()
}
})(),
new Promise<never>((_, reject) => {
timer = setTimeout(() => {
evict()
reject(new CoalesceSettleTimeoutError(key, settleTimeoutMs))
}, settleTimeoutMs)
timer.unref?.()
}),
])
inflight.set(key, guarded)
return guarded
}
export function __resetCoalesceLocallyForTests(): void {
inflight.clear()
}