Skip to content

withKeyPool

function withKeyPool(pool, factory): Provider;

Defined in: core/src/key-pool.ts:245

Give any provider a rotating pool of keys.

The subtlety is the seam’s shape. createStream is an async generator, so calling it performs no I/O. The POST and the 429 that should rotate the key happen on the first next(), long after a pool.with wrapped around the call itself would have returned, with nothing left to rotate. So the first chunk is pulled INSIDE the pool and only the rest of the stream is consumed outside it.

That line is also the honest one. Past the first chunk the answer is committed to one key, exactly as a retry is committed past its first chunk (retry.ts, rule 2): a mid-stream 429 evicts nothing and rotates nothing, because there is no way to resume a half-rendered answer on another key.

The factory runs once per attempt. An adapter with a session header mints its id when it is built, so give the factory an instanceSessionId, or each attempt opens a new session on a gateway that pins sessions to one host.

Parameter Type
pool KeyPool
factory (apiKey) => Provider

Provider