Skip to content

Commit

Permalink
identify: set stream deadlines for Identify and Identify Push streams
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Jun 19, 2023
1 parent 91f34d4 commit 8fdd3ee
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions p2p/protocol/identify/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,11 @@ const ServiceName = "libp2p.identify"

const maxPushConcurrency = 32

// StreamReadTimeout is the read timeout on all incoming Identify family streams.
var StreamReadTimeout = 60 * time.Second

const (
legacyIDSize = 2 * 1024 // 2k Bytes
signedIDSize = 8 * 1024 // 8K
maxMessages = 10
legacyIDSize = 2 * 1024 // 2k Bytes
signedIDSize = 8 * 1024 // 8K
maxMessages = 10
identifyTimeout = 60 * time.Second // imeout on all incoming Identify interactions
)

var defaultUserAgent = "github.com/libp2p/go-libp2p"
Expand Down Expand Up @@ -410,11 +408,14 @@ func (ids *idService) IdentifyWait(c network.Conn) <-chan struct{} {
}

func (ids *idService) identifyConn(c network.Conn) error {
s, err := c.NewStream(network.WithUseTransient(context.TODO(), "identify"))
ctx, cancel := context.WithTimeout(context.Background(), identifyTimeout)
defer cancel()
s, err := c.NewStream(network.WithUseTransient(ctx, "identify"))
if err != nil {
log.Debugw("error opening identify stream", "peer", c.RemotePeer(), "error", err)
return err
}
s.SetDeadline(time.Now().Add(identifyTimeout))

if err := s.SetProtocol(ID); err != nil {
log.Warnf("error setting identify protocol for stream: %s", err)
Expand All @@ -433,6 +434,7 @@ func (ids *idService) identifyConn(c network.Conn) error {

// handlePush handles incoming identify push streams
func (ids *idService) handlePush(s network.Stream) {
s.SetDeadline(time.Now().Add(identifyTimeout))
ids.handleIdentifyResponse(s, true)
}

Expand Down Expand Up @@ -494,8 +496,6 @@ func (ids *idService) handleIdentifyResponse(s network.Stream, isPush bool) erro
}
defer s.Scope().ReleaseMemory(signedIDSize)

_ = s.SetReadDeadline(time.Now().Add(StreamReadTimeout))

c := s.Conn()

r := pbio.NewDelimitedReader(s, signedIDSize)
Expand Down

0 comments on commit 8fdd3ee

Please sign in to comment.