Skip to content

Commit

Permalink
send IDPush on given conn
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo committed Nov 12, 2024
1 parent c9950e9 commit b53c174
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions p2p/protocol/identify/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,7 @@ func (ids *idService) sendPushes(ctx context.Context) {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

// We only want to send an identify push if we already have an open
// connection.
ctx = network.WithNoDial(ctx, "id push")
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 @@ -441,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

0 comments on commit b53c174

Please sign in to comment.