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.
This commit is contained in:
Felix Lange 2020-02-09 15:47:40 +01:00
parent 463842c950
commit af30b4d982

View file

@ -620,15 +620,19 @@ func (srv *Server) maxInboundConns() int {
return srv.MaxPeers - srv.maxDialedConns() return srv.MaxPeers - srv.maxDialedConns()
} }
func (srv *Server) maxDialedConns() int { func (srv *Server) maxDialedConns() (limit int) {
if srv.NoDial { if srv.NoDial || srv.MaxPeers == 0 {
return 0 return 0
} }
r := srv.DialRatio if srv.DialRatio == 0 {
if r == 0 { limit = srv.MaxPeers / defaultDialRatio
r = defaultDialRatio } else {
limit = srv.MaxPeers / srv.DialRatio
} }
return srv.MaxPeers / r if limit == 0 {
limit = 1
}
return limit
} }
func (srv *Server) setupListening() error { func (srv *Server) setupListening() error {