From a131af34392eef02407825e75eff2358ea682032 Mon Sep 17 00:00:00 2001 From: Andy Wang Date: Fri, 30 Aug 2019 20:39:23 +0100 Subject: [PATCH] Remove unnecessary Len check --- internal/multiplex/datagramBuffer.go | 6 ------ internal/multiplex/datagramBuffer_test.go | 2 +- internal/multiplex/recvBuffer.go | 1 - internal/multiplex/stream.go | 26 ++++++----------------- internal/multiplex/streamBuffer.go | 4 ---- 5 files changed, 7 insertions(+), 32 deletions(-) diff --git a/internal/multiplex/datagramBuffer.go b/internal/multiplex/datagramBuffer.go index 3689428..6c1e9fe 100644 --- a/internal/multiplex/datagramBuffer.go +++ b/internal/multiplex/datagramBuffer.go @@ -80,9 +80,3 @@ func (d *datagramBuffer) Close() error { d.rwCond.Broadcast() return nil } - -func (d *datagramBuffer) Len() int { - d.rwCond.L.Lock() - defer d.rwCond.L.Unlock() - return len(d.buf) -} diff --git a/internal/multiplex/datagramBuffer_test.go b/internal/multiplex/datagramBuffer_test.go index 6b31e75..0154ec8 100644 --- a/internal/multiplex/datagramBuffer_test.go +++ b/internal/multiplex/datagramBuffer_test.go @@ -44,7 +44,7 @@ func TestDatagramBuffer_RW(t *testing.T) { "got", b2, ) } - if pipe.Len() != 0 { + if len(pipe.buf) != 0 { t.Error("buf len is not 0 after finished reading") return } diff --git a/internal/multiplex/recvBuffer.go b/internal/multiplex/recvBuffer.go index 49a1f07..146224c 100644 --- a/internal/multiplex/recvBuffer.go +++ b/internal/multiplex/recvBuffer.go @@ -5,5 +5,4 @@ import "io" type recvBuffer interface { io.ReadCloser Write(Frame) error - Len() int } diff --git a/internal/multiplex/stream.go b/internal/multiplex/stream.go index e17d4b2..a98e586 100644 --- a/internal/multiplex/stream.go +++ b/internal/multiplex/stream.go @@ -15,13 +15,6 @@ import ( var ErrBrokenStream = errors.New("broken stream") -// ReadWriteCloseLener is io.ReadWriteCloser with Len() -// used for bufferedPipe and datagramBuffer -type ReadWriteCloseLener interface { - io.ReadWriteCloser - Len() int -} - type Stream struct { id uint32 @@ -83,20 +76,13 @@ func (s *Stream) Read(buf []byte) (n int, err error) { } } - if s.isClosed() { - // TODO: Len check may not be necessary as this can be offloaded to buffer implementation - if s.recvBuf.Len() == 0 { - return 0, ErrBrokenStream - } else { - n, err = s.recvBuf.Read(buf) - log.Tracef("%v read from stream %v with err %v", n, s.id, err) - return - } - } else { - n, err = s.recvBuf.Read(buf) - log.Tracef("%v read from stream %v with err %v", n, s.id, err) - return + n, err = s.recvBuf.Read(buf) + if err == io.EOF { + return n, ErrBrokenStream } + log.Tracef("%v read from stream %v with err %v", n, s.id, err) + return + } // Write implements io.Write diff --git a/internal/multiplex/streamBuffer.go b/internal/multiplex/streamBuffer.go index bd6c757..5182ba7 100644 --- a/internal/multiplex/streamBuffer.go +++ b/internal/multiplex/streamBuffer.go @@ -107,7 +107,3 @@ func (sb *streamBuffer) Read(buf []byte) (int, error) { func (sb *streamBuffer) Close() error { return sb.buf.Close() } - -func (sb *streamBuffer) Len() int { - return sb.buf.Len() -}