Skip to content

feature: ngx.balancer: added the pool_size argument to enable_keepalive(). - #544

Open
oowl wants to merge 2 commits into
openresty:masterfrom
oowl:feat/balancer-keepalive-pool-size
Open

oowl wants to merge 2 commits into
openresty:masterfrom
oowl:feat/balancer-keepalive-pool-size

Conversation

@oowl

@oowl oowl commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Related PRs

PR What it does
openresty/lua-nginx-module#2536 C side: pool_size in the balancer keepalive FFI API, per-pool descriptors
#544 Lua side: balancer.enable_keepalive(idle_timeout?, max_requests?, pool_size?)
openresty/test-nginx#190 Fixes the ASan CI hang in HUP tests on Ubuntu 22.04. Independent of the feature. Merged
openresty/lua-nginx-module#2540 Bumps ngx_http_lua_version to 10033
#547 Requires ngx_lua 10033, bumps lua-resty-core to 0.1.36

Suggested merge order:

  1. bugfix: send the HUP mode signals from perl instead of running kill(1). test-nginx#190, then drop the TEMPORARY CI commit from feature: added the pool_size argument to the balancer keepalive FFI API. lua-nginx-module#2536. Done: tests: the trace id number maybe two or more digits. #190 is merged and #2536 no longer carries a CI commit.
  2. feature: added the pool_size argument to the balancer keepalive FFI API. lua-nginx-module#2536 and feature: ngx.balancer: added the pool_size argument to enable_keepalive(). #544 (the FFI signature changes), together with the version bumps change: bumped ngx_http_lua_version to 10033. lua-nginx-module#2540 and change: require ngx_http_lua_module 0.10.33 and bumped version to 0.1.36. #547, so a mismatched pair fails at load time. Drop the TEMPORARY CI commits of #2540 and change: require ngx_http_lua_module 0.10.33 and bumped version to 0.1.36. #547 when merging. Drop the TEMPORARY CI commit from feature: ngx.balancer: added the pool_size argument to enable_keepalive(). #544 once #2536 is on master.

Summary

Adds an optional third argument pool_size to ngx.balancer.enable_keepalive():

balancer.enable_keepalive(idle_timeout?, max_requests?, pool_size?)

pool_size sets the maximum number of idle connections cached for the pool of the current connection. A pool is identified by the peer address, the host argument of set_current_peer() and the local address. When a pool is full, its own least recently used idle connection is closed. Without this, a busy pool evicts the idle connections of other pools from the upstream-wide balancer_keepalive cache. Omitting the argument or passing 0 keeps the current behaviour. pool_size never raises the balancer_keepalive total.

Depends on openresty/lua-nginx-module#2536, which changes the FFI signature of ngx_http_lua_ffi_balancer_enable_keepalive(). The two PRs should be merged together.

Docs

  • enable_keepalive used to say the connection goes into "the pool specified by the pool option of set_current_peer". That option does not exist (it is a leftover of the original design). The text now describes the actual pool key: peer address, host (or proxy_ssl_name for HTTPS when host is nil), and local address. It also explains that all pools share the balancer_keepalive capacity.
  • pool_size: the limit is checked against the value passed by the request whose connection is being inserted, so every request using the same pool should pass the same pool_size.

Tests

New in t/balancer-keepalive.t:

  • TEST 29: argument validation for pool_size
  • TEST 30: pool_size = 1 makes a pool evict its own oldest idle connection
  • TEST 31: with balancer_keepalive 2, a full pool does not evict another pool's idle connection. This test fails if pool_size is 0.
  • TEST 32: explicit pool_size = 0 behaves exactly like omitting it, i.e. the global LRU evicts the other pool's connection.
  • TEST 33: pool_size also applies to pools split by SNI (host nil, name from proxy_ssl_name), and each request still gets its own SNI.

New in t/balancer-keepalive-localaddr.t:

  • TEST 8: pool_size applies per local address (bind_to_local_addr). A full pool for one source address does not evict the other source address's connection.

TEST 33 fails if the C side ignores the host in the pool key, and localaddr TEST 8 fails if it ignores the local address (checked with deliberately broken builds).

Ran locally against nginx 1.31.8 + the lua-nginx-module branch, both as a normal --with-debug build and as an ASan build: t/balancer*.t and t/stream/balancer*.t all pass with no ASan reports. t/balancer-keepalive.t passed 20 runs in a row. Line length and tab checks pass.

Temporary CI commit

The last commit (tests: TEMPORARY: ...) points the CI build at the PR branch of openresty/lua-nginx-module#2536 (oowl/lua-nginx-module@feat/balancer-keepalive-pool-size), so the FFI signature matches. It must be dropped before merging, once #2536 is merged, so CI goes back to lua-nginx-module master.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

oowl added 2 commits October 26, 2026 00:42
…ve().

    balancer.enable_keepalive(idle_timeout?, max_requests?, pool_size?)

pool_size caps the number of idle connections cached for the pool of the
current connection (peer address, host given to set_current_peer() and
local address). When the pool is full, its own least recently used idle
connection is closed instead of evicting idle connections of other pools
from the upstream-wide balancer_keepalive cache. Omitting it, or passing
0, keeps the previous behaviour.

This requires the matching FFI signature change in lua-nginx-module.
The C side of the pool_size argument lives in
openresty/lua-nginx-module#2536 and is not on master yet, so CI must be
built against that branch for the FFI signature to match. Drop this
commit before merging, once #2536 is merged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants