From b2e571916d6da50af5cfba3a654a2cc9ea06f582 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Sat, 11 Dec 2021 09:06:44 -0500 Subject: [PATCH] switch from "available" to "active" connections --- src/Tiny_httpd.ml | 20 ++++++++++---------- src/Tiny_httpd.mli | 5 +++-- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/Tiny_httpd.ml b/src/Tiny_httpd.ml index f6cc707b..d4e33c59 100644 --- a/src/Tiny_httpd.ml +++ b/src/Tiny_httpd.ml @@ -700,13 +700,14 @@ end module Sem_ = struct type t = { mutable n : int; + max: int; mutex : Mutex.t; cond : Condition.t; } let create n = if n <= 0 then invalid_arg "Semaphore.create"; - { n; mutex=Mutex.create(); cond=Condition.create(); } + { n; max=n; mutex=Mutex.create(); cond=Condition.create(); } let acquire m t = Mutex.lock t.mutex; @@ -724,9 +725,7 @@ module Sem_ = struct Condition.broadcast t.cond; Mutex.unlock t.mutex - (* +1 because we decrease the semaphore before Unix.accept *) - let available_connections t = t.n + 1 - + let num_acquired self = self.max - self.n end module Route = struct @@ -871,8 +870,9 @@ type t = { let addr self = self.addr let port self = self.port -let available_connections self = - Sem_.available_connections self.sem_max_connections +let active_connections self = + (* -1 because we decrease the semaphore before Unix.accept *) + Sem_.num_acquired self.sem_max_connections - 1 let add_decode_request_cb self f = self.cb_decode_req <- f :: self.cb_decode_req let add_encode_response_cb self f = self.cb_encode_resp <- f :: self.cb_encode_resp @@ -1132,15 +1132,15 @@ let run (self:t) : (unit,_) result = handle_client_ self client_sock; Sem_.release 1 self.sem_max_connections; _debug (fun k -> k - "closing inactive connections (%d connections available)" - (Sem_.available_connections self.sem_max_connections)) + "closing inactive connections (%d connections active)" + (active_connections self)) with | e -> (try Unix.close client_sock with _ -> ()); Sem_.release 1 self.sem_max_connections; _debug (fun k -> k - "closing connections on error (%d connections available)" - (Sem_.available_connections self.sem_max_connections)); + "closing connections on error (%d connections active)" + (active_connections self)); raise e ); with e -> diff --git a/src/Tiny_httpd.mli b/src/Tiny_httpd.mli index 1b55a15a..1e7069bf 100644 --- a/src/Tiny_httpd.mli +++ b/src/Tiny_httpd.mli @@ -477,8 +477,9 @@ val is_ipv6 : t -> bool val port : t -> int (** Port on which the server listens. *) -val available_connections : t -> int -(** number of available connections on the server. *) +val active_connections : t -> int +(** number of currently opened connections with a client. + @since 0.11 *) val add_decode_request_cb : t ->