diff --git a/examples/run_test.sh b/examples/run_test.sh index e522747d..97d920b6 100755 --- a/examples/run_test.sh +++ b/examples/run_test.sh @@ -3,8 +3,10 @@ PORT=8082 ./sse_server.exe -p $PORT & +PID=$! + sleep 0.1 ./sse_client.exe -p $PORT --alarm=1 /count | tr -d '\r' || true -kill %1 +kill $PID echo "success" diff --git a/src/Tiny_httpd.ml b/src/Tiny_httpd.ml index 07956191..0270ca90 100644 --- a/src/Tiny_httpd.ml +++ b/src/Tiny_httpd.ml @@ -676,13 +676,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; @@ -699,6 +700,8 @@ module Sem_ = struct t.n <- t.n + m; Condition.broadcast t.cond; Mutex.unlock t.mutex + + let num_acquired t = t.max - t.n end module Route = struct @@ -812,6 +815,8 @@ type t = { sock: Unix.file_descr option; + timeout: float; + sem_max_connections: Sem_.t; (* semaphore to restrict the number of active concurrent connections *) @@ -841,6 +846,8 @@ type t = { let addr self = self.addr let port self = self.port +let active_connections self = 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 let set_top_handler self f = self.handler <- f @@ -951,13 +958,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 +981,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 @@ -1104,6 +1114,7 @@ let run (self:t) : (unit,_) result = raise e ); with e -> + Sem_.release 1 self.sem_max_connections; _debug (fun k -> k "Unix.accept or Thread.create raised an exception: %s" (Printexc.to_string e)) diff --git a/src/Tiny_httpd.mli b/src/Tiny_httpd.mli index ec8dee10..f9bf9a15 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 @@ -472,6 +476,9 @@ val is_ipv6 : t -> bool val port : t -> int (** Port on which the server listens. *) +val active_connections : t -> int +(** Number of active connections *) + val add_decode_request_cb : t -> (unit Request.t -> (unit Request.t * (byte_stream -> byte_stream)) option) -> unit @@ -643,4 +650,3 @@ val _debug : ((('a, out_channel, unit, unit, unit, unit) format6 -> 'a) -> unit) val _enable_debug: bool -> unit (**/**) - diff --git a/tests/download_chunked.sh b/tests/download_chunked.sh index 6d8cbde6..2fc4e3c5 100755 --- a/tests/download_chunked.sh +++ b/tests/download_chunked.sh @@ -3,11 +3,12 @@ SERVER=$1 PORT=8084 "$SERVER" . -p $PORT & +PID=$! sleep 0.1 curl -N "http://localhost:${PORT}/foo_50" -o data2 \ -H 'Tranfer-encoding: chunked' -kill %1 +kill $PID wc data2 diff --git a/tests/echo1.sh b/tests/echo1.sh index 5cad7b60..22398c94 100755 --- a/tests/echo1.sh +++ b/tests/echo1.sh @@ -4,6 +4,7 @@ ECHO=$1 PORT=8085 "$ECHO" -p $PORT & +PID=$! sleep 0.1 curl -N "http://localhost:${PORT}/echo/?a=b&c=d" -H user-agent:test -kill %1 +kill $PID diff --git a/tests/sse_count.sh b/tests/sse_count.sh index 6010ed37..dcd0c352 100755 --- a/tests/sse_count.sh +++ b/tests/sse_count.sh @@ -4,7 +4,8 @@ SSE_SERVER=$1 PORT=8086 "$SSE_SERVER" -p $PORT & +PID=$! sleep 0.1 curl -N "http://localhost:${PORT}/count/10" -H user-agent:test -kill %1 +kill $PID diff --git a/tests/upload_chunked.sh b/tests/upload_chunked.sh index ca64ac8a..9cbd2d7d 100755 --- a/tests/upload_chunked.sh +++ b/tests/upload_chunked.sh @@ -6,10 +6,11 @@ SERVER=$1 PORT=8087 "$SERVER" . -p $PORT --upload --max-upload 100000000000 & +PID=$! sleep 0.1 cat foo_50 | curl -N -X PUT http://localhost:$PORT/data --data-binary @- -H 'Transfer-Encoding: chunked' -kill %1 +kill $PID wc data