From 4ae549a8e2037826845f096cd2a702f6ffd412d4 Mon Sep 17 00:00:00 2001 From: Sukun Date: Thu, 21 Sep 2023 18:04:16 +0530 Subject: [PATCH] webrtc: fail Write early if deadline has exceeded before the call (#2578) --- p2p/transport/webrtc/stream_write.go | 4 ++++ p2p/transport/webrtc/transport_test.go | 8 +++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/p2p/transport/webrtc/stream_write.go b/p2p/transport/webrtc/stream_write.go index c7eb3bf7a4..698af9c4d6 100644 --- a/p2p/transport/webrtc/stream_write.go +++ b/p2p/transport/webrtc/stream_write.go @@ -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 { diff --git a/p2p/transport/webrtc/transport_test.go b/p2p/transport/webrtc/transport_test.go index 19d39ba282..7f4df94fc1 100644 --- a/p2p/transport/webrtc/transport_test.go +++ b/p2p/transport/webrtc/transport_test.go @@ -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) + }) }