handle zero allocation in buffered writer
This commit is contained in:
parent
9d7bed320b
commit
66d5e49c06
@ -2,9 +2,9 @@
|
||||
|
||||
Pooled Buffer IO for Go programs.
|
||||
|
||||
Package buffer provides a reader implementation similar to bufio.Reader. The underlying memory buffers can be
|
||||
used from a synchronized pool. It also supports scenarios similar to io.Pipe, where a content writer process is
|
||||
wrapped by a buffered reader.
|
||||
Package buffer provides a reader implementation similar to bufio.Reader, and a writer implementation to batch
|
||||
write operations targeting an underlying writer. The used memory buffers can be taken from a synchronized pool.
|
||||
It also supports scenarios similar to io.Pipe, where a content writer process is wrapped by a buffered reader.
|
||||
|
||||
Find the documentation here:
|
||||
|
||||
|
||||
@ -28,6 +28,10 @@ func (w *writer) write(p []byte) (int, error) {
|
||||
|
||||
if len(w.buffer) == 0 {
|
||||
w.buffer, w.err = w.options.Pool.Get()
|
||||
if len(w.buffer) == 0 && w.err == nil {
|
||||
w.err = ErrZeroAllocation
|
||||
}
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
@ -402,4 +402,14 @@ func TestWriter(t *testing.T) {
|
||||
t.Fatal(string(w.written))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("zero allocation", func(t *testing.T) {
|
||||
w := &writer{}
|
||||
p := &fakePool{}
|
||||
o := buffer.Options{Pool: p}
|
||||
b := buffer.BufferedWriter(w, o)
|
||||
if n, err := b.Write([]byte("123")); n != 0 || !errors.Is(err, buffer.ErrZeroAllocation) {
|
||||
t.Fatal(n, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user