Skip to content

Commit

Permalink
Merge pull request #5477 from oasisprotocol/peternose/stable/23.0.x/b…
Browse files Browse the repository at this point in the history
…ackport-5476

[BACKPORT/23.0.x] go/p2p/discovery: Close only idle connections to seed node
  • Loading branch information
peternose authored Nov 27, 2023
2 parents 23652ad + b99fb4a commit 17230a5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
1 change: 1 addition & 0 deletions .changelog/5476.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go/p2p/discovery: Close only idle connections to seed node
12 changes: 6 additions & 6 deletions go/p2p/discovery/bootstrap/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ func (c *client) Advertise(ctx context.Context, ns string, _ ...discovery.Option

pf.RecordSuccess()

// Close connections after every call because requests to the seed node are infrequent.
if err = c.rc.Close(c.seed.ID); err != nil {
c.logger.Warn("failed to close connections to seed node",
// Try to close connections after every call because requests to the seed node are infrequent.
if err = c.rc.CloseIdle(c.seed.ID); err != nil {
c.logger.Warn("failed to close idle connections to seed node",
"err", err,
)
}
Expand Down Expand Up @@ -246,9 +246,9 @@ func (c *client) fetchPeers(ctx context.Context, ns string, limit int) []peer.Ad
pf.RecordFailure()
}

// Close connections after every call because requests to the seed node are infrequent.
if err := c.rc.Close(c.seed.ID); err != nil {
c.logger.Warn("failed to close connections to seed node",
// Try to close connections after every call because requests to the seed node are infrequent.
if err = c.rc.CloseIdle(c.seed.ID); err != nil {
c.logger.Warn("failed to close idle connections to seed node",
"err", err,
)
}
Expand Down
16 changes: 16 additions & 0 deletions go/p2p/rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ type Client interface {
// Close closes all connections to the given peer.
Close(peerID core.PeerID) error

// CloseIdle closes all connections to the given peer that have no open streams.
CloseIdle(peerID core.PeerID) error

// RegisterListener subscribes the listener to the client notification events.
// If the listener is already registered this is a noop operation.
RegisterListener(l ClientListener)
Expand Down Expand Up @@ -545,6 +548,19 @@ func (c *client) Close(peerID core.PeerID) error {
return errs
}

// Implements Client.
func (c *client) CloseIdle(peerID core.PeerID) error {
var errs error
for _, conn := range c.host.Network().ConnsToPeer(peerID) {
if len(conn.GetStreams()) > 0 {
continue
}
err := conn.Close()
errs = errors.Join(errs, err)
}
return errs
}

// Implements Client.
func (c *client) RegisterListener(l ClientListener) {
c.listeners.Lock()
Expand Down
7 changes: 7 additions & 0 deletions go/p2p/rpc/nop.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ func (c *nopClient) Close(
return nil
}

// Implements Client.
func (c *nopClient) CloseIdle(
peer.ID,
) error {
return nil
}

// Implements Client.
func (c *nopClient) RegisterListener(ClientListener) {}

Expand Down

0 comments on commit 17230a5

Please sign in to comment.