Skip to content

eth: allow changing maxPeers on the fly #31973

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,12 @@
return nil
}

func (s *Ethereum) setMaxPeers(max int) {
s.p2pServer.SetMaxPeers(max)
s.handler.SetMaxPeers(max)
s.dropper.SetMaxPeers(s.p2pServer.MaxDialedConns(), s.p2pServer.MaxInboundConns())
}

Check failure on line 417 in eth/backend.go

View workflow job for this annotation

GitHub Actions / Lint

func (*Ethereum).setMaxPeers is unused (unused)

func (s *Ethereum) newChainView(head *types.Header) *filtermaps.ChainView {
if head == nil {
return nil
Expand Down
5 changes: 5 additions & 0 deletions eth/dropper.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ func newDropper(maxDialPeers, maxInboundPeers int) *dropper {
return cm
}

func (cm *dropper) SetMaxPeers(maxDialPeers, maxInboundPeers int) {
cm.maxDialPeers = maxDialPeers
cm.maxInboundPeers = maxInboundPeers
}

// Start the dropper.
func (cm *dropper) Start(srv *p2p.Server, syncingFunc getSyncingFunc) {
cm.peersFunc = srv.Peers
Expand Down
4 changes: 4 additions & 0 deletions eth/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,10 @@ func (h *handler) unregisterPeer(id string) {
}
}

func (h *handler) SetMaxPeers(maxPeers int) {
h.maxPeers = maxPeers
}

func (h *handler) Start(maxPeers int) {
h.maxPeers = maxPeers

Expand Down
4 changes: 4 additions & 0 deletions p2p/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions p2p/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading