Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add server:connections() #173

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/modules/http.server.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ This method is used for integrating with other main loops, and should be used in
Returns `true` if the master socket and all client connection have been closed, `false` otherwise.


### `server:connections()` <!-- --> {#http.server:connections}

Returns the number of clients currently connected to this server.


### `server:step(timeout)` <!-- --> {#http.server:step}

Step once through server's main loop: any waiting clients will be `accept()`-ed, any pending streams will start getting processed, and each `onstream` handler will get be run at most once. This method will block for *up to* `timeout` seconds. On error, returns `nil`, an error message and an error number.
Expand Down
4 changes: 4 additions & 0 deletions http/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,10 @@ function server_methods:loop(...)
return self.cq:loop(...)
end

function server_methods:connections()
return self.n_connections
end

function server_methods:add_socket(socket)
self.n_connections = self.n_connections + 1
self.cq:wrap(handle_socket, self, socket)
Expand Down
3 changes: 3 additions & 0 deletions spec/server_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,14 @@ describe("http.server module", function()
options.port = 0
end
local onstream = spy.new(function(s, stream)
assert.is_equal(1, s:connections())
stream:get_headers()
stream:shutdown()
s:close()
end)
options.onstream = onstream
local s = assert(http_server.listen(options))
assert.is_equal(0, s:connections())
assert(s:listen())
cq:wrap(function()
assert_loop(s)
Expand Down Expand Up @@ -125,6 +127,7 @@ describe("http.server module", function()
assert_loop(cq, TEST_TIMEOUT)
assert.truthy(cq:empty())
assert.spy(onstream).was.called()
assert.is_equal(0, s:connections())
end
it("works with plain http 1.1 using IP", function()
simple_test(cs.AF_INET, false, 1.1)
Expand Down