Skip to content

Commit

Permalink
Merge pull request moby#33472 from unclejack/use_buffer_io_copy
Browse files Browse the repository at this point in the history
container/stream/attach: use pools.Copy
  • Loading branch information
unclejack authored Jun 12, 2017
2 parents dd790d0 + 014095e commit ac587c4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
7 changes: 4 additions & 3 deletions container/stream/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"golang.org/x/net/context"

"github.com/Sirupsen/logrus"
"github.com/docker/docker/pkg/pools"
"github.com/docker/docker/pkg/promise"
"github.com/docker/docker/pkg/term"
)
Expand Down Expand Up @@ -86,7 +87,7 @@ func (c *Config) CopyStreams(ctx context.Context, cfg *AttachConfig) chan error
if cfg.TTY {
_, err = copyEscapable(cfg.CStdin, cfg.Stdin, cfg.DetachKeys)
} else {
_, err = io.Copy(cfg.CStdin, cfg.Stdin)
_, err = pools.Copy(cfg.CStdin, cfg.Stdin)
}
if err == io.ErrClosedPipe {
err = nil
Expand Down Expand Up @@ -116,7 +117,7 @@ func (c *Config) CopyStreams(ctx context.Context, cfg *AttachConfig) chan error
}

logrus.Debugf("attach: %s: begin", name)
_, err := io.Copy(stream, streamPipe)
_, err := pools.Copy(stream, streamPipe)
if err == io.ErrClosedPipe {
err = nil
}
Expand Down Expand Up @@ -174,5 +175,5 @@ func copyEscapable(dst io.Writer, src io.ReadCloser, keys []byte) (written int64
pr := term.NewEscapeProxy(src, keys)
defer src.Close()

return io.Copy(dst, pr)
return pools.Copy(dst, pr)
}
31 changes: 26 additions & 5 deletions pkg/pools/pools.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ import (
"github.com/docker/docker/pkg/ioutils"
)

const buffer32K = 32 * 1024

var (
// BufioReader32KPool is a pool which returns bufio.Reader with a 32K buffer.
BufioReader32KPool = newBufioReaderPoolWithSize(buffer32K)
// BufioWriter32KPool is a pool which returns bufio.Writer with a 32K buffer.
BufioWriter32KPool = newBufioWriterPoolWithSize(buffer32K)
buffer32KPool = newBufferPoolWithSize(buffer32K)
)

const buffer32K = 32 * 1024

// BufioReaderPool is a bufio reader that uses sync.Pool.
type BufioReaderPool struct {
pool sync.Pool
Expand Down Expand Up @@ -54,11 +55,31 @@ func (bufPool *BufioReaderPool) Put(b *bufio.Reader) {
bufPool.pool.Put(b)
}

type bufferPool struct {
pool sync.Pool
}

func newBufferPoolWithSize(size int) *bufferPool {
return &bufferPool{
pool: sync.Pool{
New: func() interface{} { return make([]byte, size) },
},
}
}

func (bp *bufferPool) Get() []byte {
return bp.pool.Get().([]byte)
}

func (bp *bufferPool) Put(b []byte) {
bp.pool.Put(b)
}

// Copy is a convenience wrapper which uses a buffer to avoid allocation in io.Copy.
func Copy(dst io.Writer, src io.Reader) (written int64, err error) {
buf := BufioReader32KPool.Get(src)
written, err = io.Copy(dst, buf)
BufioReader32KPool.Put(buf)
buf := buffer32KPool.Get()
written, err = io.CopyBuffer(dst, src, buf)
buffer32KPool.Put(buf)
return
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/pools/pools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,8 @@ func TestNewWriteCloserWrapperWithAWriteCloser(t *testing.T) {
t.Fatalf("The ReaderCloser should have been closed, it is not.")
}
}

func TestBufferPoolPutAndGet(t *testing.T) {
buf := buffer32KPool.Get()
buffer32KPool.Put(buf)
}

0 comments on commit ac587c4

Please sign in to comment.