1
0

rename Free() to Abort()

This commit is contained in:
Arpad Ryszka 2026-03-19 15:05:29 +01:00
parent 574f6b00e0
commit 13ee6b6965
2 changed files with 11 additions and 13 deletions

12
lib.go
View File

@ -52,8 +52,8 @@ var (
ErrZeroAllocation = errors.New("zero allocation")
// ErrAbort is returned to the writer process in case of buffered content, when the reader
// experienced an error. ErrAbort is returned to the reader process, when the resources were released
// using Free(), and any of the Read operations is called again.
// experienced an error. ErrAbort is returned to the reader process, if Abort() was called and no read
// error was received before it.
ErrAbort = errors.New("read aborted")
)
@ -211,11 +211,9 @@ func (r Reader) WriteTo(w io.Writer) (int64, error) {
return r.reader.writeTo(w)
}
// Free releases the resource held by the Reader, and puts back the underlying byte buffers into the used pool.
//
// It is used when a multi-step read operation is aborted. When a preceeding read operation resulted in an
// error, it is not necessary to call Free().
func (r Reader) Free() {
// Abort releases the resource held by the Reader, and puts back the underlying byte buffers into the used pool.
// The reader cannot be used for read operations after Abort() was called.
func (r Reader) Abort() {
if r.reader == nil {
return
}

View File

@ -151,11 +151,11 @@ func TestLib(t *testing.T) {
})
})
t.Run("free", func(t *testing.T) {
t.Run("abort", func(t *testing.T) {
t.Run("from blank state", func(t *testing.T) {
p := &fakePool{allocSize: 1 << 6}
r := buffer.BufferedReader(&gen{max: 1 << 12}, buffer.Options{Pool: p})
r.Free()
r.Abort()
b := make([]byte, 1<<9)
n, err := r.Read(b)
if n != 0 || !errors.Is(err, buffer.ErrAbort) {
@ -175,7 +175,7 @@ func TestLib(t *testing.T) {
t.Fatal(len(b), ok, err, p.alloc)
}
r.Free()
r.Abort()
b = make([]byte, 1<<9)
n, err := r.Read(b)
if n != 0 || !errors.Is(err, buffer.ErrAbort) {
@ -206,7 +206,7 @@ func TestLib(t *testing.T) {
t.Fatal(n, err)
}
r.Free()
r.Abort()
b = make([]byte, 1<<9)
n, err = r.Read(b)
if n != 0 || !errors.Is(err, errTest) {
@ -247,7 +247,7 @@ func TestLib(t *testing.T) {
}
}
r.Free()
r.Abort()
n, err := r.Read(b)
if n != 0 || !errors.Is(err, buffer.ErrAbort) {
t.Fatal(n, err)
@ -292,7 +292,7 @@ func TestLib(t *testing.T) {
t.Fatal(n, err)
}
r.Free()
r.Abort()
n, err = r.Read(b)
if n != 0 || !errors.Is(err, errTest) {
t.Fatal(n, err)