Repository navigation
Conversation
… freed in the same cycle. When a connection pool with a backlog frees a slot, resume_conn_op() posts the event of the operation at the head of wait_connect_op. That operation only leaves the queue when its event runs, so when two slots are freed in the same cycle, both calls post the same event and ngx_post_event() ignores the second one. The second slot is handed to no waiter, but the pool still counts it as busy, so new connects queue behind it too. Each lost resumption shrinks the pool until the queue drains, and under a burst, queued connects time out while slots are free. Resume the first queued operation whose event is not posted yet, and let the resume handler remove its own operation instead of the queue head. As several operations can now be posted at once, also delete a posted event before freeing its operation when the pool is destroyed.
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.
I hereby granted the copyright of the changes in this pull request
to the authors of this lua-nginx-module project.
With a connection pool
backlog, a queuedconnect()can wait until it times out while the pool has free slots.ngx_http_lua_socket_tcp_resume_conn_op()runs each time a slot is freed and posts the event of the operation at the head ofwait_connect_op. That operation stays in the queue until its event runs, wherengx_http_lua_socket_tcp_conn_op_resume_handler()removes the queue head. When two slots are freed in the same cycle, for example twosetkeepalive()calls in a row, both calls post the same event andngx_post_event()ignores the second one. The second slot is handed to no waiter. Sincespool->connectionscounts queued operations too, the pool still counts that slot as busy, so newconnect()calls queue behind it instead of taking it. Each lost resumption shrinks the pool until the queue drains.The smallest case:
pool_size = 2, backlog = 2, two connected sockets, two waiters queued with a 500ms connect timeout, then both sockets released in the same cycle:Under load this is not a corner case. With 9 workers,
pool_size = 100andbacklog = 500, and a burst of 300 concurrent requests that each run 7 concurrent cosocket queries against a local backend over a unix domain socket, the queries of one request failed in every run after the full connect timeout, about 1s with a 1s timeout and 3.45s with a 3s timeout, while the other requests went through. With a warm pool, or without a backlog, there were no failures.This change resumes the first queued operation whose event is not posted yet, so every freed slot resumes its own waiter, still in queue order, and the resume handler removes its own operation instead of the queue head. As several operations can now be posted at the same time,
ngx_http_lua_socket_shutdown_pool_helper()also deletes a posted event before it frees the operation; before, only the head could be posted.TEST 56 in
t/068-socket-keepalive.tis the case above and expects both waiters to connect. The first waiter holds its connection for 1s, longer than the second waiter's connect timeout, so the second one can only connect if it is resumed. Without the fix it fails on every run withw2: timeout; with it, it passes over HTTP/1.1 and HTTP/2. The other tests in the file fail or pass the same with and without the change in my environment; the failures come from my local setup (some tests need services it does not have, and127.0.0.2:12345is refused at once instead of timing out). Tested against nginx 1.31.1 in an OpenResty build; the changed functions and the test file are the same as on master.The same code is in stream-lua-nginx-module: openresty/stream-lua-nginx-module#415
Contributed by AxisNow.