Skip to content

bugfix: balancer keepalive connections were never reused with proxy_bind - #2533

Merged
zhuizhuhaomeng merged 1 commit into
openresty:masterfrom
setnicka:balancer-keepalive-proxy-bind
Oct 19, 2026
Merged

zhuizhuhaomeng merged 1 commit into
openresty:masterfrom
setnicka:balancer-keepalive-proxy-bind

Conversation

@setnicka

Copy link
Copy Markdown
Contributor

With proxy_bind set and balancer.enable_keepalive() used without balancer.bind_to_local_addr(), connections are saved to the Lua keepalive pool but never reused.

Since 6738c3a the pool key includes the local address. free_peer() hashed it from bp->local (NULL here), while get_cached_item() uses pc->local (the proxy_bind address), so the lookup never matched.

The fix hashes with pc->local, which is what connect() bound and what the item stores. Without proxy_bind or with bind_to_local_addr(), pc->local equals bp->local, so those cases behave as before.

I also added a test that alternates two proxy_bind addresses and expects each to reuse its own connection. If fails before the fix, passes with the fix.


I hereby granted the copyright of the changes in this pull request
to the authors of this lua-nginx-module project.

…ind.

When proxy_bind sets the local address and balancer_by_lua* enables
keepalive without calling balancer.bind_to_local_addr(), pc->local holds
the proxy_bind address while bp->local is NULL. free_peer hashed the
saved connection with bp->local, but get_cached_item looks it up with
pc->local, so the lookup never matched: every connection was saved,
never reused, and closed when it idled out.

Hash with pc->local, which is the address the connection was bound to
and the one stored in item->local_sockaddr.

Since nginx 1.29.7 the "keepalive" directive is enabled by default and
its cache takes the connections before the Lua pool, so the test turns
it off with "keepalive 0" on those versions.
@zhuizhuhaomeng
zhuizhuhaomeng merged commit ef9ac4d into openresty:master Oct 19, 2026
5 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.

2 participants