-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
handle timeouts using i/o deadlines.
Both initiator and responder now time out if reads and/or writes are blocked for 30 seconds (default negotiation timeout). Introduces two tests to validate the new behaviour. Fixes #47.
- Loading branch information
Showing
4 changed files
with
124 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package multistream | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"time" | ||
) | ||
|
||
// NegotiationTimeout is the maximum time a protocol negotiation atempt is | ||
// allowed to be inflight before it fails. | ||
var NegotiationTimeout = 30 * time.Second | ||
|
||
// setDeadline attempts to set a read and write deadline on the underlying IO | ||
// object, if it supports it. | ||
func setDeadline(rwc io.ReadWriteCloser) (func(), error) { | ||
// rwc could be: | ||
// - a net.Conn or a libp2p Stream, both of which satisfy this interface. | ||
// - something else (e.g. testing), in which case we skip over setting | ||
// a deadline. | ||
type deadline interface { | ||
SetDeadline(time.Time) error | ||
} | ||
if d, ok := rwc.(deadline); ok { | ||
if err := d.SetDeadline(time.Now().Add(NegotiationTimeout)); err != nil { | ||
// this should not happen; if it does, something is broken and we | ||
// should fail immediately. | ||
return nil, fmt.Errorf("failed while setting a deadline: %w", err) | ||
} | ||
return func() { d.SetDeadline(time.Time{}) }, nil | ||
} | ||
return func() {}, nil | ||
} |