Skip to content

Commit c9846c8

Browse files
committed
Introduce APICAST_LUA_SOCKET_KEEPALIVE_REQUESTS env var
Under highload, APIcast keepalive connection could cause unbalance traffic to backend-listenr. This PR add a new environment variable to limit the number of request a single keepalive connection can handle. Once the limit is reached APIcast will close the connection and open a new one.
1 parent 2e32a07 commit c9846c8

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
3333
- Added the `APICAST_PROXY_BUFFER_SIZE` variable to allow configuration of the buffer size for handling response from the proxied servers. [PR #1473](https://github.com/3scale/APIcast/pull/1473), [THREESCALE-8410](https://issues.redhat.com/browse/THREESCALE-8410)
3434

3535
- Added the `APICAST_HTTPS_VERIFY_CLIENT` variable to allow configuration of the `ssl_verify_client` directive. [PR #1491](https://github.com/3scale/APIcast/pull/1491) [THREESCALE-10156](https://issues.redhat.com/browse/THREESCALE-10156)
36+
- Add `APICAST_LUA_SOCKET_KEEPALIVE_REQUESTS` to limit the number of requests a single keepalive socket can handle [PR #1496](https://github.com/3scale/APIcast/pull/1496) [THREESCALE-11321](https://issues.redhat.com/browse/THREESCALE-11321)
3637

3738
## [3.15.0] 2024-04-04
3839

doc/parameters.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,16 @@ connections.
487487
By default Gateway does not enable it, and the keepalive timeout on nginx is set
488488
to [75 seconds](http://nginx.org/en/docs/http/ngx_http_core_module.html#keepalive_timeout)
489489

490+
### `APICAST_LUA_SOCKET_KEEPALIVE_REQUESTS`
491+
492+
**Value:** positive integers
493+
**Example:** "1"
494+
495+
Sets the maximum number of requests that one keepalive connection can serve.
496+
After reaching the limit, the connection closes.
497+
498+
NOTE: This value affects connections opened by APIcast and will not have any
499+
impact on requests proxied via APIcast.
490500

491501
### `APICAST_CACHE_STATUS_CODES`
492502

gateway/src/resty/resolver/http.lua

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ local url_helper = require('resty.url_helper')
55
local format = string.format
66

77
local setmetatable = setmetatable
8+
local resty_env = require 'resty.env'
9+
local tonumber = tonumber
10+
local keepalive_request = resty_env.get('APICAST_LUA_SOCKET_KEEPALIVE_REQUESTS')
811

912
local _M = setmetatable({}, { __index = resty_http })
1013

@@ -85,4 +88,23 @@ function _M.connect(self, options, ...)
8588
return ok, err
8689
end
8790

91+
function _M:set_keepalive()
92+
if keepalive_request then
93+
local count, err = resty_http.get_reused_times(self)
94+
if err then
95+
return nil, err
96+
end
97+
if count >= tonumber(keepalive_request) then
98+
resty_http.close(self)
99+
return true
100+
end
101+
end
102+
103+
local ok, err = resty_http.set_keepalive(self)
104+
if not ok then
105+
return nil, err
106+
end
107+
return true
108+
end
109+
88110
return _M

0 commit comments

Comments
 (0)