From ffc9835ad632d1484c4bf1425b2e6e22b9a2bc51 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Mon, 5 May 2014 12:20:07 -0400 Subject: [PATCH] Report connection counts --- echoserver.erl | 10 ++++++++-- uvserver.c | 9 +++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/echoserver.erl b/echoserver.erl index 45b0b3c..a9efc05 100644 --- a/echoserver.erl +++ b/echoserver.erl @@ -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) -> diff --git a/uvserver.c b/uvserver.c index 832c127..536deda 100644 --- a/uvserver.c +++ b/uvserver.c @@ -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);