Report connection counts

This commit is contained in:
Tony Garnock-Jones 2014-05-05 12:20:07 -04:00
parent 7fd69aa072
commit ffc9835ad6
2 changed files with 17 additions and 2 deletions

View File

@ -4,7 +4,10 @@
start() ->
Port = 5999,
CounterPid = spawn(fun () -> counter(0) end),
CounterPid = spawn(fun () ->
timer:send_interval(2000, report_stats),
counter(0)
end),
{ok, LSock} = gen_tcp:listen(Port, [{active, true}, {packet, line}, {reuseaddr, true}]),
io:format("Erlang echo server running on port ~p.~n", [Port]),
accept_loop(LSock, CounterPid).
@ -20,7 +23,10 @@ counter(Count) ->
init:stop();
NewCount ->
counter(NewCount)
end
end;
report_stats ->
io:format("~p connections~n", [Count]),
counter(Count)
end.
accept_loop(LSock, CounterPid) ->

View File

@ -7,6 +7,10 @@
static int connection_count = 0;
static void on_timer(uv_timer_t *timer, int status) {
printf("%d connections\n", connection_count);
}
static uv_buf_t alloc_buffer(uv_handle_t *handle, size_t suggested_size) {
return uv_buf_init(calloc(1, suggested_size), suggested_size);
}
@ -23,6 +27,7 @@ static void on_input(uv_stream_t *conn, ssize_t nread, uv_buf_t buf) {
/* but we don't care; just close, and be done */
connection_count--;
if (connection_count == 0) {
printf("Exiting on zero connection count.\n");
uv_stop(conn->loop);
}
uv_close((uv_handle_t *) conn, NULL);
@ -58,6 +63,7 @@ static int const PORTNUMBER = 5999;
int main(int argc, char const *argv[]) {
uv_loop_t *uv = uv_default_loop();
uv_tcp_t serversock;
uv_timer_t stats;
struct sockaddr_in bind_addr;
printf("uvserver; libuv version %s\n", uv_version_string());
@ -72,6 +78,9 @@ int main(int argc, char const *argv[]) {
uv_tcp_bind(&serversock, bind_addr);
uv_listen((uv_stream_t *) &serversock, 4, on_connection);
uv_timer_init(uv, &stats);
uv_timer_start(&stats, on_timer, 2000, 2000);
uv_run(uv, UV_RUN_DEFAULT);
uv_loop_delete(uv);