Skip to content
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

fix(identify): push should not dial a new connection #3035

Merged
merged 2 commits into from
Nov 18, 2024
Merged
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
34 changes: 24 additions & 10 deletions p2p/protocol/identify/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,8 @@ func (ids *idService) sendPushes(ctx context.Context) {
defer func() { <-sem }()
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
str, err := ids.Host.NewStream(ctx, c.RemotePeer(), IDPush)

str, err := newStreamAndNegotiate(ctx, c, IDPush)
if err != nil { // connection might have been closed recently
return
}
Expand Down Expand Up @@ -437,25 +438,38 @@ func (ids *idService) IdentifyWait(c network.Conn) <-chan struct{} {
return e.IdentifyWaitChan
}

func (ids *idService) identifyConn(c network.Conn) error {
ctx, cancel := context.WithTimeout(context.Background(), Timeout)
defer cancel()
// newStreamAndNegotiate opens a new stream on the given connection and negotiates the given protocol.
func newStreamAndNegotiate(ctx context.Context, c network.Conn, proto protocol.ID) (network.Stream, error) {
s, err := c.NewStream(network.WithAllowLimitedConn(ctx, "identify"))
if err != nil {
log.Debugw("error opening identify stream", "peer", c.RemotePeer(), "error", err)
return err
return nil, err
}
err = s.SetDeadline(time.Now().Add(Timeout))
if err != nil {
return nil, err
}
s.SetDeadline(time.Now().Add(Timeout))

if err := s.SetProtocol(ID); err != nil {
if err := s.SetProtocol(proto); err != nil {
log.Warnf("error setting identify protocol for stream: %s", err)
s.Reset()
_ = s.Reset()
}

// ok give the response to our handler.
if err := msmux.SelectProtoOrFail(ID, s); err != nil {
if err := msmux.SelectProtoOrFail(proto, s); err != nil {
log.Infow("failed negotiate identify protocol with peer", "peer", c.RemotePeer(), "error", err)
s.Reset()
_ = s.Reset()
return nil, err
}
return s, nil
}

func (ids *idService) identifyConn(c network.Conn) error {
ctx, cancel := context.WithTimeout(context.Background(), Timeout)
defer cancel()
s, err := newStreamAndNegotiate(network.WithAllowLimitedConn(ctx, "identify"), c, ID)
if err != nil {
log.Debugw("error opening identify stream", "peer", c.RemotePeer(), "error", err)
return err
}

Expand Down
Loading