Skip to content

Commit 3050ee5

Browse files
committed
httpev: Tcp.handle_lwt
1 parent 03ce953 commit 3050ee5

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

httpev.ml

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,34 @@ let handle events fd k =
598598
in
599599
setup ()
600600

601+
let handle_lwt ?(single=false) fd k =
602+
match%lwt Exn_lwt.map Lwt_unix.accept fd with
603+
| `Exn (Unix.Unix_error (Unix.EMFILE,_,_)) ->
604+
let pause = 2. in
605+
log #error "too many open files, disabling accept for %s" (Time.duration_str pause);
606+
Lwt_unix.sleep pause
607+
| `Exn Lwt.Canceled -> log #info "canceling accept loop"; Lwt.fail Lwt.Canceled
608+
| `Exn exn -> log #warn ~exn "accept"; Lwt.return_unit
609+
| `Ok (fd,addr as peer) ->
610+
let task =
611+
begin
612+
try%lwt
613+
Unix.set_close_on_exec (Lwt_unix.unix_file_descr fd);
614+
k peer
615+
with exn -> log #warn ~exn "accepted (%s)" (Nix.show_addr addr); Lwt.return_unit
616+
end [%lwt.finally
617+
Lwt_unix.(Exn.suppress (shutdown fd) SHUTDOWN_ALL);
618+
Lwt_unix.close fd
619+
]
620+
in
621+
if single then
622+
task
623+
else
624+
begin
625+
Lwt.ignore_result task; (* "fork" processing *)
626+
Lwt.return_unit
627+
end
628+
601629
end
602630

603631
let check_hung_requests server =
@@ -1017,38 +1045,10 @@ let handle_client_lwt client cin answer =
10171045

10181046
let accept_hook = ref (fun () -> ())
10191047

1020-
let handle_lwt ~single fd k =
1021-
!accept_hook ();
1022-
match%lwt Exn_lwt.map Lwt_unix.accept fd with
1023-
| `Exn (Unix.Unix_error (Unix.EMFILE,_,_)) ->
1024-
let pause = 2. in
1025-
log #error "too many open files, disabling accept for %s" (Time.duration_str pause);
1026-
Lwt_unix.sleep pause
1027-
| `Exn Lwt.Canceled -> log #info "canceling accept loop"; Lwt.fail Lwt.Canceled
1028-
| `Exn exn -> log #warn ~exn "accept"; Lwt.return_unit
1029-
| `Ok (fd,addr as peer) ->
1030-
let task =
1031-
begin
1032-
try%lwt
1033-
Unix.set_close_on_exec (Lwt_unix.unix_file_descr fd);
1034-
k peer
1035-
with exn -> log #warn ~exn "accepted (%s)" (Nix.show_addr addr); Lwt.return_unit
1036-
end [%lwt.finally
1037-
Lwt_unix.(Exn.suppress (shutdown fd) SHUTDOWN_ALL);
1038-
Lwt_unix.close fd
1039-
]
1040-
in
1041-
if single then
1042-
task
1043-
else
1044-
begin
1045-
Lwt.ignore_result task; (* "fork" processing *)
1046-
Lwt.return_unit
1047-
end
1048-
10491048
let handle_lwt config fd k =
10501049
let rec loop () =
1051-
let%lwt () = handle_lwt ~single:config.single fd k in
1050+
!accept_hook ();
1051+
let%lwt () = Tcp.handle_lwt ~single:config.single fd k in
10521052
let%lwt () = if config.yield then Lwt_unix.yield () else Lwt.return_unit in
10531053
loop ()
10541054
in

0 commit comments

Comments
 (0)