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

identify: set stream deadlines for Identify and Identify Push streams #2382

Merged
merged 1 commit into from
Jun 19, 2023
Merged
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
11 changes: 6 additions & 5 deletions p2p/protocol/identify/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ const ServiceName = "libp2p.identify"

const maxPushConcurrency = 32

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

const (
legacyIDSize = 2 * 1024 // 2k Bytes
Expand Down Expand Up @@ -410,11 +409,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(), Timeout)
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(Timeout))

if err := s.SetProtocol(ID); err != nil {
log.Warnf("error setting identify protocol for stream: %s", err)
Expand All @@ -433,6 +435,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(Timeout))
ids.handleIdentifyResponse(s, true)
}

Expand Down Expand Up @@ -494,8 +497,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
12 changes: 6 additions & 6 deletions p2p/protocol/identify/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -742,10 +742,10 @@ func TestLargePushMessage(t *testing.T) {
}

func TestIdentifyResponseReadTimeout(t *testing.T) {
timeout := identify.StreamReadTimeout
identify.StreamReadTimeout = 100 * time.Millisecond
timeout := identify.Timeout
identify.Timeout = 100 * time.Millisecond
defer func() {
identify.StreamReadTimeout = timeout
identify.Timeout = timeout
}()

ctx, cancel := context.WithCancel(context.Background())
Expand Down Expand Up @@ -788,10 +788,10 @@ func TestIdentifyResponseReadTimeout(t *testing.T) {
}

func TestIncomingIDStreamsTimeout(t *testing.T) {
timeout := identify.StreamReadTimeout
identify.StreamReadTimeout = 100 * time.Millisecond
timeout := identify.Timeout
identify.Timeout = 100 * time.Millisecond
defer func() {
identify.StreamReadTimeout = timeout
identify.Timeout = timeout
}()

ctx, cancel := context.WithCancel(context.Background())
Expand Down