diff --git a/ctxreader/ctxreader.go b/ctxreader/ctxreader.go deleted file mode 100644 index ed394776..00000000 --- a/ctxreader/ctxreader.go +++ /dev/null @@ -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 -} diff --git a/ctxreader/ctxreader_test.go b/ctxreader/ctxreader_test.go deleted file mode 100644 index 60c3859c..00000000 --- a/ctxreader/ctxreader_test.go +++ /dev/null @@ -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) - } -} diff --git a/transport/azure/azure.go b/transport/azure/azure.go index 16183002..3e29a0a7 100644 --- a/transport/azure/azure.go +++ b/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 diff --git a/transport/fs/fs.go b/transport/fs/fs.go index c50677ef..0bac8d33 100644 --- a/transport/fs/fs.go +++ b/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 diff --git a/transport/gcs/gcs.go b/transport/gcs/gcs.go index f291ab48..9a7a51fa 100644 --- a/transport/gcs/gcs.go +++ b/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 diff --git a/transport/swift/swift.go b/transport/swift/swift.go index e4d7c094..38a7b866 100644 --- a/transport/swift/swift.go +++ b/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