Browse Source

Remove ctxreader. It didn't solve the problem it meant to solve but it adds unneeded complexity

DarthSim 2 years ago
parent
commit
6d087712aa

+ 0 - 44
ctxreader/ctxreader.go

@@ -1,44 +0,0 @@
-package ctxreader
-
-import (
-	"context"
-	"io"
-	"sync"
-	"sync/atomic"
-)
-
-type ctxReader struct {
-	r         io.ReadCloser
-	err       atomic.Value
-	closeOnce sync.Once
-}
-
-func (r *ctxReader) Read(p []byte) (int, error) {
-	if err := r.err.Load(); err != nil {
-		return 0, err.(error)
-	}
-	return r.r.Read(p)
-}
-
-func (r *ctxReader) Close() (err error) {
-	r.closeOnce.Do(func() { err = r.r.Close() })
-	return
-}
-
-func New(ctx context.Context, r io.ReadCloser, closeOnDone bool) io.ReadCloser {
-	if ctx.Done() == nil {
-		return r
-	}
-
-	ctxr := ctxReader{r: r}
-
-	go func(ctx context.Context) {
-		<-ctx.Done()
-		ctxr.err.Store(ctx.Err())
-		if closeOnDone {
-			ctxr.closeOnce.Do(func() { ctxr.r.Close() })
-		}
-	}(ctx)
-
-	return &ctxr
-}

+ 0 - 114
ctxreader/ctxreader_test.go

@@ -1,114 +0,0 @@
-package ctxreader
-
-import (
-	"context"
-	"crypto/rand"
-	"testing"
-	"time"
-
-	"github.com/stretchr/testify/require"
-	"github.com/stretchr/testify/suite"
-)
-
-type testReader struct {
-	closed bool
-}
-
-func (r *testReader) Read(p []byte) (int, error) {
-	return rand.Reader.Read(p)
-}
-
-func (r *testReader) Close() error {
-	r.closed = true
-	return nil
-}
-
-type CtxReaderTestSuite struct {
-	suite.Suite
-}
-
-func (s *CtxReaderTestSuite) TestReadUntilCanceled() {
-	ctx, cancel := context.WithCancel(context.Background())
-
-	r := New(ctx, &testReader{}, false)
-	p := make([]byte, 1024)
-
-	_, err := r.Read(p)
-	require.Nil(s.T(), err)
-
-	cancel()
-	time.Sleep(time.Second)
-
-	_, err = r.Read(p)
-	require.Equal(s.T(), err, context.Canceled)
-}
-
-func (s *CtxReaderTestSuite) TestReturnOriginalOnBackgroundContext() {
-	rr := &testReader{}
-	r := New(context.Background(), rr, false)
-
-	require.Equal(s.T(), rr, r)
-}
-
-func (s *CtxReaderTestSuite) TestClose() {
-	ctx, cancel := context.WithCancel(context.Background())
-	defer cancel()
-
-	rr := &testReader{}
-	New(ctx, rr, true).Close()
-
-	require.True(s.T(), rr.closed)
-}
-
-func (s *CtxReaderTestSuite) TestCloseOnCancel() {
-	ctx, cancel := context.WithCancel(context.Background())
-
-	rr := &testReader{}
-	New(ctx, rr, true)
-
-	cancel()
-	time.Sleep(time.Second)
-
-	require.True(s.T(), rr.closed)
-}
-
-func (s *CtxReaderTestSuite) TestDontCloseOnCancel() {
-	ctx, cancel := context.WithCancel(context.Background())
-
-	rr := &testReader{}
-	New(ctx, rr, false)
-
-	cancel()
-	time.Sleep(time.Second)
-
-	require.False(s.T(), rr.closed)
-}
-
-func TestCtxReader(t *testing.T) {
-	suite.Run(t, new(CtxReaderTestSuite))
-}
-
-func BenchmarkRawReader(b *testing.B) {
-	r := testReader{}
-
-	b.ResetTimer()
-
-	p := make([]byte, 1024)
-	for i := 0; i < b.N; i++ {
-		r.Read(p)
-	}
-}
-
-func BenchmarkCtxReader(b *testing.B) {
-	ctx, cancel := context.WithTimeout(context.Background(), time.Hour)
-	defer cancel()
-
-	r := New(ctx, &testReader{}, true)
-
-	b.ResetTimer()
-
-	p := make([]byte, 1024)
-	for i := 0; i < b.N; i++ {
-		r.Read(p)
-	}
-}

+ 1 - 2
transport/azure/azure.go

@@ -16,7 +16,6 @@ import (
 	"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blockblob"
 
 	"github.com/imgproxy/imgproxy/v3/config"
-	"github.com/imgproxy/imgproxy/v3/ctxreader"
 	"github.com/imgproxy/imgproxy/v3/httprange"
 	"github.com/imgproxy/imgproxy/v3/transport/notmodified"
 )
@@ -159,7 +158,7 @@ func (t transport) RoundTrip(req *http.Request) (*http.Response, error) {
 		ProtoMinor:    0,
 		Header:        header,
 		ContentLength: contentLength,
-		Body:          ctxreader.New(req.Context(), result.Body, true),
+		Body:          result.Body,
 		Close:         true,
 		Request:       req,
 	}, nil

+ 1 - 2
transport/fs/fs.go

@@ -14,7 +14,6 @@ import (
 	"strings"
 
 	"github.com/imgproxy/imgproxy/v3/config"
-	"github.com/imgproxy/imgproxy/v3/ctxreader"
 	"github.com/imgproxy/imgproxy/v3/httprange"
 	"github.com/imgproxy/imgproxy/v3/transport/notmodified"
 )
@@ -96,7 +95,7 @@ func (t transport) RoundTrip(req *http.Request) (resp *http.Response, err error)
 		ProtoMinor:    0,
 		Header:        header,
 		ContentLength: size,
-		Body:          ctxreader.New(req.Context(), body, true),
+		Body:          body,
 		Close:         true,
 		Request:       req,
 	}, nil

+ 1 - 2
transport/gcs/gcs.go

@@ -15,7 +15,6 @@ import (
 	htransport "google.golang.org/api/transport/http"
 
 	"github.com/imgproxy/imgproxy/v3/config"
-	"github.com/imgproxy/imgproxy/v3/ctxreader"
 	"github.com/imgproxy/imgproxy/v3/httprange"
 	defaultTransport "github.com/imgproxy/imgproxy/v3/transport"
 	"github.com/imgproxy/imgproxy/v3/transport/notmodified"
@@ -157,7 +156,7 @@ func (t transport) RoundTrip(req *http.Request) (*http.Response, error) {
 		ProtoMinor:    0,
 		Header:        header,
 		ContentLength: reader.Attrs.Size,
-		Body:          ctxreader.New(req.Context(), reader, true),
+		Body:          reader,
 		Close:         true,
 		Request:       req,
 	}, nil

+ 1 - 2
transport/swift/swift.go

@@ -12,7 +12,6 @@ import (
 	"github.com/ncw/swift/v2"
 
 	"github.com/imgproxy/imgproxy/v3/config"
-	"github.com/imgproxy/imgproxy/v3/ctxreader"
 	defaultTransport "github.com/imgproxy/imgproxy/v3/transport"
 	"github.com/imgproxy/imgproxy/v3/transport/notmodified"
 )
@@ -103,7 +102,7 @@ func (t transport) RoundTrip(req *http.Request) (resp *http.Response, err error)
 		ProtoMajor: 1,
 		ProtoMinor: 0,
 		Header:     header,
-		Body:       ctxreader.New(req.Context(), object, true),
+		Body:       object,
 		Close:      true,
 		Request:    req,
 	}, nil