Skip to content

. - #1104

Closed
mgraczyk wants to merge 1 commit into
encode:masterfrom
mgraczyk:pool-assign-linear-upstream
Closed

.#1104
mgraczyk wants to merge 1 commit into
encode:masterfrom
mgraczyk:pool-assign-linear-upstream

Conversation

@mgraczyk

@mgraczyk mgraczyk commented Aug 15, 2026

Copy link
Copy Markdown

No description provided.

_assign_requests_to_connections rebuilt the list of available connections
and the list of idle connections from scratch for every queued request, so
one pass cost O(queued requests x open connections). A pass runs on every
request arrival and every response close while holding the pool lock, so
a saturated pool (all connections busy, many requests waiting) spent most
of its time re-scanning: with 1,000 connections and 1,000-2,000 queued
requests a single pass took 0.5-1.0 seconds of pure Python.

Which connections are available or idle does not depend on the request
being assigned, and assigning a request does not change any connection's
state, so the per-request scans always produced the same lists apart from
the connections the loop itself created or closed. Compute both lists once
per pass, filter the available list by origin for each request, keep both
lists up to date as connections are created and closed, and stop early
once the pool is full with nothing idle or available. Assignment and close
decisions are identical to before.

Median time per pass with 1,000 open connections (CPython 3.13):

  queued requests    before      after
              100     49 ms     0.8 ms
            1,000    497 ms     1.2 ms
            2,000  1,001 ms     1.0 ms
@mgraczyk mgraczyk closed this by deleting the head repository Aug 15, 2026
@mgraczyk mgraczyk changed the title Scan connections once per pool assignment pass . Aug 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant