Skip to content

Commit

Permalink
Merge pull request #16 from lxzan/dev
Browse files Browse the repository at this point in the history
Avoid repeated closures
lxzan authored Mar 22, 2024
2 parents e59de96 + e7bdbb3 commit 0d45eaf
Showing 4 changed files with 13 additions and 4 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -17,4 +17,3 @@ debug
vendor/
examples/
bin/
go.work*
7 changes: 7 additions & 0 deletions go.work
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
go 1.18

use (
.
./contrib/pb
./contrib/yaml
)
8 changes: 5 additions & 3 deletions internal/pool.go
Original file line number Diff line number Diff line change
@@ -40,8 +40,10 @@ func (c *CloserWrapper) Read(p []byte) (n int, err error) {
}

func (c *CloserWrapper) Close() error {
c.B.Reset()
bytebufferpool.Put(c.B)
c.B, c.R = nil, nil
// 避免重复关闭, 引发panic
if c.B != nil {
bytebufferpool.Put(c.B)
c.B, c.R = nil, nil
}
return nil
}
1 change: 1 addition & 0 deletions internal/pool_test.go
Original file line number Diff line number Diff line change
@@ -26,5 +26,6 @@ func TestCloserWrapper(t *testing.T) {
cw.Read(make([]byte, 10))
cw.Close()
assert.Nil(t, cw.B)
cw.Close()
assert.Nil(t, cw.R)
}

0 comments on commit 0d45eaf

Please sign in to comment.