From 5046ea5626dc4a66a1ab5073c99009fbded57647 Mon Sep 17 00:00:00 2001 From: Csaba Kiraly Date: Sat, 3 May 2025 10:38:42 +0200 Subject: [PATCH] p2p/server: allow setting MaxPeers dynamically Signed-off-by: Csaba Kiraly --- p2p/dial.go | 4 ++++ p2p/server.go | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/p2p/dial.go b/p2p/dial.go index 225709427c..3b2c84d445 100644 --- a/p2p/dial.go +++ b/p2p/dial.go @@ -185,6 +185,10 @@ func newDialScheduler(config dialConfig, it enode.Iterator, setupFunc dialSetupF return d } +func (d *dialScheduler) setMaxDialPeers(maxDialPeers int) { + d.maxDialPeers = maxDialPeers +} + // stop shuts down the dialer, canceling all current dial tasks. func (d *dialScheduler) stop() { d.cancel() diff --git a/p2p/server.go b/p2p/server.go index d9105976dd..6b22c1e45e 100644 --- a/p2p/server.go +++ b/p2p/server.go @@ -420,6 +420,13 @@ func (srv *Server) Start() (err error) { return nil } +func (srv *Server) SetMaxPeers(max int) { + srv.lock.Lock() + defer srv.lock.Unlock() + srv.MaxPeers = max + srv.dialsched.setMaxDialPeers(srv.MaxDialedConns()) +} + func (srv *Server) setupLocalNode() error { // Create the devp2p handshake. pubkey := crypto.FromECDSAPub(&srv.PrivateKey.PublicKey)