Async Resource Pool With Acquire Release

A fixed pool recycles connections: acquire resolves immediately or parks the caller FIFO, and release hands the resource to the longest-waiting job.

JavascriptResilience

Code

javascriptsnippet
46 lines
// A fixed pool reuses expensive resources (connections, workers) instead of
// creating one per task. acquire() parks callers in FIFO order when empty.
class Pool {
  #free;
  #waiters = [];

  constructor(resources) {
    this.#free = [...resources];
    this.size = resources.length;
  }

  get available() {
    return this.#free.length;
  }

  get waiting() {
    return this.#waiters.length;
  }

  acquire() {
    if (this.#free.length > 0) return Promise.resolve(this.#free.pop());
    // Nothing free: resolve later, when release() hands a resource back.
    return new Promise((resolve) => this.#waiters.push(resolve));
  }

  release(resource) {
    const next = this.#waiters.shift();
    if (next) next(resource); // Straight to the longest-waiting caller.
    else this.#free.push(resource);
  }
}

let connectionId = 0;
const pool = new Pool(
  Array.from({ length: 2 }, () => ({ id: ++connectionId }))
);

async function job(n) {
  const connection = await pool.acquire();
  console.log(`job ${n} borrowed connection ${connection.id} (free=${pool.available}, waiting=${pool.waiting})`);
  await new Promise((resolve) => setTimeout(resolve, 20 * n));
  pool.release(connection);
}

await Promise.all([job(1), job(2), job(3), job(4), job(5)]);
console.log('done, free connections:', pool.available);

Output

Captured from a sandboxed run of this snippet in the build pipeline.

job 1 borrowed connection 2 (free=0, waiting=3)
job 2 borrowed connection 1 (free=0, waiting=3)
job 3 borrowed connection 2 (free=0, waiting=2)
job 4 borrowed connection 1 (free=0, waiting=1)
job 5 borrowed connection 2 (free=0, waiting=0)
done, free connections: 2

How it works

One way to read it: the class Pool and the function job, doing all the work.

The async parts await their results, which keeps ordering predictable.

What comes back is a new object, not a reference to anything passed in.

What to watch for

  • Zero-fetch snippet: every identifier resolves inside the language distribution.
  • The guard this.#free.length > 0 short-circuits the flow with an early return when its condition holds.

Putting it to use

The primary signature is async function job(n)

Look for the done, free connections: prefixes when the file runs; they tie each line to its step.

Core lines

return new Promise((resolve) => this.#waiters.push(resolve));
await new Promise((resolve) => setTimeout(resolve, 20 * n));
if (this.#free.length > 0) return Promise.resolve(this.#free.pop());

Interactive Tools for This Topic

FAQ

Does `job` modify its input?
No. The code builds and returns a new value and leaves the arguments untouched, so it is safe to call on shared or frozen data structures.
Does it depend on any third-party library?
No third-party library is referenced; the standard library covers it. The file defines `job` at load time.