From 20a36919ce9348b6840f888ca0f2b796d21c83a0 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Sat, 17 Feb 2024 11:51:50 -0500 Subject: [PATCH] perf: only call `select` in accept loop if accept would have blocked --- src/Tiny_httpd_server.ml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Tiny_httpd_server.ml b/src/Tiny_httpd_server.ml index 103533ca..4094fba5 100644 --- a/src/Tiny_httpd_server.ml +++ b/src/Tiny_httpd_server.ml @@ -984,7 +984,6 @@ module Unix_tcp_server_ = struct Unix.set_nonblock sock; while self.running do - ignore (Unix.select [ sock ] [] [ sock ] 1.0 : _ * _ * _); match Unix.accept sock with | client_sock, client_addr -> (* limit concurrency *) @@ -1012,7 +1011,8 @@ module Unix_tcp_server_ = struct ignore Unix.(sigprocmask SIG_UNBLOCK Sys.[ sigint; sighup ]) | exception Unix.Unix_error ((Unix.EAGAIN | Unix.EWOULDBLOCK), _, _) -> - () + (* wait for the socket to be ready, and re-enter the loop *) + ignore (Unix.select [ sock ] [] [ sock ] 1.0 : _ * _ * _) | exception e -> Log.error (fun k -> k "Unix.accept or Thread.create raised an exception: %s"