Repository navigation
Conversation
Add a functional test showing that, at -rpcmaxconnections, idle keep-alive clients keep their slots: with -rpcservertimeout disabled, a new connection over the limit is never accepted while all slots are held by clients that have finished their requests.
When the server is at -rpcmaxconnections, new connections wait in the kernel's accept queue. A keep-alive connection keeps its slot after its request has been answered, until the client closes it or it reaches -rpcservertimeout (default 30 s). A client holding the maximum number of idle keep-alive connections therefore blocks all other clients (including bitcoin-cli) for up to 30 s, without any error. If a connection is waiting while the server is at the limit, disconnect the keep-alive client that has been idle the longest, so that the waiting connection can be accepted. Only clients whose requests have all been answered, that have not started a new request, and that have been idle for at least HTTP_MIN_IDLE_FOR_EVICTION (250 ms) are considered. At most one client is evicted per I/O loop iteration. At the limit, the listening sockets are only polled if a client could be evicted, so that a pending connection that can't be accepted does not turn the I/O loop into a busy loop. Tests: - Add a unit test for HTTPRemoteClient::IsEvictable(). - check_connection_limit_idle_clients(): the waiting connection is now served, and the client that has been idle the longest is evicted. - check_connection_limit(): its clients were idle keep-alive clients, which are now evicted. They now keep an unfinished request open, so they are busy and can't be evicted. This keeps checking that a connection over the limit waits when no client can be evicted, and that closing a connection frees its slot.
Owner
Author
|
Closing in favor of #31 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes TODO
Problem
Since the libevent replacement (bitcoin#35182), the HTTP server limits the number of connected clients to
-rpcmaxconnections(default 16, bitcoin#35730). At the limit, new connections are not accepted and wait in the kernel's accept queue.A keep-alive connection keeps its slot after its request has been answered. The slot is only freed when the client closes the connection, or after
-rpcservertimeout(default 30 s) of inactivity. So a client that holds the maximum number of idle keep-alive connections blocks every other RPC and REST client, includingbitcoin-cli, for up to 30 s, without any error or log message.This happens in practice with a client that keeps a pool of parallel keep-alive connections. The popular indexer
electrs, for example, fetches blocks over REST with one connection per CPU thread. On a 32-core machine, every batch of blocks it fetches while catching up stalls for about 30 s. Measured on master with 100 clients that each make one request and then keep their connection open (-rpcmaxconnections=64): 62 were answered at once, and the remaining 38 only after 31.1 s, when the idle connections hit-rpcservertimeout. With-rpcservertimeout=10the stall shrinks to 10–11 s. The libevent server had no connection limit, so this didn't happen before v32.Solution
If a connection is waiting while the server is at the limit, disconnect the keep-alive client that has been idle the longest, so that the waiting connection can be accepted:
HTTP_MIN_IDLE_FOR_EVICTION(250 ms). New connections that have not sent a request yet, and clients with a request in progress, are never evicted.httpdebug category.Behavior below the limit is unchanged, and so is the behavior at the limit when no client is idle.
An evicted client may be just about to send its next request, and then sees the connection closed. That is the same race keep-alive clients already face with the idle timeout. The minimum idle time makes it unlikely.
Alternatives considered
-rpcmaxconnections: this only moves the threshold, and costs file descriptors, which is why the default was lowered from 128 to 16 in http: limit connected HTTPRemoteClients bitcoin/bitcoin#35730.Connection: closein replies while connections are waiting: this avoids the race above, but doesn't help with clients that went idle before the other connections arrived, so idle connections would still need to be evicted. The trigger is the waiting connection, so the eviction is done there.503: the client gets an error instead of a delay, while idle slots stay blocked.Tests
-rpcservertimeout=0, a new connection is never accepted. The second commit changes it to the new expectation: the client that has been idle the longest is evicted, and the new connection is served.HTTPRemoteClient::IsEvictable(), including the minimum idle time (with a mocked clock).check_connection_limit()filled all slots with idle keep-alive clients, which would now be evicted. Its clients now keep an unfinished request open, so they are busy, and the test still checks that a connection over the limit waits when no client can be evicted.Related: bitcoin#36204 addresses a different way of holding a slot: clients that never finish sending a request.