From 2da0c83b5a898d986218011f4f775b5fcf305206 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Mon, 23 Jan 2017 11:10:02 +0100 Subject: [PATCH] P2p: postpone the first maintenance step. --- src/node/net/p2p.ml | 3 +-- src/node/net/p2p.mli | 2 +- src/node/shell/node.ml | 31 ++++++++++++++++++------------- src/node/shell/tezos_p2p.ml | 4 ++-- src/node/shell/tezos_p2p.mli | 2 +- 5 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/node/net/p2p.ml b/src/node/net/p2p.ml index b5713b0a1..94be084e8 100644 --- a/src/node/net/p2p.ml +++ b/src/node/net/p2p.ml @@ -261,9 +261,8 @@ type ('msg, 'meta) t = { } type ('msg, 'meta) net = ('msg, 'meta) t -let bootstrap ~config ~limits meta_cfg msg_cfg = +let create ~config ~limits meta_cfg msg_cfg = Real.create ~config ~limits meta_cfg msg_cfg >>= fun net -> - Real.maintain net () >>= fun () -> Lwt.return { gid = Real.gid net ; maintain = Real.maintain net ; diff --git a/src/node/net/p2p.mli b/src/node/net/p2p.mli index 06b3dc93e..b74f36fe1 100644 --- a/src/node/net/p2p.mli +++ b/src/node/net/p2p.mli @@ -124,7 +124,7 @@ type ('msg, 'meta) net = ('msg, 'meta) t val faked_network : ('msg, 'meta) net (** Main network initialisation function *) -val bootstrap : +val create : config:config -> limits:limits -> 'meta meta_config -> 'msg message_config -> ('msg, 'meta) net Lwt.t diff --git a/src/node/shell/node.ml b/src/node/shell/node.ml index e54e5d678..aa8d16c55 100644 --- a/src/node/shell/node.ml +++ b/src/node/shell/node.ml @@ -211,7 +211,10 @@ let init_p2p net_params = Lwt.return Tezos_p2p.faked_network | Some (config, limits) -> lwt_log_notice "bootstraping network..." >>= fun () -> - Tezos_p2p.bootstrap config limits + Tezos_p2p.create config limits >>= fun p2p -> + Lwt.async (fun () -> Tezos_p2p.maintain p2p) ; + Lwt.return p2p + let create ~genesis ~store_root ~context_root ?test_protocol ?patch_context net_params = @@ -234,11 +237,12 @@ let create end >>=? fun global_net -> Validator.activate validator global_net >>= fun global_validator -> let cleanup () = + Tezos_p2p.shutdown p2p >>= fun () -> Lwt.join [ Validator.shutdown validator ; Discoverer.shutdown discoverer ] >>= fun () -> State.store state in - + let canceler = Lwt_utils.Canceler.create () in lwt_log_info "starting worker..." >>= fun () -> let worker = let handle_msg peer msg = @@ -249,22 +253,23 @@ let create Lwt.return_unit in let rec worker_loop () = - Tezos_p2p.recv p2p >>= fun (peer, msg) -> + Lwt_utils.protect ~canceler begin fun () -> + Tezos_p2p.recv p2p >>= return + end >>=? fun (peer, msg) -> handle_msg peer msg >>= fun () -> worker_loop () in - Lwt.catch - worker_loop - (function - | Queue.Empty -> cleanup () - | exn -> - lwt_log_error "unexpected exception in worker\n%s" - (Printexc.to_string exn) >>= fun () -> - Tezos_p2p.shutdown p2p >>= fun () -> - cleanup ()) + worker_loop () >>= function + | Error [Lwt_utils.Canceled] | Ok () -> + cleanup () + | Error err -> + lwt_log_error + "@[Unexpected error in worker@ %a@]" + pp_print_error err >>= fun () -> + cleanup () in let shutdown () = lwt_log_info "stopping worker..." >>= fun () -> - Tezos_p2p.shutdown p2p >>= fun () -> + Lwt_utils.Canceler.cancel canceler >>= fun () -> worker >>= fun () -> lwt_log_info "stopped" in diff --git a/src/node/shell/tezos_p2p.ml b/src/node/shell/tezos_p2p.ml index 0b4ef48d2..26ffe1d3a 100644 --- a/src/node/shell/tezos_p2p.ml +++ b/src/node/shell/tezos_p2p.ml @@ -106,8 +106,8 @@ and msg_cfg : _ P2p.message_config = { type net = (Message.t, Metadata.t) P2p.net -let bootstrap ~config ~limits = - P2p.bootstrap ~config ~limits meta_cfg msg_cfg +let create ~config ~limits = + P2p.create ~config ~limits meta_cfg msg_cfg let broadcast = P2p.broadcast let try_send = P2p.try_send diff --git a/src/node/shell/tezos_p2p.mli b/src/node/shell/tezos_p2p.mli index db1344baa..0f1111e40 100644 --- a/src/node/shell/tezos_p2p.mli +++ b/src/node/shell/tezos_p2p.mli @@ -8,7 +8,7 @@ type net val faked_network : net (** Main network initialisation function *) -val bootstrap : config:config -> limits:limits -> net Lwt.t +val create : config:config -> limits:limits -> net Lwt.t (** A maintenance operation : try and reach the ideal number of peers *) val maintain : net -> unit Lwt.t