Skip to content

Commit d337206

Browse files
committed
feature: added the pool_size argument to the balancer keepalive FFI API.
The balancer keepalive cache has a single capacity per upstream {} block, set by balancer_keepalive (32 by default), and evicts the least recently used idle connection regardless of which pool it belongs to. When many pools share one upstream, as is typical when peers are picked dynamically in balancer_by_lua*, a burst to one peer can push out the idle connections of every other peer. ngx_http_lua_ffi_balancer_enable_keepalive() now takes a pool_size argument. A pool is the same key the cache already matches on for reuse: the peer address, the host given to set_current_peer() and the local address. When a connection is about to be cached and its pool already holds pool_size idle connections, the least recently used idle connection of that same pool is closed and its slot reused, instead of taking a slot from the global LRU. pool_size 0 keeps the previous behaviour, and pool_size never raises the balancer_keepalive total. To keep this O(1), the hash buckets now hold one descriptor per pool instead of the cached connections themselves. A descriptor keeps the pool's idle connections (most recently used first) and their count, so reusing a connection, checking whether a pool is full and evicting its oldest connection no longer scan the other cached connections. Scanning the bucket costs about 10ns per idle connection of the pool, i.e. ~5us per released connection with a full pool of 512. Descriptors are preallocated next to the cache items, one per item, as a pool only exists while it holds idle connections. The key comparison used by the reuse path is factored out into ngx_http_lua_balancer_item_match(). This changes the FFI signature, lua-resty-core needs to be updated accordingly.
1 parent d9d95ab commit d337206

3 files changed

Lines changed: 923 additions & 62 deletions

File tree

0 commit comments

Comments
 (0)