-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an option to control keep-alive connections
By default http module set keep-alive option in connections to make it persistent. However sometimes we need persistent connection and close it instead. It is possible with option keepalive_disable. Option accepts a map where key is a user agent name with assigned non-nil value. 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 untested. Example: local httpd = http_server.new('127.0.0.1', 8080, { log_requests = true, log_errors = true, keepalive_disable = { ['curl/7.68.0'] = true }, }) 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/ Fixes #137
- Loading branch information
Showing
4 changed files
with
100 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,75 @@ | ||
local t = require('luatest') | ||
local http_client = require('http.client') | ||
|
||
local helpers = require('test.helpers') | ||
|
||
local g = t.group() | ||
|
||
g.before_test('test_keepalive_default', function() | ||
g.httpd = helpers.cfgserv() | ||
g.httpd:start() | ||
end) | ||
|
||
g.before_test('test_keepalive_change_in_runtime', function() | ||
g.httpd = helpers.cfgserv() | ||
g.httpd:start() | ||
end) | ||
|
||
g.before_test('test_keepalive_allowed', function() | ||
g.httpd = helpers.cfgserv() | ||
g.httpd:start() | ||
end) | ||
|
||
g.before_test('test_keepalive_disallowed', function() | ||
local useragent = 'Mozilla/4.0' | ||
g.httpd = helpers.cfgserv({ | ||
keepalive_disable = { | ||
[useragent] = true, | ||
} | ||
}) | ||
g.httpd:start() | ||
end) | ||
|
||
g.after_each(function() | ||
helpers.teardown(g.httpd) | ||
end) | ||
|
||
g.test_keepalive_default = function(g) | ||
local httpd = g.httpd | ||
t.assert_equals(httpd.options.keepalive_disable, {}) | ||
end | ||
|
||
g.test_keepalive_allowed = function() | ||
local useragent = 'Mozilla/4.0' | ||
local r = http_client.request('GET', helpers.base_uri .. '/test', nil, { | ||
headers = { | ||
['user-agent'] = useragent, | ||
connection = 'keep-alive' | ||
}, | ||
}) | ||
t.assert_equals(r.status, 200) | ||
t.assert_equals(r.headers.connection, 'keep-alive') | ||
end | ||
|
||
g.test_keepalive_disallowed = function() | ||
local useragent = 'Mozilla/4.0' | ||
local r = http_client.request('GET', helpers.base_uri .. '/test', nil, { | ||
headers = { | ||
['user-agent'] = useragent, | ||
connection = 'keep-alive' | ||
}, | ||
}) | ||
t.assert_equals(r.status, 200) | ||
t.assert_equals(r.headers.connection, 'close') | ||
end | ||
|
||
g.test_keepalive_change_in_runtime = function(g) | ||
local httpd = g.httpd | ||
t.assert_equals(httpd.options.keepalive_disable, {}) | ||
httpd.options.keepalive_disable = { | ||
['Mozilla/4.0'] = true | ||
} | ||
t.assert_equals(httpd.options.keepalive_disable, { | ||
["Mozilla/4.0"] = true | ||
}) | ||
end |