diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c12e882d..59890f3b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Added +- 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) + ## [3.15.0] 2024-04-04 ### Fixed diff --git a/doc/parameters.md b/doc/parameters.md index 362617b32..17fe8d628 100644 --- a/doc/parameters.md +++ b/doc/parameters.md @@ -478,6 +478,16 @@ connections. By default Gateway does not enable it, and the keepalive timeout on nginx is set to [75 seconds](http://nginx.org/en/docs/http/ngx_http_core_module.html#keepalive_timeout) +### `APICAST_LUA_SOCKET_KEEPALIVE_REQUESTS` + +**Value:** positive integers +**Example:** "1" + +Sets the maximum number of requests that one keepalive connection can serve. +After reaching the limit, the connection closes. + +NOTE: This value affects connections opened by APIcast and will not have any +impact on requests proxied via APIcast. ### `APICAST_CACHE_STATUS_CODES` diff --git a/gateway/src/resty/resolver/http.lua b/gateway/src/resty/resolver/http.lua index 4f646ffbc..82e6e2789 100644 --- a/gateway/src/resty/resolver/http.lua +++ b/gateway/src/resty/resolver/http.lua @@ -5,6 +5,9 @@ local url_helper = require('resty.url_helper') local format = string.format local setmetatable = setmetatable +local resty_env = require 'resty.env' +local tonumber = tonumber +local keepalive_request = resty_env.get('APICAST_LUA_SOCKET_KEEPALIVE_REQUESTS') local _M = setmetatable({}, { __index = resty_http }) @@ -85,4 +88,23 @@ function _M.connect(self, options, ...) return ok, err end +function _M:set_keepalive() + if keepalive_request then + local count, err = resty_http.get_reused_times(self) + if err then + return nil, err + end + if count >= tonumber(keepalive_request) then + resty_http.close(self) + return true + end + end + + local ok, err = resty_http.set_keepalive(self) + if not ok then + return nil, err + end + return true +end + return _M