Skip to content

Commit

Permalink
Use timeout context for NewStream call
Browse files Browse the repository at this point in the history
  • Loading branch information
ligustah committed Oct 20, 2024
1 parent 8170ac4 commit 02d373e
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions crawler/crawler.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ type messageSender struct {

// SendRequest sends a peer a message and waits for its response
func (ms *messageSender) SendRequest(ctx context.Context, p peer.ID, pmes *pb.Message) (*pb.Message, error) {
s, err := ms.h.NewStream(ctx, p, ms.protocols...)
tctx, cancel := context.WithTimeout(ctx, ms.timeout)
defer cancel()

s, err := ms.h.NewStream(tctx, p, ms.protocols...)

Check warning on line 81 in crawler/crawler.go

View check run for this annotation

Codecov / codecov/patch

crawler/crawler.go#L78-L81

Added lines #L78 - L81 were not covered by tests
if err != nil {
return nil, err
}
Expand All @@ -86,8 +89,6 @@ func (ms *messageSender) SendRequest(ctx context.Context, p peer.ID, pmes *pb.Me
}

r := protoio.NewDelimitedReader(s, network.MessageSizeMax)
tctx, cancel := context.WithTimeout(ctx, ms.timeout)
defer cancel()
defer func() { _ = s.Close() }()

msg := new(pb.Message)
Expand Down

0 comments on commit 02d373e

Please sign in to comment.