Skip to content

Commit

Permalink
Add an option to control keep-alive connections
Browse files Browse the repository at this point in the history
By default http module set keep-alive option in connections to make it
persistent:

$ tarantool examples/middleware.lua
started
entering the event loop
HEAD /

...

$ curl -I  http://127.0.0.1:8080
HTTP/1.1 404 Not found
Content-length: 0
Server: Tarantool http (tarantool v1.10.6-0-g5372cd2fa)
Connection: keep-alive
$

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.

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

Fixes #137
  • Loading branch information
ligurio committed Oct 20, 2021
1 parent 7bc5c74 commit 5ea1b09
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added
- Add option to control keepalive (#137)

## [2.1.0] - 2020-01-30
### Added
- Return ability to set loggers for a specific route
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ server = require('http.server').new(host, port[, { options } ])
By default uses `log.info` function for requests logging.
* `log_errors` - same as the `log_requests` option but is used for error messages logging.
By default uses `log.error()` function.
* `keepalive_disable` - disables keep-alive connections with misbehaving
clients. Parameter accept a map that contains a user agents with non-nil
value. By default map is empty.

## Creating a router

Expand Down
18 changes: 18 additions & 0 deletions http/server/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ local function get_error_logger(server_opts, route_opts)
return log.debug
end

local function is_no_keepalive(disallowed_map, useragent)
if type(disallowed_map) ~= 'table' or useragent == nil then
return false
end
if disallowed_map[useragent] ~= nil then
return true
end

return false
end

----------
-- Server
----------
Expand Down Expand Up @@ -259,6 +270,12 @@ local function process_client(self, s, peer)
end
end

local no_keepalive = is_no_keepalive(self.options.keepalive_disable,
p.headers['user-agent'])
if no_keepalive == true then
hdrs.connection = 'close'
end

-- generate response {{{
local response = {
"HTTP/1.1 ";
Expand Down Expand Up @@ -379,6 +396,7 @@ local new = function(host, port, options)
log_requests = true,
log_errors = true,
display_errors = true,
keepalive_disable = {},
}

local self = {
Expand Down

0 comments on commit 5ea1b09

Please sign in to comment.