P2p: postpone the first maintenance step.

This commit is contained in:
Grégoire Henry 2017-01-23 11:10:02 +01:00
parent 5ee3581d60
commit 2da0c83b5a
5 changed files with 23 additions and 19 deletions

View File

@ -261,9 +261,8 @@ type ('msg, 'meta) t = {
} }
type ('msg, 'meta) net = ('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.create ~config ~limits meta_cfg msg_cfg >>= fun net ->
Real.maintain net () >>= fun () ->
Lwt.return { Lwt.return {
gid = Real.gid net ; gid = Real.gid net ;
maintain = Real.maintain net ; maintain = Real.maintain net ;

View File

@ -124,7 +124,7 @@ type ('msg, 'meta) net = ('msg, 'meta) t
val faked_network : ('msg, 'meta) net val faked_network : ('msg, 'meta) net
(** Main network initialisation function *) (** Main network initialisation function *)
val bootstrap : val create :
config:config -> limits:limits -> config:config -> limits:limits ->
'meta meta_config -> 'msg message_config -> ('msg, 'meta) net Lwt.t 'meta meta_config -> 'msg message_config -> ('msg, 'meta) net Lwt.t

View File

@ -211,7 +211,10 @@ let init_p2p net_params =
Lwt.return Tezos_p2p.faked_network Lwt.return Tezos_p2p.faked_network
| Some (config, limits) -> | Some (config, limits) ->
lwt_log_notice "bootstraping network..." >>= fun () -> 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 let create
~genesis ~store_root ~context_root ?test_protocol ?patch_context net_params = ~genesis ~store_root ~context_root ?test_protocol ?patch_context net_params =
@ -234,11 +237,12 @@ let create
end >>=? fun global_net -> end >>=? fun global_net ->
Validator.activate validator global_net >>= fun global_validator -> Validator.activate validator global_net >>= fun global_validator ->
let cleanup () = let cleanup () =
Tezos_p2p.shutdown p2p >>= fun () ->
Lwt.join [ Validator.shutdown validator ; Lwt.join [ Validator.shutdown validator ;
Discoverer.shutdown discoverer ] >>= fun () -> Discoverer.shutdown discoverer ] >>= fun () ->
State.store state State.store state
in in
let canceler = Lwt_utils.Canceler.create () in
lwt_log_info "starting worker..." >>= fun () -> lwt_log_info "starting worker..." >>= fun () ->
let worker = let worker =
let handle_msg peer msg = let handle_msg peer msg =
@ -249,22 +253,23 @@ let create
Lwt.return_unit Lwt.return_unit
in in
let rec worker_loop () = 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 () -> handle_msg peer msg >>= fun () ->
worker_loop () in worker_loop () in
Lwt.catch worker_loop () >>= function
worker_loop | Error [Lwt_utils.Canceled] | Ok () ->
(function cleanup ()
| Queue.Empty -> cleanup () | Error err ->
| exn -> lwt_log_error
lwt_log_error "unexpected exception in worker\n%s" "@[Unexpected error in worker@ %a@]"
(Printexc.to_string exn) >>= fun () -> pp_print_error err >>= fun () ->
Tezos_p2p.shutdown p2p >>= fun () -> cleanup ()
cleanup ())
in in
let shutdown () = let shutdown () =
lwt_log_info "stopping worker..." >>= fun () -> lwt_log_info "stopping worker..." >>= fun () ->
Tezos_p2p.shutdown p2p >>= fun () -> Lwt_utils.Canceler.cancel canceler >>= fun () ->
worker >>= fun () -> worker >>= fun () ->
lwt_log_info "stopped" lwt_log_info "stopped"
in in

View File

@ -106,8 +106,8 @@ and msg_cfg : _ P2p.message_config = {
type net = (Message.t, Metadata.t) P2p.net type net = (Message.t, Metadata.t) P2p.net
let bootstrap ~config ~limits = let create ~config ~limits =
P2p.bootstrap ~config ~limits meta_cfg msg_cfg P2p.create ~config ~limits meta_cfg msg_cfg
let broadcast = P2p.broadcast let broadcast = P2p.broadcast
let try_send = P2p.try_send let try_send = P2p.try_send

View File

@ -8,7 +8,7 @@ type net
val faked_network : net val faked_network : net
(** Main network initialisation function *) (** 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 *) (** A maintenance operation : try and reach the ideal number of peers *)
val maintain : net -> unit Lwt.t val maintain : net -> unit Lwt.t