diff --git a/src/Tiny_httpd.ml b/src/Tiny_httpd.ml index 89b211d1..4ba154cd 100644 --- a/src/Tiny_httpd.ml +++ b/src/Tiny_httpd.ml @@ -812,6 +812,8 @@ type t = { sock: Unix.file_descr option; + timeout: float; + sem_max_connections: Sem_.t; (* semaphore to restrict the number of active concurrent connections *) @@ -951,13 +953,14 @@ let add_route_server_sent_handler ?accept self route f = let create ?(masksigpipe=true) ?(max_connections=32) + ?(timeout=0.0) ?(new_thread=(fun f -> ignore (Thread.create f () : Thread.t))) ?(addr="127.0.0.1") ?(port=8080) ?sock () : t = let handler _req = Response.fail ~code:404 "no top handler" in let max_connections = max 4 max_connections in { new_thread; addr; port; sock; masksigpipe; handler; running= true; sem_max_connections=Sem_.create max_connections; - path_handlers=[]; + path_handlers=[]; timeout; cb_encode_resp=[]; cb_decode_req=[]; } @@ -973,6 +976,8 @@ let find_map f l = in aux f l let handle_client_ (self:t) (client_sock:Unix.file_descr) : unit = + let _ = Unix.(setsockopt_float client_sock SO_RCVTIMEO self.timeout) in + let _ = Unix.(setsockopt_float client_sock SO_SNDTIMEO self.timeout) in let ic = Unix.in_channel_of_descr client_sock in let oc = Unix.out_channel_of_descr client_sock in let buf = Buf_.create() in diff --git a/src/Tiny_httpd.mli b/src/Tiny_httpd.mli index ec8dee10..ffa8e639 100644 --- a/src/Tiny_httpd.mli +++ b/src/Tiny_httpd.mli @@ -434,6 +434,7 @@ type t val create : ?masksigpipe:bool -> ?max_connections:int -> + ?timeout:float -> ?new_thread:((unit -> unit) -> unit) -> ?addr:string -> ?port:int -> @@ -454,6 +455,9 @@ val create : could use a thread pool instead. @param max_connections maximum number of simultaneous connections. + @param timeout connection is closed if the socket does not do read or + write for the amount of second. Default: 0.0 which means no timeout. + timeout is not recommended when using proxy. @param addr address (IPv4 or IPv6) to listen on. Default ["127.0.0.1"]. @param port to listen on. Default [8080]. @param sock an existing socket given to the server to listen on, e.g. by @@ -643,4 +647,3 @@ val _debug : ((('a, out_channel, unit, unit, unit, unit) format6 -> 'a) -> unit) val _enable_debug: bool -> unit (**/**) -