Skip to content

Commit

Permalink
fix: basichost: Use NegotiationTimeout as fallback timeout for NewStream
Browse files Browse the repository at this point in the history
in case no timeout was provided via context.
  • Loading branch information
MarcoPolo committed Oct 23, 2024
1 parent 83d458c commit d069252
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions p2p/host/basic/basic_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,10 @@ type HostOpts struct {
// MultistreamMuxer is essential for the *BasicHost and will use a sensible default value if omitted.
MultistreamMuxer *msmux.MultistreamMuxer[protocol.ID]

// NegotiationTimeout determines the read and write timeouts on streams.
// If 0 or omitted, it will use DefaultNegotiationTimeout.
// If below 0, timeouts on streams will be deactivated.
// NegotiationTimeout determines the read and write timeouts when negotiating
// protocols for streams. If 0 or omitted, it will use
// DefaultNegotiationTimeout. If below 0, timeouts on streams will be
// deactivated.
NegotiationTimeout time.Duration

// AddrsFactory holds a function which can be used to override or filter the result of Addrs.
Expand Down Expand Up @@ -689,6 +690,14 @@ func (h *BasicHost) RemoveStreamHandler(pid protocol.ID) {
// to create one. If ProtocolID is "", writes no header.
// (Thread-safe)
func (h *BasicHost) NewStream(ctx context.Context, p peer.ID, pids ...protocol.ID) (str network.Stream, strErr error) {
if _, ok := ctx.Deadline(); !ok {
if h.negtimeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, h.negtimeout)
defer cancel()
}
}

// If the caller wants to prevent the host from dialing, it should use the NoDial option.
if nodial, _ := network.GetNoDial(ctx); !nodial {
err := h.Connect(ctx, peer.AddrInfo{ID: p})
Expand Down

0 comments on commit d069252

Please sign in to comment.