From 3a3d0848adaaf20b481a499920b8f2a6e91db0f0 Mon Sep 17 00:00:00 2001 From: OCamlPro-Iguernlala Date: Thu, 19 Jul 2018 15:25:48 +0200 Subject: [PATCH] P2p: randomly allow one additional incoming connection If 'max_connections' is already reached, this modification will enable one additional incoming connection if 'Random.bool()' is true. This will allow a kind of connections diversification by enabling a call to P2p_maintenance.too_many_connections, which will close some connection with a low score (depending on the chosen scoring criterion) --- src/bin_node/main.ml | 1 + src/lib_p2p/p2p_pool.ml | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/bin_node/main.ml b/src/bin_node/main.ml index 77a12535f..75c566cc9 100644 --- a/src/bin_node/main.ml +++ b/src/bin_node/main.ml @@ -67,6 +67,7 @@ let commands = [ ] let () = + Random.self_init () ; match Cmdliner.Term.eval_choice (term, info) commands with | `Error _ -> exit 1 | `Help -> exit 0 diff --git a/src/lib_p2p/p2p_pool.ml b/src/lib_p2p/p2p_pool.ml index c4bfd3cb8..0ca8e1e4a 100644 --- a/src/lib_p2p/p2p_pool.ml +++ b/src/lib_p2p/p2p_pool.ml @@ -1129,8 +1129,14 @@ and swap pool conn current_peer_id new_point = let accept pool fd point = log pool (Incoming_connection point) ; + let max_active_conns = + if Random.bool () then + (* randomly allow one additional incoming connection *) + pool.config.max_connections + 1 + else + pool.config.max_connections in if pool.config.max_incoming_connections <= P2p_point.Table.length pool.incoming - || pool.config.max_connections <= active_connections pool + || max_active_conns <= active_connections pool (* silently ignore banned points *) || (P2p_acl.banned_addr pool.acl (fst point)) then Lwt.async (fun () -> P2p_fd.close fd)