Skip to main content
Version: Next

IConcurrencySystem

The contract between an AutoscaledPool and its concurrency "governor" — the object that answers is there free compute for one more task? and tracks the budget that tasks are booked against. ConcurrencySystem is the canonical implementation; the interface lets alternate governors be substituted without depending on its internals.

Every allocation method is told which consumer is asking, so an implementation can allocate per consumer. ConcurrencySystem does not: it serves whoever asks first, which can starve a pool that joins a saturated system late.

Implemented by

Index

Properties

readonlycurrentConcurrency

currentConcurrency: number

The number of parallel tasks currently booked against this governor, regardless of which pool booked them.

readonlydesiredConcurrency

desiredConcurrency: number

The number of tasks that should currently be running in parallel, assuming a sufficient supply of them. How it is derived is up to the implementation, hence read-only here — but it must always be at least 1, or a pool could never start the first task.

readonlyisRunning

isRunning: boolean

Whether the governor is ready to be booked against. pool.run() refuses to run when this is false. An implementation with no startup lifecycle simply reports true.

Methods

hasCapacityForTask

  • hasCapacityForTask(consumer): boolean
  • May one more task start right now, on behalf of consumer? A cheap pre-check the pool consults before querying task readiness.

    Must not enforce rate limits that only make sense for ready tasks (e.g. a per-minute task cap): the pool calls this before knowing whether any task is ready, so refusing here would stall an already-empty queue.

    Must also return true whenever consumer has nothing in flight of its own. A false sends that pool straight to its finished-check without consulting isTaskReadyFunction, so a governor that starves an idle pool can make its run() resolve while work is still pending. Tracking bookings per consumer answers that directly; ConcurrencySystem, which does not, instead never refuses while currentConcurrency is 0.


    Parameters

    Returns boolean

registerTaskEnd

  • registerTaskEnd(consumer): void
  • Returns a task's slot to consumer's budget. Called once the task settles (resolve or reject).


    Parameters

    Returns void

tryRegisterTaskStart

  • tryRegisterTaskStart(consumer): boolean
  • Books a task against the budget for consumer, returning false (without booking) when there is no room — the budget is spent, the consumer is over its share, or an implementation-specific rate limit was reached.

    Must be an atomic (synchronous) check-and-book: several pools may share one governor, and a check separated from the booking by an await lets two of them claim the last free slot at once.


    Parameters

    Returns boolean