package async

  1. Overview
  2. Docs
Monadic concurrency library

Install

Dune Dependency

Authors

Maintainers

Sources

async-v0.15.0.tar.gz
sha256=0d3bb21c4bed1edca25ede5e4fbc76790e22379806183846baff475281a6d6a9

doc/src/async.async_rpc/rpc.ml.html

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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
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 On_exception = Rpc_kernel.On_exception
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

  let create
        ?implementations
        ~connection_state
        ?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 =
    match%map
      Deferred.Or_error.try_with
        ~run:
          `Schedule
        ~rest:`Log
        (fun () -> Reader.peek_bin_prot reader contains_magic_prefix)
    with
    | Error _ | Ok `Eof -> false
    | Ok (`Ok b) -> b
  ;;

  let with_close
        ?implementations
        ?max_message_size
        ?handshake_timeout
        ?heartbeat_config
        ?description
        ~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
      ?description
      ~connection_state
      (Transport.of_reader_writer reader writer ?max_message_size)
      ~dispatch_queries
      ~on_handshake_error
  ;;

  let server_with_close
        ?max_message_size
        ?handshake_timeout
        ?heartbeat_config
        ?description
        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
      ?description
      (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
          (Monitor.try_with
             ~run:
               `Schedule
             ~rest:`Log
             ~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 serve_with_transport
        ~handshake_timeout
        ~heartbeat_config
        ~implementations
        ~description
        ~connection_state
        ~on_handshake_error
        transport
    =
    let%bind res =
      collect_errors transport ~f:(fun () ->
        match%bind
          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
        with
        | 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)
    in
    let%map () = Transport.close transport in
    Result.ok_exn res
  ;;

  let make_serve_func
        serve_with_transport_handler
        ~implementations
        ~initial_connection_state
        ~where_to_listen
        ?max_connections
        ?backlog
        ?drop_incoming_connections
        ?time_source
        ?max_message_size
        ?make_transport
        ?handshake_timeout
        ?heartbeat_config
        ?auth
        ?(on_handshake_error = `Ignore)
        ?on_handler_error
        ()
    =
    serve_with_transport_handler
      ~where_to_listen
      ?max_connections
      ?backlog
      ?drop_incoming_connections
      ?time_source
      ?max_message_size
      ?make_transport
      ?auth
      ?on_handler_error
      (fun ~client_addr ~server_addr transport ->
         let description =
           let server_addr = (server_addr :> Socket.Address.t) in
           let client_addr = (client_addr :> Socket.Address.t) in
           Info.create_s
             [%message
               "TCP server"
                 (server_addr : Socket.Address.t)
                 (client_addr : Socket.Address.t)]
         in
         serve_with_transport
           ~handshake_timeout
           ~heartbeat_config
           ~implementations
           ~description
           ~connection_state:(fun conn -> initial_connection_state client_addr conn)
           ~on_handshake_error
           transport)
  ;;

  (* eta-expand [implementations] to avoid value restriction. *)
  let serve ~implementations = make_serve_func Rpc_transport.Tcp.serve ~implementations

  (* eta-expand [implementations] to avoid value restriction. *)
  let serve_inet ~implementations =
    make_serve_func Rpc_transport.Tcp.serve_inet ~implementations
  ;;

  let default_handshake_timeout_float =
    Time_ns.Span.to_span_float_round_nearest
      Async_rpc_kernel.Async_rpc_kernel_private.default_handshake_timeout
  ;;

  let client'
        ?implementations
        ?max_message_size
        ?make_transport
        ?handshake_timeout:(handshake_timeout_float = default_handshake_timeout_float)
        ?heartbeat_config
        ?description
        where_to_connect
    =
    let handshake_timeout =
      Time_ns.Span.of_span_float_round_nearest handshake_timeout_float
    in
    let finish_handshake_by = Time_ns.add (Time_ns.now ()) handshake_timeout in
    match%bind
      Rpc_transport.Tcp.connect
        ?max_message_size
        ?make_transport
        ~tcp_connect_timeout:handshake_timeout
        where_to_connect
    with
    | Error _ as error -> return error
    | Ok (transport, 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%bind rpc_connection =
        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
      in
      (match rpc_connection with
       | Ok t -> return (Ok (sock_peername, t))
       | Error _ as error ->
         let%bind () = Transport.close transport in
         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
        ?description
        where_to_connect
        f
    =
    client'
      ?implementations
      ?max_message_size
      ?make_transport
      ?handshake_timeout
      ?heartbeat_config
      ?description
      where_to_connect
    >>=? fun (remote_server, t) ->
    let%bind result =
      Monitor.try_with
        ~run:
          `Schedule
        ~rest:`Log
        (fun () -> f ~remote_server t)
    in
    let%map () = close t ~reason:(Info.of_string "Rpc.Connection.with_client finished") in
    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.