package async
Monadic concurrency library
Install
Dune Dependency
Authors
Maintainers
Sources
v0.17.0.tar.gz
sha256=3edbaa62a7ea910da0d5c06974da9b2b7e14a9e2648596bc7c9f09c89970b5f8
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 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
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 How_to_recognise_errors = Rpc_kernel.How_to_recognise_errors module Implementation = Rpc_kernel.Implementation module Implementations = Rpc_kernel.Implementations module On_exception = Rpc_kernel.On_exception 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 ?identification reader writer = create ?implementations ~connection_state ?handshake_timeout: (Option.map handshake_timeout ~f:Time_ns.Span.of_span_float_round_nearest) ?heartbeat_config ?description ?identification (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 let serve_with_transport ?identification transport ~handshake_timeout ~heartbeat_config ~implementations ~description ~connection_state ~on_handshake_error ~client_addr = 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 ?identification ~implementations ~description ~connection_state transport with | Ok t -> close_finished t | Error handshake_error -> (match on_handshake_error with | `Call f -> f client_addr handshake_error | `Raise -> raise handshake_error | `Ignore -> ()); Deferred.unit) in let%map () = Transport.close transport in Result.ok_exn res ;; let connection_description ?description ~server_addr ~client_addr () = let server_addr = (server_addr :> Socket.Address.t) in let client_addr = (client_addr :> Socket.Address.t) in let connection_description = Info.create_s [%message "TCP server" (server_addr : Socket.Address.t) (client_addr : Socket.Address.t)] in match description with | None -> connection_description | Some description -> Info.of_list [ description; connection_description ] ;; let default_on_handshake_error = `Ignore 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 = default_on_handshake_error) ?on_handler_error ?description ?identification () = 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 -> serve_with_transport ~handshake_timeout ~heartbeat_config ~implementations ~description:(connection_description ?description ~server_addr ~client_addr ()) ~connection_state:(fun conn -> initial_connection_state client_addr conn) ~on_handshake_error ~client_addr ?identification 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 serve_unix ~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 = default_on_handshake_error) ?on_handler_error ?description ?identification () = Rpc_transport.Tcp.serve_unix ~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 peer_creds transport -> serve_with_transport ~handshake_timeout ~heartbeat_config ~implementations ~description:(connection_description ?description ~server_addr ~client_addr ()) ~connection_state:(fun conn -> initial_connection_state client_addr peer_creds conn) ~on_handshake_error ~client_addr ?identification transport) ;; 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 ?identification 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.of_list [ desc ; Info.create_s [%message "via TCP" ~_:(where_to_connect : _ 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 (T { connection_state; implementations }) = Client_implementations.null () in Rpc_kernel.Connection.create transport ~handshake_timeout ?heartbeat_config ?identification ~implementations ~description ~connection_state | Some (Client_implementations.T { connection_state; implementations }) -> Rpc_kernel.Connection.create transport ~handshake_timeout ?heartbeat_config ?identification ~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 ?identification where_to_connect = client' ?implementations ?max_message_size ?make_transport ?handshake_timeout ?heartbeat_config ?description ?identification where_to_connect >>|? snd ;; let with_client' ?implementations ?max_message_size ?make_transport ?handshake_timeout ?heartbeat_config ?description ?identification where_to_connect f = client' ?implementations ?max_message_size ?make_transport ?handshake_timeout ?heartbeat_config ?description ?identification 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 ?description ?identification where_to_connect f = with_client' ?implementations ?max_message_size ?make_transport ?handshake_timeout ?heartbeat_config ?description ?identification where_to_connect (fun ~remote_server:_ -> f) ;; end module For_debugging = struct let default_path = lazy ("/dev/shm/rpc-message-reader-errors__pid_" ^ Pid.to_string (Core_unix.getpid ())) ;; let path_override = ref None let dump_deserialization_error buf ~pos = (* Dump the message synchronously, this is this ok as this is being used to debug why the application is seeing this error, and is likely about to shut down from the failed RPC anyway. *) let open_file () = let path = match !path_override with | None -> force default_path | Some path -> path in ( Core_unix.openfile path ~mode: [ O_CREAT ; O_WRONLY ; (let ensure_we_dont_fill_up_filesystem_if_program_is_emitting_many_of_these = Core_unix.O_TRUNC in ensure_we_dont_fill_up_filesystem_if_program_is_emitting_many_of_these) ] , path ) in let fd, path = try open_file () with | exn -> if Option.is_none !path_override then raise exn else ( path_override := None; open_file ()) in let result = match Bigstring_unix.really_write fd buf with | () -> sprintf "Dump of buffer written to %s. This message begins at offset %d" path pos | exception e -> sprintf "Tried and failed to dump internal buffer to %s (offset %d): %s" path pos (Exn.to_string e) in Core_unix.close fd; result ;; let enable_dumping_buffers_on_deserialization_errors ?path_override:override () = (match override with | None -> () | Some override -> path_override := Some override); Async_rpc_kernel.Async_rpc_kernel_private.Util.dumper_for_deserialization_errors := dump_deserialization_error ;; let () = match Sys.getenv "ASYNC_RPC_DEBUG_DUMP_DESERIALIZATION_ERRORS" with | None -> (* support for old env var *) (match Sys.getenv "ASYNC_RPC_DEBUG_DUMP_MESSAGE_LENGTH_ERRORS" with | None | Some "" -> () | Some _ -> enable_dumping_buffers_on_deserialization_errors ()) | Some dump_to -> let path_override = match dump_to with | "" | "1" -> None | dump_to when String.contains dump_to '/' -> Some dump_to | dump_to -> Some ("/dev/shm" ^/ dump_to) in enable_dumping_buffers_on_deserialization_errors ?path_override () ;; end
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>