Skip to content

Commit

Permalink
webrtc: fail Write early if deadline has exceeded before the call (#2578
Browse files Browse the repository at this point in the history
)
  • Loading branch information
sukunrt committed Oct 18, 2023
1 parent cd9e7cb commit edbd0e0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 4 additions & 0 deletions p2p/transport/webrtc/stream_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func (s *stream) Write(b []byte) (int, error) {
s.readLoopOnce.Do(s.spawnControlMessageReader)
}

if !s.writeDeadline.IsZero() && time.Now().After(s.writeDeadline) {
return 0, os.ErrDeadlineExceeded
}

var writeDeadlineTimer *time.Timer
defer func() {
if writeDeadlineTimer != nil {
Expand Down
8 changes: 7 additions & 1 deletion p2p/transport/webrtc/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,10 +402,16 @@ func TestTransportWebRTC_Deadline(t *testing.T) {
stream, err := conn.OpenStream(context.Background())
require.NoError(t, err)

stream.SetWriteDeadline(time.Now().Add(200 * time.Millisecond))
stream.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))
largeBuffer := make([]byte, 2*1024*1024)
_, err = stream.Write(largeBuffer)
require.ErrorIs(t, err, os.ErrDeadlineExceeded)

stream.SetWriteDeadline(time.Now().Add(-200 * time.Millisecond))
smallBuffer := make([]byte, 1024)
_, err = stream.Write(smallBuffer)
require.ErrorIs(t, err, os.ErrDeadlineExceeded)

})
}

Expand Down

0 comments on commit edbd0e0

Please sign in to comment.