@@ -598,6 +598,34 @@ let handle events fd k =
598
598
in
599
599
setup ()
600
600
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
+
601
629
end
602
630
603
631
let check_hung_requests server =
@@ -1017,38 +1045,10 @@ let handle_client_lwt client cin answer =
1017
1045
1018
1046
let accept_hook = ref (fun () -> () )
1019
1047
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
-
1049
1048
let handle_lwt config fd k =
1050
1049
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
1052
1052
let % lwt () = if config.yield then Lwt_unix. yield () else Lwt. return_unit in
1053
1053
loop ()
1054
1054
in
0 commit comments