pkg/ioutils: move BytesPipe to container/streams/bytespipe

These types are only used internally in container/streams and have no
external consumers. move them to a subpackage of container/streams.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-01-09 16:20:22 +01:00
parent 00d1b92d0a
commit 15ce2914a5
6 changed files with 15 additions and 26 deletions

View File

@@ -156,12 +156,6 @@ issues:
linters:
- staticcheck
# FIXME temporarily suppress these until they are moved internal to container/streams.
- text: "SA1019: ioutils\\.(ErrClosed|BytesPipe|NewBytesPipe)"
path: "container/stream/"
linters:
- staticcheck
- text: "ineffectual assignment to ctx"
source: "ctx[, ].*=.*\\(ctx[,)]"
linters:

View File

@@ -1,4 +1,4 @@
package ioutils // import "github.com/docker/docker/pkg/ioutils"
package bytespipe
import (
"errors"

View File

@@ -1,4 +1,4 @@
package ioutils // import "github.com/docker/docker/pkg/ioutils"
package bytespipe
import (
"bytes"

View File

@@ -1,4 +1,4 @@
package ioutils // import "github.com/docker/docker/pkg/ioutils"
package bytespipe
import (
"errors"
@@ -18,8 +18,6 @@ const blockThreshold = 1e6
var (
// ErrClosed is returned when Write is called on a closed BytesPipe.
//
// Deprecated: this type is only used internally, and will be removed in the next release.
ErrClosed = errors.New("write to closed BytesPipe")
bufPools = make(map[int]*sync.Pool)
@@ -30,8 +28,6 @@ var (
// All written data may be read at most once. Also, BytesPipe allocates
// and releases new byte slices to adjust to current needs, so the buffer
// won't be overgrown after peak loads.
//
// Deprecated: this type is only used internally, and will be removed in the next release.
type BytesPipe struct {
mu sync.Mutex
wait *sync.Cond
@@ -41,12 +37,10 @@ type BytesPipe struct {
readBlock bool // check read BytesPipe is Wait() or not
}
// NewBytesPipe creates new BytesPipe, initialized by specified slice.
// New creates new BytesPipe, initialized by specified slice.
// If buf is nil, then it will be initialized with slice which cap is 64.
// buf will be adjusted in a way that len(buf) == 0, cap(buf) == cap(buf).
//
// Deprecated: this function is only used internally, and will be removed in the next release.
func NewBytesPipe() *BytesPipe {
func New() *BytesPipe {
bp := &BytesPipe{}
bp.buf = append(bp.buf, getBuffer(minCap))
bp.wait = sync.NewCond(&bp.mu)

View File

@@ -1,4 +1,4 @@
package ioutils // import "github.com/docker/docker/pkg/ioutils"
package bytespipe
import (
"crypto/sha256"
@@ -9,7 +9,7 @@ import (
)
func TestBytesPipeRead(t *testing.T) {
buf := NewBytesPipe()
buf := New()
_, _ = buf.Write([]byte("12"))
_, _ = buf.Write([]byte("34"))
_, _ = buf.Write([]byte("56"))
@@ -49,7 +49,7 @@ func TestBytesPipeRead(t *testing.T) {
}
func TestBytesPipeWrite(t *testing.T) {
buf := NewBytesPipe()
buf := New()
_, _ = buf.Write([]byte("12"))
_, _ = buf.Write([]byte("34"))
_, _ = buf.Write([]byte("56"))
@@ -62,7 +62,7 @@ func TestBytesPipeWrite(t *testing.T) {
// Regression test for #41941.
func TestBytesPipeDeadlock(t *testing.T) {
bp := NewBytesPipe()
bp := New()
bp.buf = []*fixedBuffer{getBuffer(blockThreshold)}
rd := make(chan error)
@@ -145,7 +145,7 @@ func TestBytesPipeWriteRandomChunks(t *testing.T) {
expected := hex.EncodeToString(hash.Sum(nil))
// write/read through buffer
buf := NewBytesPipe()
buf := New()
hash.Reset()
done := make(chan struct{})
@@ -186,7 +186,7 @@ func BenchmarkBytesPipeWrite(b *testing.B) {
testData := []byte("pretty short line, because why not?")
for i := 0; i < b.N; i++ {
readBuf := make([]byte, 1024)
buf := NewBytesPipe()
buf := New()
go func() {
var err error
for err == nil {
@@ -205,7 +205,7 @@ func BenchmarkBytesPipeRead(b *testing.B) {
rd := make([]byte, 512)
for i := 0; i < b.N; i++ {
b.StopTimer()
buf := NewBytesPipe()
buf := New()
for j := 0; j < 500; j++ {
_, _ = buf.Write(make([]byte, 1024))
}

View File

@@ -9,6 +9,7 @@ import (
"github.com/containerd/containerd/cio"
"github.com/containerd/log"
"github.com/docker/docker/container/stream/bytespipe"
"github.com/docker/docker/pkg/ioutils"
"github.com/docker/docker/pkg/pools"
)
@@ -64,7 +65,7 @@ func (c *Config) StdinPipe() io.WriteCloser {
// It adds this new out pipe to the Stdout broadcaster.
// This will block stdout if unconsumed.
func (c *Config) StdoutPipe() io.ReadCloser {
bytesPipe := ioutils.NewBytesPipe()
bytesPipe := bytespipe.New()
c.stdout.Add(bytesPipe)
return bytesPipe
}
@@ -73,7 +74,7 @@ func (c *Config) StdoutPipe() io.ReadCloser {
// It adds this new err pipe to the Stderr broadcaster.
// This will block stderr if unconsumed.
func (c *Config) StderrPipe() io.ReadCloser {
bytesPipe := ioutils.NewBytesPipe()
bytesPipe := bytespipe.New()
c.stderr.Add(bytesPipe)
return bytesPipe
}