Skip to content

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

Merged
zhuizhuhaomeng merged 1 commit into
openresty:masterfrom
oowl:feat/balancer-keepalive-pool-size
Oct 31, 2026
Merged

zhuizhuhaomeng merged 1 commit 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. Closed, not merged
#547 Requires ngx_lua 10033, bumps lua-resty-core to 0.1.36. Closed, not merged

Integration status:


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.

CI

The temporary CI commit has been removed. CI now clones upstream openresty/lua-nginx-module master again. The feature and tests are unchanged.

The C-side dependency openresty/lua-nginx-module#2536 is still open. Until it lands on master, the upstream C function has the old FFI signature, so this CI configuration is expected to fail. Previous successful CI runs used the matching #2536 branch.

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.

…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.
@zhuizhuhaomeng
zhuizhuhaomeng merged commit 16747a5 into openresty:master Oct 31, 2026
2 of 3 checks passed
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.

3 participants