From 4c3a1f2f343637b615c26a27a55afa12dd5aaed5 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Thu, 18 Jan 2024 17:46:10 +0100 Subject: [PATCH] gateway: remove dead, unexported lazySeek --- gateway/lazyseek.go | 61 ------------------------- gateway/lazyseek_test.go | 98 ---------------------------------------- 2 files changed, 159 deletions(-) delete mode 100644 gateway/lazyseek.go delete mode 100644 gateway/lazyseek_test.go diff --git a/gateway/lazyseek.go b/gateway/lazyseek.go deleted file mode 100644 index e0ec44a82..000000000 --- a/gateway/lazyseek.go +++ /dev/null @@ -1,61 +0,0 @@ -package gateway - -import ( - "errors" - "fmt" - "io" -) - -// The HTTP server uses seek to determine the file size. Actually _seeking_ can -// be slow so we wrap the seeker in a _lazy_ seeker. -type lazySeeker struct { - reader io.ReadSeeker - - size int64 - offset int64 - realOffset int64 -} - -func (s *lazySeeker) Seek(offset int64, whence int) (int64, error) { - switch whence { - case io.SeekEnd: - return s.Seek(s.size+offset, io.SeekStart) - case io.SeekCurrent: - return s.Seek(s.offset+offset, io.SeekStart) - case io.SeekStart: - if offset < 0 { - return s.offset, errors.New("invalid seek offset") - } - s.offset = offset - return s.offset, nil - default: - return s.offset, fmt.Errorf("invalid whence: %d", whence) - } -} - -func (s *lazySeeker) Read(b []byte) (int, error) { - // If we're past the end, EOF. - if s.offset >= s.size { - return 0, io.EOF - } - - // actually seek - for s.offset != s.realOffset { - off, err := s.reader.Seek(s.offset, io.SeekStart) - if err != nil { - return 0, err - } - s.realOffset = off - } - off, err := s.reader.Read(b) - s.realOffset += int64(off) - s.offset += int64(off) - return off, err -} - -func (s *lazySeeker) Close() error { - if closer, ok := s.reader.(io.Closer); ok { - return closer.Close() - } - return nil -} diff --git a/gateway/lazyseek_test.go b/gateway/lazyseek_test.go deleted file mode 100644 index fdc6d2b34..000000000 --- a/gateway/lazyseek_test.go +++ /dev/null @@ -1,98 +0,0 @@ -package gateway - -import ( - "errors" - "io" - "strings" - "testing" - - "github.com/stretchr/testify/require" -) - -type badSeeker struct { - io.ReadSeeker -} - -var errBadSeek = errors.New("bad seeker") - -func (bs badSeeker) Seek(offset int64, whence int) (int64, error) { - off, err := bs.ReadSeeker.Seek(0, io.SeekCurrent) - if err != nil { - panic(err) - } - return off, errBadSeek -} - -func TestLazySeekerError(t *testing.T) { - underlyingBuffer := strings.NewReader("fubar") - s := &lazySeeker{ - reader: badSeeker{underlyingBuffer}, - size: underlyingBuffer.Size(), - } - off, err := s.Seek(0, io.SeekEnd) - require.NoError(t, err) - require.Equal(t, s.size, off, "expected to seek to the end") - - // shouldn't have actually seeked. - b, err := io.ReadAll(s) - require.NoError(t, err) - require.Equal(t, 0, len(b), "expected to read nothing") - - // shouldn't need to actually seek. - off, err = s.Seek(0, io.SeekStart) - require.NoError(t, err) - require.Equal(t, int64(0), off, "expected to seek to the start") - - b, err = io.ReadAll(s) - require.NoError(t, err) - require.Equal(t, "fubar", string(b), "expected to read string") - - // should fail the second time. - off, err = s.Seek(0, io.SeekStart) - require.NoError(t, err) - require.Equal(t, int64(0), off, "expected to seek to the start") - - // right here... - b, err = io.ReadAll(s) - require.NotNil(t, err) - require.Equal(t, errBadSeek, err) - require.Equal(t, 0, len(b), "expected to read nothing") -} - -func TestLazySeeker(t *testing.T) { - underlyingBuffer := strings.NewReader("fubar") - s := &lazySeeker{ - reader: underlyingBuffer, - size: underlyingBuffer.Size(), - } - expectByte := func(b byte) { - t.Helper() - var buf [1]byte - n, err := io.ReadFull(s, buf[:]) - require.NoError(t, err) - require.Equal(t, 1, n, "expected to read one byte, read %d", n) - require.Equal(t, b, buf[0]) - } - expectSeek := func(whence int, off, expOff int64, expErr string) { - t.Helper() - n, err := s.Seek(off, whence) - if expErr == "" { - require.NoError(t, err) - } else { - require.EqualError(t, err, expErr) - } - require.Equal(t, expOff, n) - } - - expectSeek(io.SeekEnd, 0, s.size, "") - b, err := io.ReadAll(s) - require.NoError(t, err) - require.Equal(t, 0, len(b), "expected to read nothing") - expectSeek(io.SeekEnd, -1, s.size-1, "") - expectByte('r') - expectSeek(io.SeekStart, 0, 0, "") - expectByte('f') - expectSeek(io.SeekCurrent, 1, 2, "") - expectByte('b') - expectSeek(io.SeekCurrent, -100, 3, "invalid seek offset") -}