Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 0 additions & 71 deletions pkg/ioutils/readers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"crypto/sha256"
"encoding/hex"
"io"

"golang.org/x/net/context"
)

type readCloserWrapper struct {
Expand Down Expand Up @@ -83,72 +81,3 @@ func (r *OnEOFReader) runFunc() {
r.Fn = nil
}
}

// cancelReadCloser wraps an io.ReadCloser with a context for cancelling read
// operations.
type cancelReadCloser struct {
cancel func()
pR *io.PipeReader // Stream to read from
pW *io.PipeWriter
}

// NewCancelReadCloser creates a wrapper that closes the ReadCloser when the
// context is cancelled. The returned io.ReadCloser must be closed when it is
// no longer needed.
func NewCancelReadCloser(ctx context.Context, in io.ReadCloser) io.ReadCloser {
pR, pW := io.Pipe()

// Create a context used to signal when the pipe is closed
doneCtx, cancel := context.WithCancel(context.Background())

p := &cancelReadCloser{
cancel: cancel,
pR: pR,
pW: pW,
}

go func() {
_, err := io.Copy(pW, in)
select {
case <-ctx.Done():
// If the context was closed, p.closeWithError
// was already called. Calling it again would
// change the error that Read returns.
default:
p.closeWithError(err)
}
in.Close()
}()
go func() {
for {
select {
case <-ctx.Done():
p.closeWithError(ctx.Err())
case <-doneCtx.Done():
return
}
}
}()

return p
}

// Read wraps the Read method of the pipe that provides data from the wrapped
// ReadCloser.
func (p *cancelReadCloser) Read(buf []byte) (n int, err error) {
return p.pR.Read(buf)
}

// closeWithError closes the wrapper and its underlying reader. It will
// cause future calls to Read to return err.
func (p *cancelReadCloser) closeWithError(err error) {
p.pW.CloseWithError(err)
p.cancel()
}

// Close closes the wrapper its underlying reader. It will cause
// future calls to Read to return io.EOF.
func (p *cancelReadCloser) Close() error {
p.closeWithError(io.EOF)
return nil
}
22 changes: 2 additions & 20 deletions pkg/ioutils/readers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@ package ioutils

import (
"fmt"
"io/ioutil"
"strings"
"testing"
"time"

"golang.org/x/net/context"
)

// Implement io.Reader
type errorReader struct{}

func (r *errorReader) Read(p []byte) (int, error) {
return 0, fmt.Errorf("Error reader always fail.")
return 0, fmt.Errorf("error reader always fail")
}

func TestReadCloserWrapperClose(t *testing.T) {
Expand All @@ -35,7 +31,7 @@ func TestReaderErrWrapperReadOnError(t *testing.T) {
called = true
})
_, err := wrapper.Read([]byte{})
if err == nil || !strings.Contains(err.Error(), "Error reader always fail.") {
if err == nil || !strings.Contains(err.Error(), "error reader always fail") {
t.Fatalf("readErrWrapper should returned an error")
}
if !called {
Expand Down Expand Up @@ -78,17 +74,3 @@ func (p *perpetualReader) Read(buf []byte) (n int, err error) {
}
return len(buf), nil
}

func TestCancelReadCloser(t *testing.T) {
ctx, _ := context.WithTimeout(context.Background(), 100*time.Millisecond)
cancelReadCloser := NewCancelReadCloser(ctx, ioutil.NopCloser(&perpetualReader{}))
for {
var buf [128]byte
_, err := cancelReadCloser.Read(buf[:])
if err == context.DeadlineExceeded {
break
} else if err != nil {
t.Fatalf("got unexpected error: %v", err)
}
}
}
156 changes: 0 additions & 156 deletions vendor/golang.org/x/net/context/context.go

This file was deleted.

72 changes: 0 additions & 72 deletions vendor/golang.org/x/net/context/go17.go

This file was deleted.

Loading