package async_unix

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file tcp.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
open! Core
open! Import
module Unix = Unix_syscalls
module Socket = Unix.Socket

module Where_to_connect = struct
  type 'addr t =
    { socket_type : 'addr Socket.Type.t
    ; remote_address : unit -> 'addr Deferred.t
    ; local_address : 'addr option
    ; info : Sexp.t
    }

  let sexp_of_t _ { info; _ } = info

  type inet = Socket.Address.Inet.t t [@@deriving sexp_of]
  type unix = Socket.Address.Unix.t t [@@deriving sexp_of]

  let remote_address t = t.remote_address ()

  let create_local_address ~bind_to_address ~bind_to_port =
    match bind_to_address, bind_to_port with
    | None, None -> None
    | None, Some port -> Some (Socket.Address.Inet.create_bind_any ~port)
    | Some inet_addr, bind_to_port ->
      let port = Option.value bind_to_port ~default:0 in
      Some (Socket.Address.Inet.create ~port inet_addr)
  ;;

  let of_host_and_port
    ?bind_to_address
    ?bind_to_port
    ?(show_port_in_test = false)
    ({ Host_and_port.host; port } as hp)
    =
    { socket_type = Socket.Type.tcp
    ; remote_address =
        (fun () ->
          Unix.Inet_addr.of_string_or_getbyname host
          >>| fun inet_addr -> Socket.Address.Inet.create inet_addr ~port)
    ; local_address = create_local_address ~bind_to_address ~bind_to_port
    ; info =
        (match show_port_in_test with
         | true -> [%sexp (hp : Host_and_port.t)]
         | false -> [%sexp (hp : Host_and_port.Hide_port_in_test.t)])
    }
  ;;

  let of_file file =
    { socket_type = Socket.Type.unix
    ; remote_address = (fun () -> return (Socket.Address.Unix.create file))
    ; local_address = None
    ; info = [%sexp_of: string] file
    }
  ;;

  let of_inet_address ?bind_to_address ?bind_to_port ?(show_port_in_test = false) address =
    { socket_type = Socket.Type.tcp
    ; remote_address = (fun () -> return address)
    ; local_address = create_local_address ~bind_to_address ~bind_to_port
    ; info =
        (match show_port_in_test with
         | true -> [%sexp (address : Socket.Address.Inet.Show_port_in_test.t)]
         | false -> [%sexp (address : Socket.Address.Inet.t)])
    }
  ;;

  let of_unix_address address =
    { socket_type = Socket.Type.unix
    ; remote_address = (fun () -> return address)
    ; local_address = None
    ; info = [%sexp_of: Socket.Address.Unix.t] address
    }
  ;;
end

let close_sock_on_error s f =
  Monitor.try_with ~run:`Schedule ~rest:`Log ~name:"Tcp.close_sock_on_error" f
  >>| function
  | Ok v -> v
  | Error e ->
    (* [close] may fail, but we don't really care, since it will fail
       asynchronously.  The error we really care about is [e], and the
       [raise_error] will cause the current monitor to see that. *)
    don't_wait_for (Unix.close (Socket.fd s));
    raise e
;;

let reader_writer_of_sock ?buffer_age_limit ?reader_buffer_size ?writer_buffer_size s =
  let fd = Socket.fd s in
  ( Reader.create ?buf_len:reader_buffer_size fd
  , Writer.create ?buffer_age_limit ?buf_len:writer_buffer_size fd )
;;

let connect_sock
  ?socket
  ?interrupt
  ?(timeout = sec 10.)
  ?time_source
  (where_to_connect : _ Where_to_connect.t)
  =
  let time_source =
    match time_source with
    | Some x -> Time_source.read_only x
    | None -> Time_source.wall_clock ()
  in
  where_to_connect.remote_address ()
  >>= fun address ->
  let timeout =
    Time_source.Event.after time_source (Time_ns.Span.of_span_float_round_nearest timeout)
  in
  let interrupt =
    let timeout =
      Time_source.Event.fired timeout
      >>= function
      | Aborted () -> Deferred.never ()
      | Happened () -> Deferred.unit
    in
    match interrupt with
    | None -> timeout
    | Some interrupt -> Deferred.any [ interrupt; timeout ]
  in
  let connect_interruptible s = Socket.connect_interruptible s address ~interrupt in
  Deferred.create (fun result ->
    let s =
      match socket with
      | Some s -> s
      | None -> Socket.create where_to_connect.socket_type
    in
    close_sock_on_error s (fun () ->
      match where_to_connect.local_address with
      | None -> connect_interruptible s
      | Some local_interface ->
        Socket.bind s local_interface >>= fun s -> connect_interruptible s)
    >>> function
    | `Ok s ->
      Time_source.Event.abort_if_possible timeout ();
      Ivar.fill_exn result s
    | `Interrupted ->
      don't_wait_for (Unix.close (Socket.fd s));
      let address = Socket.Address.to_string address in
      (match Time_source.Event.abort timeout () with
       | Previously_happened () ->
         raise_s [%sexp "connection attempt timeout", (address : string)]
       | Ok | Previously_aborted () ->
         raise_s [%sexp "connection attempt aborted", (address : string)]))
;;

let connect
  ?socket
  ?buffer_age_limit
  ?interrupt
  ?reader_buffer_size
  ?writer_buffer_size
  ?timeout
  ?time_source
  where_to_connect
  =
  connect_sock ?socket ?interrupt ?timeout ?time_source where_to_connect
  >>| fun s ->
  let r, w =
    reader_writer_of_sock ?buffer_age_limit ?reader_buffer_size ?writer_buffer_size s
  in
  s, r, w
;;

let collect_errors writer f =
  let monitor = Writer.monitor writer in
  ignore (Monitor.detach_and_get_error_stream monitor : _ Stream.t);
  (* don't propagate errors up, we handle them here *)
  choose
    [ choice (Monitor.get_next_error monitor) (fun e -> Error e)
    ; choice
        (Monitor.try_with ~run:`Schedule ~rest:`Log ~name:"Tcp.collect_errors" f)
        Fn.id
    ]
;;

let close_connection_via_reader_and_writer r w =
  let force_close_event = Clock.Event.after (sec 30.) in
  let force_close =
    Clock.Event.fired force_close_event
    >>= function
    | Aborted () -> Deferred.never ()
    | Happened () -> Deferred.unit
  in
  Writer.close w ~force_close
  >>= fun () ->
  Clock.Event.abort_if_possible force_close_event ();
  Reader.close r
;;

let with_connection
  ?buffer_age_limit
  ?interrupt
  ?reader_buffer_size
  ?writer_buffer_size
  ?timeout
  ?time_source
  where_to_connect
  f
  =
  connect_sock ?interrupt ?timeout ?time_source where_to_connect
  >>= fun socket ->
  let r, w =
    reader_writer_of_sock ?buffer_age_limit ?reader_buffer_size ?writer_buffer_size socket
  in
  let res = collect_errors w (fun () -> f socket r w) in
  Deferred.any
    [ (res >>| fun (_ : ('a, exn) Result.t) -> ())
    ; Reader.close_finished r
    ; Writer.close_finished w
    ]
  >>= fun () ->
  close_connection_via_reader_and_writer r w
  >>= fun () ->
  res
  >>| function
  | Ok v -> v
  | Error e -> raise e
;;

module Bind_to_address = struct
  type t =
    | Address of Unix.Inet_addr.t
    | All_addresses
    | Localhost
  [@@deriving sexp_of]
end

module Bind_to_port = struct
  type t =
    | On_port of int
    | On_port_chosen_by_os
  [@@deriving sexp_of]
end

module Where_to_listen = struct
  type ('address, 'listening_on) t =
    { socket_type : 'address Socket.Type.t
    ; address : 'address
    ; listening_on : ('address -> 'listening_on[@sexp.opaque])
    }
  [@@deriving sexp_of, fields ~getters]

  type inet = (Socket.Address.Inet.t, int) t [@@deriving sexp_of]
  type unix = (Socket.Address.Unix.t, string) t [@@deriving sexp_of]

  let is_inet_witness t = Socket.Family.is_inet_witness (Socket.Type.family t.socket_type)
  let create ~socket_type ~address ~listening_on = { socket_type; address; listening_on }

  let bind_to (bind_to_address : Bind_to_address.t) (bind_to_port : Bind_to_port.t) =
    let port =
      match bind_to_port with
      | On_port port -> port
      | On_port_chosen_by_os -> 0
    in
    let address =
      match bind_to_address with
      | All_addresses -> Socket.Address.Inet.create_bind_any ~port
      | Address addr -> Socket.Address.Inet.create addr ~port
      | Localhost -> Socket.Address.Inet.create Unix.Inet_addr.localhost ~port
    in
    { socket_type = Socket.Type.tcp
    ; address
    ; listening_on =
        (function
         | `Inet (_, port) -> port)
    }
  ;;

  let of_port port = bind_to All_addresses (On_port port)
  let of_port_chosen_by_os = bind_to All_addresses On_port_chosen_by_os

  let of_file path =
    { socket_type = Socket.Type.unix
    ; address = Socket.Address.Unix.create path
    ; listening_on = (fun _ -> path)
    }
  ;;

  let binding_on_port_chosen_by_os t =
    match t.address with
    | `Inet _ as inet -> Socket.Address.Inet.port inet = 0
    | `Unix _ -> false
  ;;

  let max_retries_upon_addr_in_use t =
    match binding_on_port_chosen_by_os t with
    | true -> 10
    | false -> 0
  ;;
end

module Server = struct
  module Connection = struct
    type 'address t =
      { client_socket : ([ `Active ], 'address) Socket.t
      ; client_address : 'address
      }
    [@@deriving fields ~iterators:iter, sexp_of]

    let invariant invariant_address t =
      Invariant.invariant [%here] t [%sexp_of: _ t] (fun () ->
        let check f = Invariant.check_field t f in
        Fields.iter ~client_socket:ignore ~client_address:(check invariant_address))
    ;;

    let create ~client_socket ~client_address = { client_socket; client_address }
    let close t = Fd.close (Socket.fd t.client_socket)
  end

  module Max_connections = struct
    type t =
      { limit : int
      ; time_source : Time_source.t
      ; listening_on : Info.t
      ; mutable last_logged : Time_ns.t option
      }

    let sexp_of_t t = [%sexp_of: int] t.limit

    let create ~limit ~time_source ~listening_on =
      { limit; time_source; listening_on; last_logged = None }
    ;;

    (* We make sure not to be too spammy with logs. This number was chosen pretty
       arbitrarily. *)
    let log_threshold = Time_ns.Span.of_min 1.
    let max_connection_limit_logger = ref (eprint_s ?mach:None)
    let set_logger = ( := ) max_connection_limit_logger

    let log_at_limit t ~now =
      !max_connection_limit_logger
        [%message
          "At limit of Tcp server [max_connections]. New connections will not be \
           accepted until an existing connection is closed."
            ~limit:(t.limit : int)
            ~listening_on:(t.listening_on : Info.t)];
      t.last_logged <- Some now
    ;;

    let maybe_log_at_limit t =
      let now = Time_source.now t.time_source in
      match t.last_logged with
      | None -> log_at_limit t ~now
      | Some last_logged ->
        if Time_ns.Span.( > ) (Time_ns.diff now last_logged) log_threshold
        then log_at_limit t ~now
    ;;
  end

  type ('address, 'listening_on) t =
    { socket : ([ `Passive ], 'address) Socket.t
    ; listening_on : 'listening_on
    ; on_handler_error : [ `Raise | `Ignore | `Call of 'address -> exn -> unit ]
    ; handle_client :
        'address -> ([ `Active ], 'address) Socket.t -> (unit, exn) Result.t Deferred.t
    ; max_connections : Max_connections.t
    ; max_accepts_per_batch : int
    ; connections : 'address Connection.t Bag.t
    ; mutable accept_is_pending : bool
    ; mutable drop_incoming_connections : bool
    ; close_finished_and_handlers_determined : unit Ivar.t
    }
  [@@deriving fields ~getters ~setters ~iterators:iter, sexp_of]

  let is_dropping_incoming_connections t = t.drop_incoming_connections
  let num_connections t = Bag.length t.connections
  let listening_socket = socket

  type inet = (Socket.Address.Inet.t, int) t [@@deriving sexp_of]
  type unix = (Socket.Address.Unix.t, string) t [@@deriving sexp_of]

  let listening_on_address (t : (_, _) t) = Socket.getsockname t.socket

  let invariant t : unit =
    try
      let check f field = f (Field.get field t) in
      Fields.iter
        ~socket:ignore
        ~listening_on:ignore
        ~on_handler_error:ignore
        ~handle_client:ignore
        ~max_connections:
          (check (fun (max_connections : Max_connections.t) ->
             assert (max_connections.limit >= 1)))
        ~max_accepts_per_batch:
          (check (fun max_accepts_per_batch -> assert (max_accepts_per_batch >= 1)))
        ~connections:
          (check (fun connections ->
             Bag.invariant (Connection.invariant ignore) connections;
             let num_connections = num_connections t in
             assert (num_connections >= 0);
             assert (num_connections <= t.max_connections.limit)))
        ~accept_is_pending:ignore
        ~drop_incoming_connections:ignore
        ~close_finished_and_handlers_determined:ignore
    with
    | exn ->
      failwiths ~here:[%here] "invariant failed" (exn, t) [%sexp_of: exn * (_, _) t]
  ;;

  let fd t = Socket.fd t.socket
  let is_closed t = Fd.is_closed (fd t)
  let close_finished t = Fd.close_finished (fd t)

  let close_finished_and_handlers_determined t =
    Ivar.read t.close_finished_and_handlers_determined
  ;;

  let close ?(close_existing_connections = false) t =
    let fd_closed = Fd.close (fd t) in
    if not close_existing_connections
    then fd_closed
    else
      (* Connections are removed from the bag by the [maybe_accept] below, as the fds are
         closed. *)
      Deferred.all_unit
        (fd_closed :: List.map (Bag.to_list t.connections) ~f:Connection.close)
  ;;

  (* [maybe_accept] is a bit tricky, but the idea is to avoid calling [accept] until we
     have an available slot (determined by [num_connections < max_connections]). *)
  let rec maybe_accept t =
    let available_slots = t.max_connections.limit - num_connections t in
    if (not (is_closed t)) && available_slots > 0 && not t.accept_is_pending
    then (
      t.accept_is_pending <- true;
      Socket.accept_at_most ~limit:(min t.max_accepts_per_batch available_slots) t.socket
      >>> fun accept_result ->
      t.accept_is_pending <- false;
      match accept_result with
      | `Socket_closed -> ()
      | `Ok conns ->
        (* It is possible that someone called [close t] after the [accept] returned but
           before we got here.  In that case, we just close the clients.  One might argue
           that if [close] was called with [close_existing_connections = false], then we
           should not close these, but instead let the clients finish their business. One
           may want this for example to arrange a smooth handover when using
           [SO_REUSEPORT].
           Unfortunately, even if we make this fix, a smooth handover
           does not seem to be possible on Linux, since Linux assigns a connection to a
           process before [accept] is called, which creates an inherent race between that
           and [close]: any connections assigned to a listening socket at the time of
           [close] will be dropped.
           *)
        if is_closed t || t.drop_incoming_connections
        then
          List.iter conns ~f:(fun (sock, _) -> don't_wait_for (Fd.close (Socket.fd sock)))
        else
          (* We first [handle_client] on all the connections, which increases
             [num_connections], and then call [maybe_accept] to try to accept more
             clients, which respects the just-increased [num_connections]. *)
          List.iter conns ~f:(fun (sock, addr) -> handle_client t sock addr);
        maybe_accept t)
    else if (not (is_closed t)) && available_slots = 0
    then Max_connections.maybe_log_at_limit t.max_connections

  and handle_client t client_socket client_address =
    let connection = Connection.create ~client_socket ~client_address in
    let connections_elt = Bag.add t.connections connection in
    t.handle_client client_address client_socket
    >>> fun res ->
    Connection.close connection
    >>> fun () ->
    Bag.remove t.connections connections_elt;
    if Deferred.is_determined (close_finished t) && num_connections t = 0
    then Ivar.fill_if_empty t.close_finished_and_handlers_determined ();
    (match res with
     | Ok () -> ()
     | Error e ->
       (try
          match t.on_handler_error with
          | `Ignore -> ()
          | `Raise -> raise e
          | `Call f -> f client_address e
        with
        | e ->
          don't_wait_for (close t);
          raise e));
    maybe_accept t
  ;;

  let create_from_socket
    ~max_connections
    ?(max_accepts_per_batch = 1)
    ?(drop_incoming_connections = false)
    ~on_handler_error
    (where_to_listen : _ Where_to_listen.t)
    handle_client
    socket
    =
    let t =
      { socket
      ; listening_on = where_to_listen.listening_on (Socket.getsockname socket)
      ; on_handler_error
      ; handle_client
      ; max_connections
      ; max_accepts_per_batch
      ; connections = Bag.create ()
      ; accept_is_pending = false
      ; drop_incoming_connections
      ; close_finished_and_handlers_determined = Ivar.create ()
      }
    in
    (close_finished t
     >>> fun () ->
     if num_connections t = 0
     then Ivar.fill_if_empty t.close_finished_and_handlers_determined ());
    maybe_accept t;
    t
  ;;

  let get_max_connections_limit max_connections =
    match max_connections with
    | None -> 10_000
    | Some max_connections ->
      if max_connections <= 0
      then
        failwiths
          ~here:[%here]
          "Tcp.Server.creater got negative [max_connections]"
          max_connections
          sexp_of_int;
      max_connections
  ;;

  module Socket_creator : sig
    type 'a t constraint 'a = [< Socket.Address.t ]

    val create
      :  ([ `Unconnected ], 'addr) Socket.t option
      -> ('addr, _) Where_to_listen.t
      -> 'addr t

    val bind_and_listen_maybe_retry
      :  'addr t
      -> f:
           (([ `Unconnected ], 'addr) Socket.t
            -> ([ `Passive ], 'addr) Socket.t Deferred.t)
      -> ([ `Passive ], 'addr) Socket.t Deferred.t

    val bind_and_listen_maybe_retry'
      :  'addr t
      -> f:(([ `Unconnected ], 'addr) Socket.t -> ([ `Passive ], 'addr) Socket.t)
      -> ([ `Passive ], 'addr) Socket.t
  end = struct
    type 'addr t =
      { create_socket : unit -> ([ `Unconnected ], 'addr) Socket.t
      ; retries_upon_addr_in_use : int
      }

    let create maybe_socket where_to_listen =
      match maybe_socket with
      | Some socket -> { create_socket = Fn.const socket; retries_upon_addr_in_use = 0 }
      | None ->
        { create_socket =
            (fun () ->
              let socket = Socket.create where_to_listen.Where_to_listen.socket_type in
              Socket.setopt socket Socket.Opt.reuseaddr true;
              socket)
        ; retries_upon_addr_in_use =
            Where_to_listen.max_retries_upon_addr_in_use where_to_listen
        }
    ;;

    let handle_exn t socket exn ~retries_attempted_upon_addr_in_use =
      don't_wait_for (Unix.close (Socket.fd socket));
      match t.retries_upon_addr_in_use > retries_attempted_upon_addr_in_use, exn with
      | true, Unix.Unix_error (EADDRINUSE, _, _) -> `Please_retry
      | _, _ ->
        if retries_attempted_upon_addr_in_use > 0
        then
          raise_s
            [%message
              "Failed to bind and listen to socket."
                (exn : Exn.t)
                (retries_attempted_upon_addr_in_use : int)]
        else raise exn
    ;;

    let rec aux_bind_and_listen_maybe_retry t ~retries_attempted_upon_addr_in_use ~f =
      let socket = t.create_socket () in
      match%bind Monitor.try_with ~extract_exn:true (fun () -> f socket) with
      | Ok v -> return v
      | Error exn ->
        let `Please_retry = handle_exn t socket exn ~retries_attempted_upon_addr_in_use in
        aux_bind_and_listen_maybe_retry
          t
          ~retries_attempted_upon_addr_in_use:(retries_attempted_upon_addr_in_use + 1)
          ~f
    ;;

    let rec aux_bind_and_listen_maybe_retry' t ~retries_attempted_upon_addr_in_use ~f =
      let socket = t.create_socket () in
      try f socket with
      | exn ->
        let `Please_retry = handle_exn t socket exn ~retries_attempted_upon_addr_in_use in
        aux_bind_and_listen_maybe_retry'
          t
          ~retries_attempted_upon_addr_in_use:(retries_attempted_upon_addr_in_use + 1)
          ~f
    ;;

    let bind_and_listen_maybe_retry =
      aux_bind_and_listen_maybe_retry ~retries_attempted_upon_addr_in_use:0
    ;;

    let bind_and_listen_maybe_retry' =
      aux_bind_and_listen_maybe_retry' ~retries_attempted_upon_addr_in_use:0
    ;;
  end

  let create_sock_non_inet_internal
    ?max_connections
    ?max_accepts_per_batch
    ?backlog
    ?drop_incoming_connections
    ?socket
    ?time_source
    ~on_handler_error
    (where_to_listen : _ Where_to_listen.t)
    handle_client
    =
    let time_source =
      match time_source with
      | Some x -> Time_source.read_only x
      | None -> Time_source.wall_clock ()
    in
    let%map socket =
      let socket_creator = Socket_creator.create socket where_to_listen in
      Socket_creator.bind_and_listen_maybe_retry socket_creator ~f:(fun socket ->
        Socket.bind_keep_opts socket where_to_listen.address >>| Socket.listen ?backlog)
    in
    let max_connections =
      Max_connections.create
        ~limit:(get_max_connections_limit max_connections)
        ~time_source
          (* We must call [Fd.info] on the socket's fd after [Socket.bind] is called,
           otherwise the [Info.t] won't have been set yet. *)
        ~listening_on:(Fd.info (Socket.fd socket))
    in
    create_from_socket
      ~max_connections
      ?max_accepts_per_batch
      ?drop_incoming_connections
      ~on_handler_error
      where_to_listen
      handle_client
      socket
  ;;

  let create_sock_inet_internal
    ?max_connections
    ?max_accepts_per_batch
    ?backlog
    ?drop_incoming_connections
    ?(socket : ([ `Unconnected ], Socket.Address.Inet.t) Socket.t option)
    ?time_source
    ~on_handler_error
    (where_to_listen : (Socket.Address.Inet.t, 'listening_on) Where_to_listen.t)
    handle_client
    =
    let time_source =
      match time_source with
      | Some x -> Time_source.read_only x
      | None -> Time_source.wall_clock ()
    in
    let socket =
      let socket_creator = Socket_creator.create socket where_to_listen in
      Socket_creator.bind_and_listen_maybe_retry' socket_creator ~f:(fun socket ->
        Socket.bind_inet_keep_opts socket where_to_listen.address
        |> Socket.listen ?backlog)
    in
    let max_connections =
      Max_connections.create
        ~limit:(get_max_connections_limit max_connections)
        ~time_source
          (* We must call [Fd.info] on the socket's fd after [Socket.bind_inet] is called,
           otherwise the [Info.t] won't have been set yet. *)
        ~listening_on:(Fd.info (Socket.fd socket))
    in
    create_from_socket
      ~max_connections
      ?max_accepts_per_batch
      ?drop_incoming_connections
      ~on_handler_error
      where_to_listen
      handle_client
      socket
  ;;

  type ('address, 'listening_on, 'time_source_access) create_sock_async =
    ?max_connections:int
    -> ?max_accepts_per_batch:int
    -> ?backlog:int
    -> ?drop_incoming_connections:bool
    -> ?socket:([ `Unconnected ], 'address) Socket.t
    -> ?time_source:([> read ] as 'time_source_access) Time_source.T1.t
    -> on_handler_error:[ `Call of 'address -> exn -> unit | `Ignore | `Raise ]
    -> ('address, 'listening_on) Where_to_listen.t
    -> ('address -> ([ `Active ], 'address) Socket.t -> (unit, exn) Result.t Deferred.t)
    -> ('address, 'listening_on) t Deferred.t

  let create_sock_inet_internal_async : ('address, _, _) create_sock_async =
    fun ?max_connections
        ?max_accepts_per_batch
        ?backlog
        ?drop_incoming_connections
        ?socket
        ?time_source
        ~on_handler_error
        where_to_listen
        handle_client ->
    return
      (create_sock_inet_internal
         ?max_connections
         ?max_accepts_per_batch
         ?backlog
         ?drop_incoming_connections
         ?socket
         ?time_source
         ~on_handler_error
         where_to_listen
         handle_client)
  ;;

  type ('address, 'listening_on, 't) create_sock_async_no_constraint =
    | T :
        ('address, 'listening_on, 't) create_sock_async
        -> ('address, 'listening_on, 't) create_sock_async_no_constraint

  type 'address is_address_type = T : [< Socket.Address.t ] is_address_type

  let create_sock_internal_type_hackery
    : type address listening_on.
      is_address:address is_address_type
      -> is_inet:(address, [ `Inet of Unix.Inet_addr.t * int ]) Type_equal.t option
      -> (address, listening_on, _) create_sock_async_no_constraint
    =
    fun ~is_address ~is_inet ->
    match is_inet with
    | Some T -> T create_sock_inet_internal_async
    | None ->
      let T = is_address in
      T create_sock_non_inet_internal
  ;;

  let create_sock_internal : (_, _, [> read ]) create_sock_async =
    fun ?max_connections
        ?max_accepts_per_batch
        ?backlog
        ?drop_incoming_connections
        ?socket
        ?time_source
        ~on_handler_error
        where_to_listen
        handle_client ->
    let (T f) =
      create_sock_internal_type_hackery
        ~is_inet:(Where_to_listen.is_inet_witness where_to_listen)
        ~is_address:T
    in
    f
      ?max_connections
      ?max_accepts_per_batch
      ?backlog
      ?drop_incoming_connections
      ?socket
      ?time_source
      ~on_handler_error
      where_to_listen
      handle_client
  ;;

  let create_sock
    ?max_connections
    ?max_accepts_per_batch
    ?backlog
    ?drop_incoming_connections
    ?socket
    ?time_source
    ~on_handler_error
    (where_to_listen : ('address, 'listening_on) Where_to_listen.t)
    (handle_client :
      ([< Socket.Address.t ] as 'b) -> ([ `Active ], 'b) Socket.t -> unit Deferred.t)
    =
    create_sock_internal
      ?max_connections
      ?max_accepts_per_batch
      ?backlog
      ?drop_incoming_connections
      ~on_handler_error
      ?socket
      ?time_source
      where_to_listen
      (fun client_address client_socket ->
      Monitor.try_with ~run:`Schedule ~rest:`Log ~name:"Tcp.Server.create_sock" (fun () ->
        handle_client client_address client_socket))
  ;;

  let create_sock_inet
    ?max_connections
    ?max_accepts_per_batch
    ?backlog
    ?drop_incoming_connections
    ?socket
    ?time_source
    ~on_handler_error
    where_to_listen
    handle_client
    =
    create_sock_inet_internal
      ?max_connections
      ?max_accepts_per_batch
      ?backlog
      ?drop_incoming_connections
      ?socket
      ?time_source
      ~on_handler_error
      where_to_listen
      (fun client_address client_socket ->
      Monitor.try_with
        ~run:`Schedule
        ~rest:`Log
        ~name:"Tcp.Server.create_sock_inet"
        (fun () -> handle_client client_address client_socket))
  ;;

  let create_internal
    ~create_sock
    ?buffer_age_limit
    ?max_connections
    ?max_accepts_per_batch
    ?backlog
    ?drop_incoming_connections
    ?socket
    ?time_source
    ~on_handler_error
    where_to_listen
    handle_client
    =
    create_sock
      ?max_connections
      ?max_accepts_per_batch
      ?backlog
      ?drop_incoming_connections
      ?socket
      ?time_source
      ~on_handler_error
      where_to_listen
      (fun client_address client_socket ->
      let r, w = reader_writer_of_sock ?buffer_age_limit client_socket in
      Writer.set_raise_when_consumer_leaves w false;
      Deferred.any
        [ collect_errors w (fun () -> handle_client client_address r w)
        ; Writer.consumer_left w |> Deferred.ok
        ]
      >>= fun res -> close_connection_via_reader_and_writer r w >>| fun () -> res)
  ;;

  let create_inet
    ?buffer_age_limit
    ?max_connections
    ?max_accepts_per_batch
    ?backlog
    ?drop_incoming_connections
    ?socket
    ?time_source
    ~on_handler_error
    where_to_listen
    handle_client
    =
    create_internal
      ~create_sock:create_sock_inet_internal
      ?buffer_age_limit
      ?max_connections
      ?max_accepts_per_batch
      ?backlog
      ?drop_incoming_connections
      ?socket
      ?time_source
      ~on_handler_error
      where_to_listen
      handle_client
  ;;

  let create
    ?buffer_age_limit
    ?max_connections
    ?max_accepts_per_batch
    ?backlog
    ?drop_incoming_connections
    ?socket
    ?time_source
    ~on_handler_error
    where_to_listen
    handle_client
    =
    create_internal
      ~create_sock:create_sock_internal
      ?buffer_age_limit
      ?max_connections
      ?max_accepts_per_batch
      ?backlog
      ?drop_incoming_connections
      ?socket
      ?time_source
      ~on_handler_error
      where_to_listen
      handle_client
  ;;

  module Private = struct
    let fd = fd
  end
end

module Aliases = struct
  type 'a with_connect_options =
    ?buffer_age_limit:[ `At_most of Time.Span.t | `Unlimited ]
    -> ?interrupt:unit Deferred.t
    -> ?reader_buffer_size:int
    -> ?writer_buffer_size:int
    -> ?timeout:Time.Span.t
    -> ?time_source:Time_source.t
    -> 'a
end

module Private = struct
  let close_connection_via_reader_and_writer = close_connection_via_reader_and_writer
  let set_max_connection_limit_logger = Server.Max_connections.set_logger
end
OCaml

Innovation. Community. Security.