Skip to content

Commit

Permalink
Introduce APICAST_LUA_SOCKET_KEEPALIVE_REQUESTS env var
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tkan145 committed Oct 18, 2024
1 parent 2e32a07 commit c9846c8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- 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)

- 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)
- 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

Expand Down
10 changes: 10 additions & 0 deletions doc/parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,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`

Expand Down
22 changes: 22 additions & 0 deletions gateway/src/resty/resolver/http.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 })

Expand Down Expand Up @@ -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

Check warning on line 95 in gateway/src/resty/resolver/http.lua

View check run for this annotation

Codecov / codecov/patch

gateway/src/resty/resolver/http.lua#L93-L95

Added lines #L93 - L95 were not covered by tests
end
if count >= tonumber(keepalive_request) then
resty_http.close(self)
return true

Check warning on line 99 in gateway/src/resty/resolver/http.lua

View check run for this annotation

Codecov / codecov/patch

gateway/src/resty/resolver/http.lua#L97-L99

Added lines #L97 - L99 were not covered by tests
end
end

local ok, err = resty_http.set_keepalive(self)
if not ok then
return nil, err

Check warning on line 105 in gateway/src/resty/resolver/http.lua

View check run for this annotation

Codecov / codecov/patch

gateway/src/resty/resolver/http.lua#L105

Added line #L105 was not covered by tests
end
return true
end

return _M

0 comments on commit c9846c8

Please sign in to comment.