From af30b4d982f55cdac623466f5c5a9cb36a75556d Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Sun, 9 Feb 2020 15:47:40 +0100 Subject: [PATCH] p2p: fix maxDialedConns for maxpeers < dialRatio Always allocate at least one dial slot unless dialing is disabled using NoDial or MaxPeers == 0. Most importantly, this fixes MaxPeers == 1 to dedicate the sole slot to dialing instead of listening. --- p2p/server.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/p2p/server.go b/p2p/server.go index 4545206363..1d4ac970a5 100644 --- a/p2p/server.go +++ b/p2p/server.go @@ -620,15 +620,19 @@ func (srv *Server) maxInboundConns() int { return srv.MaxPeers - srv.maxDialedConns() } -func (srv *Server) maxDialedConns() int { - if srv.NoDial { +func (srv *Server) maxDialedConns() (limit int) { + if srv.NoDial || srv.MaxPeers == 0 { return 0 } - r := srv.DialRatio - if r == 0 { - r = defaultDialRatio + if srv.DialRatio == 0 { + limit = srv.MaxPeers / defaultDialRatio + } else { + limit = srv.MaxPeers / srv.DialRatio } - return srv.MaxPeers / r + if limit == 0 { + limit = 1 + } + return limit } func (srv *Server) setupListening() error {