Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
By default HTTP server sets a keep-alive option in connections to make it persistent. However sometimes keeping connections alive is not required and user may want to disable it for a certain connections. Patch adds a new option 'disable_keepalive' that accepts a table with user agent names. HTTP server will disable keep-alive for connections established by user agents listed in a table. Note: nginx web-server has a similar option, see [1]. Unfortunately HTTP client [1] builtin into Tarantool supports only HTTP protocol version 1.1, so version 1.0 is not covered in regression tests. Example: local httpd = http_server.new('127.0.0.1', 8080, { disable_keepalive = { 'curl/7.68.0' }, }) 1. https://nginx.org/en/docs/http/ngx_http_core_module.html#keepalive_disable 2. https://www.tarantool.io/en/doc/latest/reference/reference_lua/http/ Part of #137 Reviewed-by: Alexander Turenko <alexander.turenko@tarantool.org>
- Loading branch information
Showing
4 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
local t = require('luatest') | ||
local http_client = require('http.client') | ||
|
||
local helpers = require('test.helpers') | ||
|
||
local g = t.group() | ||
|
||
g.after_each(function() | ||
helpers.teardown(g.httpd) | ||
end) | ||
|
||
g.before_test('test_keepalive_is_allowed', function() | ||
g.httpd = helpers.cfgserv() | ||
g.httpd:start() | ||
end) | ||
|
||
g.test_keepalive_is_allowed = function() | ||
local conn_is_opened = false | ||
local conn_is_closed = false | ||
g.httpd.internal.preprocess_client_handler = function() conn_is_opened = true end | ||
g.httpd.internal.postprocess_client_handler = function() conn_is_closed = true end | ||
|
||
-- Set HTTP keepalive headers: Connection:Keep-Alive and | ||
-- Keep-Alive:timeout=<keepalive_idle>. Otherwise HTTP client will send | ||
-- "Connection:close". | ||
local opts = { | ||
keepalive_idle = 3600, | ||
keepalive_interval = 3600, | ||
} | ||
local r = http_client.new():request('GET', helpers.base_uri .. '/test', nil, opts) | ||
t.assert_equals(r.status, 200) | ||
t.assert_equals(r.headers.connection, 'keep-alive') | ||
t.assert_equals(conn_is_opened, true) | ||
t.assert_equals(conn_is_closed, false) -- Connection is alive. | ||
end | ||
|
||
g.before_test('test_keepalive_is_disallowed', function() | ||
g.useragent = 'Mozilla/4.0' | ||
g.httpd = helpers.cfgserv({ | ||
disable_keepalive = { g.useragent }, | ||
}) | ||
g.httpd:start() | ||
end) | ||
|
||
g.test_keepalive_is_disallowed = function() | ||
local conn_is_opened = false | ||
local conn_is_closed = false | ||
g.httpd.internal.preprocess_client_handler = function() conn_is_opened = true end | ||
g.httpd.internal.postprocess_client_handler = function() conn_is_closed = true end | ||
|
||
-- Set HTTP keepalive headers: Connection:Keep-Alive and | ||
-- Keep-Alive:timeout=<keepalive_idle>. Otherwise HTTP client will send | ||
-- "Connection:close". | ||
local opts = { | ||
headers = { | ||
['user-agent'] = g.useragent, | ||
}, | ||
keepalive_idle = 3600, | ||
keepalive_interval = 3600, | ||
} | ||
local r = http_client.new():request('GET', helpers.base_uri .. '/test', nil, opts) | ||
t.assert_equals(r.status, 200) | ||
t.assert_equals(r.headers.connection, 'close') | ||
t.assert_equals(conn_is_opened, true) | ||
t.assert_equals(conn_is_closed, true) -- Connection is closed. | ||
end | ||
|
||
g.before_test('test_disable_keepalive_is_set', function() | ||
g.useragent = 'Mozilla/5.0' | ||
g.httpd = helpers.cfgserv({ | ||
disable_keepalive = { g.useragent }, | ||
}) | ||
g.httpd:start() | ||
end) | ||
|
||
g.test_disable_keepalive_is_set = function(g) | ||
local httpd = g.httpd | ||
t.assert_equals(httpd.disable_keepalive[g.useragent], true) | ||
end | ||
|
||
g.before_test('test_disable_keepalive_default', function() | ||
g.httpd = helpers.cfgserv() | ||
g.httpd:start() | ||
end) | ||
|
||
g.test_disable_keepalive_default = function(g) | ||
local httpd = g.httpd | ||
t.assert_equals(table.getn(httpd.options.disable_keepalive), 0) | ||
end |