switch from "available" to "active" connections

This commit is contained in:
Simon Cruanes 2021-12-11 09:06:44 -05:00
parent c3265af08f
commit b2e571916d
No known key found for this signature in database
GPG key ID: 4AC01D0849AA62B6
2 changed files with 13 additions and 12 deletions

View file

@ -700,13 +700,14 @@ end
module Sem_ = struct module Sem_ = struct
type t = { type t = {
mutable n : int; mutable n : int;
max: int;
mutex : Mutex.t; mutex : Mutex.t;
cond : Condition.t; cond : Condition.t;
} }
let create n = let create n =
if n <= 0 then invalid_arg "Semaphore.create"; 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 = let acquire m t =
Mutex.lock t.mutex; Mutex.lock t.mutex;
@ -724,9 +725,7 @@ module Sem_ = struct
Condition.broadcast t.cond; Condition.broadcast t.cond;
Mutex.unlock t.mutex Mutex.unlock t.mutex
(* +1 because we decrease the semaphore before Unix.accept *) let num_acquired self = self.max - self.n
let available_connections t = t.n + 1
end end
module Route = struct module Route = struct
@ -871,8 +870,9 @@ type t = {
let addr self = self.addr let addr self = self.addr
let port self = self.port let port self = self.port
let available_connections self = let active_connections self =
Sem_.available_connections self.sem_max_connections (* -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_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 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; handle_client_ self client_sock;
Sem_.release 1 self.sem_max_connections; Sem_.release 1 self.sem_max_connections;
_debug (fun k -> k _debug (fun k -> k
"closing inactive connections (%d connections available)" "closing inactive connections (%d connections active)"
(Sem_.available_connections self.sem_max_connections)) (active_connections self))
with with
| e -> | e ->
(try Unix.close client_sock with _ -> ()); (try Unix.close client_sock with _ -> ());
Sem_.release 1 self.sem_max_connections; Sem_.release 1 self.sem_max_connections;
_debug (fun k -> k _debug (fun k -> k
"closing connections on error (%d connections available)" "closing connections on error (%d connections active)"
(Sem_.available_connections self.sem_max_connections)); (active_connections self));
raise e raise e
); );
with e -> with e ->

View file

@ -477,8 +477,9 @@ val is_ipv6 : t -> bool
val port : t -> int val port : t -> int
(** Port on which the server listens. *) (** Port on which the server listens. *)
val available_connections : t -> int val active_connections : t -> int
(** number of available connections on the server. *) (** number of currently opened connections with a client.
@since 0.11 *)
val add_decode_request_cb : val add_decode_request_cb :
t -> t ->