package async

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

Source file rpc.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
open Core
open Import
module Transport = Rpc_transport
module Low_latency_transport = Rpc_transport_low_latency
module Any = Rpc_kernel.Any
module Description = Rpc_kernel.Description
module Implementation = Rpc_kernel.Implementation
module Implementations = Rpc_kernel.Implementations
module One_way = Rpc_kernel.One_way
module Pipe_rpc = Rpc_kernel.Pipe_rpc
module Rpc = Rpc_kernel.Rpc
module State_rpc = Rpc_kernel.State_rpc
module Pipe_close_reason = Rpc_kernel.Pipe_close_reason

module Connection = struct
  include Rpc_kernel.Connection

  (* unfortunately, copied from reader0.ml *)
  let default_max_message_size = 100 * 1024 * 1024

  let create
        ?implementations
        ~connection_state
        ?(max_message_size = default_max_message_size)
        ?handshake_timeout
        ?heartbeat_config
        ?description
        reader
        writer
    =
    create
      ?implementations
      ~connection_state
      ?handshake_timeout:
        (Option.map handshake_timeout ~f:Time_ns.Span.of_span_float_round_nearest)
      ?heartbeat_config
      ?description
      (Transport.of_reader_writer reader writer ~max_message_size)
  ;;

  let contains_magic_prefix reader =
    Deferred.Or_error.try_with (fun () ->
      Reader.peek_bin_prot reader contains_magic_prefix)
    >>| function
    | Error _ | Ok `Eof -> false
    | Ok (`Ok b) -> b
  ;;

  let with_close
        ?implementations
        ?(max_message_size = default_max_message_size)
        ?handshake_timeout
        ?heartbeat_config
        ~connection_state
        reader
        writer
        ~dispatch_queries
        ~on_handshake_error
    =
    with_close
      ?implementations
      ?handshake_timeout:
        (Option.map handshake_timeout ~f:Time_ns.Span.of_span_float_round_nearest)
      ?heartbeat_config
      ~connection_state
      (Transport.of_reader_writer reader writer ~max_message_size)
      ~dispatch_queries
      ~on_handshake_error
  ;;

  let server_with_close
        ?(max_message_size = default_max_message_size)
        ?handshake_timeout
        ?heartbeat_config
        reader
        writer
        ~implementations
        ~connection_state
        ~on_handshake_error
    =
    server_with_close
      ?handshake_timeout:
        (Option.map handshake_timeout ~f:Time_ns.Span.of_span_float_round_nearest)
      ?heartbeat_config
      (Transport.of_reader_writer reader writer ~max_message_size)
      ~implementations
      ~connection_state
      ~on_handshake_error
  ;;

  let collect_errors (transport : Transport.t) ~f =
    let monitor = Transport.Writer.monitor transport.writer in
    (* don't propagate errors up, we handle them here *)
    ignore (Monitor.detach_and_get_error_stream monitor);
    choose
      [ choice (Monitor.get_next_error monitor) (fun e -> Error e)
      ; choice (try_with ~name:"Rpc.Connection.collect_errors" f) Fn.id
      ]
  ;;

  type transport_maker = Fd.t -> max_message_size:int -> Transport.t

  type on_handshake_error =
    [ `Raise
    | `Ignore
    | `Call of Exn.t -> unit
    ]

  let default_transport_maker fd ~max_message_size = Transport.of_fd fd ~max_message_size

  let serve_with_transport
        ~handshake_timeout
        ~heartbeat_config
        ~implementations
        ~description
        ~connection_state
        ~on_handshake_error
        transport
    =
    collect_errors transport ~f:(fun () ->
      Rpc_kernel.Connection.create
        ?handshake_timeout:
          (Option.map handshake_timeout ~f:Time_ns.Span.of_span_float_round_nearest)
        ?heartbeat_config
        ~implementations
        ~description
        ~connection_state
        transport
      >>= function
      | Ok t -> close_finished t
      | Error handshake_error ->
        (match on_handshake_error with
         | `Call f -> f handshake_error
         | `Raise -> raise handshake_error
         | `Ignore -> ());
        Deferred.unit)
    >>= fun res -> Transport.close transport >>| fun () -> Result.ok_exn res
  ;;

  let make_serve_func
        tcp_creator
        ~implementations
        ~initial_connection_state
        ~where_to_listen
        ?max_connections
        ?backlog
        ?(max_message_size = default_max_message_size)
        ?(make_transport = default_transport_maker)
        ?handshake_timeout
        ?heartbeat_config
        ?(auth = fun _ -> true)
        ?(on_handshake_error = `Ignore)
        ?(on_handler_error = `Ignore)
        ()
    =
    tcp_creator
      ?max_connections
      ?max_accepts_per_batch:None
      ?backlog
      ?socket:None
      ~on_handler_error
      where_to_listen
      (fun inet socket ->
         match (Socket.getpeername socket :> Socket.Address.t) with
         | exception _could_raise_if_the_socket_disconnects_quickly -> Deferred.unit
         | client_addr ->
           if not (auth inet)
           then Deferred.unit
           else (
             let description =
               let server_addr = (Socket.getsockname socket :> Socket.Address.t) in
               Info.create_s
                 [%message
                   "TCP server"
                     (server_addr : Socket.Address.t)
                     (client_addr : Socket.Address.t)]
             in
             let connection_state = initial_connection_state inet in
             serve_with_transport
               ~handshake_timeout
               ~heartbeat_config
               ~implementations
               ~description
               ~connection_state
               ~on_handshake_error
               (make_transport ~max_message_size (Socket.fd socket))))
  ;;

  let serve ~implementations ~initial_connection_state ~where_to_listen =
    make_serve_func
      Tcp.Server.create_sock
      ~implementations
      ~initial_connection_state
      ~where_to_listen
  ;;

  let serve_inet ~implementations ~initial_connection_state ~where_to_listen =
    make_serve_func
      Tcp.Server.create_sock_inet
      ~implementations
      ~initial_connection_state
      ~where_to_listen
  ;;

  let client'
        ?implementations
        ?(max_message_size = default_max_message_size)
        ?(make_transport = default_transport_maker)
        ?(handshake_timeout =
          Time_ns.Span.to_span_float_round_nearest
            Async_rpc_kernel.Async_rpc_kernel_private.default_handshake_timeout)
        ?heartbeat_config
        ?description
        where_to_connect
    =
    let finish_handshake_by =
      Time_ns.add
        (Time_ns.now ())
        (Time_ns.Span.of_span_float_round_nearest handshake_timeout)
    in
    Monitor.try_with (fun () ->
      Tcp.connect_sock ~timeout:handshake_timeout where_to_connect)
    >>=? fun sock ->
    match Socket.getpeername sock with
    | exception exn_could_be_raised_if_the_socket_is_diconnected_now ->
      Socket.shutdown sock `Both;
      Deferred.Result.fail exn_could_be_raised_if_the_socket_is_diconnected_now
    | sock_peername ->
      let description =
        match description with
        | None ->
          Info.create
            "Client connected via TCP"
            where_to_connect
            [%sexp_of: _ Tcp.Where_to_connect.t]
        | Some desc ->
          Info.tag_arg
            desc
            "via TCP"
            where_to_connect
            [%sexp_of: _ Tcp.Where_to_connect.t]
      in
      let handshake_timeout = Time_ns.diff finish_handshake_by (Time_ns.now ()) in
      let transport = make_transport (Socket.fd sock) ~max_message_size in
      (match implementations with
       | None ->
         let { Client_implementations.connection_state; implementations } =
           Client_implementations.null ()
         in
         Rpc_kernel.Connection.create
           transport
           ~handshake_timeout
           ?heartbeat_config
           ~implementations
           ~description
           ~connection_state
       | Some { Client_implementations.connection_state; implementations } ->
         Rpc_kernel.Connection.create
           transport
           ~handshake_timeout
           ?heartbeat_config
           ~implementations
           ~description
           ~connection_state)
      >>= (function
        | Ok t -> return (Ok (sock_peername, t))
        | Error _ as error -> Transport.close transport >>= fun () -> return error)
  ;;

  let client
        ?implementations
        ?max_message_size
        ?make_transport
        ?handshake_timeout
        ?heartbeat_config
        ?description
        where_to_connect
    =
    client'
      ?implementations
      ?max_message_size
      ?make_transport
      ?handshake_timeout
      ?heartbeat_config
      ?description
      where_to_connect
    >>|? snd
  ;;

  let with_client'
        ?implementations
        ?max_message_size
        ?make_transport
        ?handshake_timeout
        ?heartbeat_config
        where_to_connect
        f
    =
    client'
      ?implementations
      ?max_message_size
      ?make_transport
      ?handshake_timeout
      ?heartbeat_config
      where_to_connect
    >>=? fun (remote_server, t) ->
    try_with (fun () -> f ~remote_server t)
    >>= fun result ->
    close t ~reason:(Info.of_string "Rpc.Connection.with_client finished")
    >>| fun () -> result
  ;;

  let with_client
        ?implementations
        ?max_message_size
        ?make_transport
        ?handshake_timeout
        ?heartbeat_config
        where_to_connect
        f
    =
    with_client'
      ?implementations
      ?max_message_size
      ?make_transport
      ?handshake_timeout
      ?heartbeat_config
      where_to_connect
      (fun ~remote_server:_ -> f)
  ;;
end
OCaml

Innovation. Community. Security.