New Upstream Snapshot - golang-github-oxtoacart-bpool

Ready changes

Summary

Merged new upstream version: 0.0~git20190530.03653db+ds (was: 0.0~git20150712.4e1c556).

Resulting package

Built on 2022-10-21T21:57 (took 6m58s)

The resulting binary packages can be installed (if you have the apt repository enabled) by running one of:

apt install -t fresh-snapshots golang-github-oxtoacart-bpool-dev

Lintian Result

Diff

diff --git a/bufferpool.go b/bufferpool.go
index 8c8ac64..249a078 100644
--- a/bufferpool.go
+++ b/bufferpool.go
@@ -38,3 +38,8 @@ func (bp *BufferPool) Put(b *bytes.Buffer) {
 	default: // Discard the buffer if the pool is full.
 	}
 }
+
+// NumPooled returns the number of items currently pooled.
+func (bp *BufferPool) NumPooled() int {
+	return len(bp.c)
+}
diff --git a/bufferpool_test.go b/bufferpool_test.go
index b344bdc..a4f8e68 100644
--- a/bufferpool_test.go
+++ b/bufferpool_test.go
@@ -23,7 +23,7 @@ func TestBufferPool(t *testing.T) {
 	close(bufPool.c)
 
 	// Check the size of the pool.
-	if len(bufPool.c) != size {
+	if bufPool.NumPooled() != size {
 		t.Fatalf("bufferpool size invalid: got %v want %v", len(bufPool.c), size)
 	}
 
diff --git a/bytepool.go b/bytepool.go
index ef3898a..a5ebe05 100644
--- a/bytepool.go
+++ b/bytepool.go
@@ -5,6 +5,7 @@ package bpool
 type BytePool struct {
 	c chan []byte
 	w int
+	h int
 }
 
 // NewBytePool creates a new BytePool bounded to the given maxSize, with new
@@ -31,14 +32,24 @@ func (bp *BytePool) Get() (b []byte) {
 
 // Put returns the given Buffer to the BytePool.
 func (bp *BytePool) Put(b []byte) {
+	if cap(b) < bp.w {
+		// someone tried to put back a too small buffer, discard it
+		return
+	}
+
 	select {
-	case bp.c <- b:
+	case bp.c <- b[:bp.w]:
 		// buffer went back into pool
 	default:
 		// buffer didn't go back into pool, just discard
 	}
 }
 
+// NumPooled returns the number of items currently pooled.
+func (bp *BytePool) NumPooled() int {
+	return len(bp.c)
+}
+
 // Width returns the width of the byte arrays in this pool.
 func (bp *BytePool) Width() (n int) {
 	return bp.w
diff --git a/bytepool_test.go b/bytepool_test.go
index 216fd5b..0b79b72 100644
--- a/bytepool_test.go
+++ b/bytepool_test.go
@@ -19,7 +19,23 @@ func TestBytePool(t *testing.T) {
 		t.Fatalf("bytepool length invalid: got %v want %v", len(b), width)
 	}
 
-	bufPool.Put(b)
+	// Try putting some invalid buffers into pool
+	bufPool.Put(make([]byte, width-1))
+	bufPool.Put(make([]byte, width)[2:])
+	if len(bufPool.c) > 0 {
+		t.Fatal("bytepool should have rejected invalid packets")
+	}
+
+	// Try putting a short slice into pool
+	bufPool.Put(make([]byte, width)[:2])
+	if len(bufPool.c) != 1 {
+		t.Fatal("bytepool should have accepted short slice with sufficient capacity")
+	}
+
+	b = bufPool.Get()
+	if len(b) != width {
+		t.Fatalf("bytepool length invalid: got %v want %v", len(b), width)
+	}
 
 	// Fill the pool beyond the capped pool size.
 	for i := 0; i < size*2; i++ {
@@ -30,7 +46,7 @@ func TestBytePool(t *testing.T) {
 	close(bufPool.c)
 
 	// Check the size of the pool.
-	if len(bufPool.c) != size {
+	if bufPool.NumPooled() != size {
 		t.Fatalf("bytepool size invalid: got %v want %v", len(bufPool.c), size)
 	}
 
diff --git a/byteslice.go b/byteslice.go
new file mode 100644
index 0000000..7b94b39
--- /dev/null
+++ b/byteslice.go
@@ -0,0 +1,81 @@
+package bpool
+
+// WrapByteSlice wraps a []byte as a ByteSlice
+func WrapByteSlice(full []byte, headerLength int) ByteSlice {
+	return ByteSlice{
+		full:    full,
+		current: full[headerLength:],
+		head:    headerLength,
+		end:     len(full),
+	}
+}
+
+// ByteSlice provides a wrapper around []byte with some added convenience
+type ByteSlice struct {
+	full    []byte
+	current []byte
+	head    int
+	end     int
+}
+
+// ResliceTo reslices the end of the current slice.
+func (b ByteSlice) ResliceTo(end int) ByteSlice {
+	return ByteSlice{
+		full:    b.full,
+		current: b.current[:end],
+		head:    b.head,
+		end:     b.head + end,
+	}
+}
+
+// Bytes returns the current slice
+func (b ByteSlice) Bytes() []byte {
+	return b.current
+}
+
+// BytesWithHeader returns the current slice preceded by the header
+func (b ByteSlice) BytesWithHeader() []byte {
+	return b.full[:b.end]
+}
+
+// Full returns the full original buffer underlying the ByteSlice
+func (b ByteSlice) Full() []byte {
+	return b.full
+}
+
+// ByteSlicePool is a bool of byte slices
+type ByteSlicePool interface {
+	// Get gets a byte slice from the pool
+	GetSlice() ByteSlice
+	// Put returns a byte slice to the pool
+	PutSlice(ByteSlice)
+	// NumPooled returns the number of currently pooled items
+	NumPooled() int
+}
+
+// NewByteSlicePool creates a new ByteSlicePool bounded to the
+// given maxSize, with new byte arrays sized based on width
+func NewByteSlicePool(maxSize int, width int) ByteSlicePool {
+	return NewHeaderPreservingByteSlicePool(maxSize, width, 0)
+}
+
+// NewHeaderPreservingByteSlicePool creates a new ByteSlicePool bounded to the
+// given maxSize, with new byte arrays sized based on width and headerLength
+// preserved at the beginning of the slice.
+func NewHeaderPreservingByteSlicePool(maxSize int, width int, headerLength int) ByteSlicePool {
+	return &BytePool{
+		c: make(chan []byte, maxSize),
+		w: width + headerLength,
+		h: headerLength,
+	}
+}
+
+// GetSlice implements the method from interface ByteSlicePool
+func (bp *BytePool) GetSlice() ByteSlice {
+	return WrapByteSlice(bp.Get(), bp.h)
+}
+
+// PutSlice implements the method from interface ByteSlicePool
+func (bp *BytePool) PutSlice(b ByteSlice) {
+	bp.Put(b.Full())
+}
diff --git a/byteslice_test.go b/byteslice_test.go
new file mode 100644
index 0000000..f7fc31a
--- /dev/null
+++ b/byteslice_test.go
@@ -0,0 +1,38 @@
+package bpool
+
+import (
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
+
+func TestByteSlice(t *testing.T) {
+	full := []byte("abcde")
+	bs := WrapByteSlice(full, 1)
+	assert.EqualValues(t, full[1:], bs.Bytes())
+	assert.EqualValues(t, full, bs.BytesWithHeader())
+	assert.EqualValues(t, full, bs.Full())
+	bs = bs.ResliceTo(2)
+	assert.EqualValues(t, full[1:3], bs.Bytes())
+	assert.EqualValues(t, full[:3], bs.BytesWithHeader())
+	assert.EqualValues(t, full, bs.Full())
+	bs = bs.ResliceTo(1)
+	assert.EqualValues(t, full[1:2], bs.Bytes())
+	assert.EqualValues(t, full[:2], bs.BytesWithHeader())
+	assert.EqualValues(t, full, bs.Full())
+
+}
+
+func TestHeaderPreservingByteSlicePool(t *testing.T) {
+	full := []byte{0, 0, 'a', 'b', 'c'}
+	data := full[2:]
+	pool := NewHeaderPreservingByteSlicePool(1, 3, 2)
+	b := pool.GetSlice()
+	copy(b.Bytes(), data)
+	assert.Equal(t, data, b.Bytes())
+	assert.Equal(t, full, b.Full())
+	pool.PutSlice(b)
+	assert.Equal(t, 1, pool.NumPooled())
+	pool.PutSlice(WrapByteSlice(full, 2))
+	assert.Equal(t, 1, pool.NumPooled(), "Pool should not grow beyond its size limit")
+}
diff --git a/debian/changelog b/debian/changelog
index af9c8f0..bb816bb 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,4 +1,4 @@
-golang-github-oxtoacart-bpool (0.0~git20150712.4e1c556-2) UNRELEASED; urgency=low
+golang-github-oxtoacart-bpool (0.0~git20190530.03653db+ds-1) UNRELEASED; urgency=low
 
   * Apply multi-arch hints.
     + golang-github-oxtoacart-bpool-dev: Add Multi-Arch: foreign.
@@ -6,8 +6,9 @@ golang-github-oxtoacart-bpool (0.0~git20150712.4e1c556-2) UNRELEASED; urgency=lo
   * Set upstream metadata fields: Bug-Database, Bug-Submit, Repository,
     Repository-Browse.
   * Update standards version to 4.5.1, no changes needed.
+  * New upstream snapshot.
 
- -- Debian Janitor <janitor@jelmer.uk>  Wed, 01 Jul 2020 11:37:39 -0000
+ -- Debian Janitor <janitor@jelmer.uk>  Fri, 21 Oct 2022 21:51:35 -0000
 
 golang-github-oxtoacart-bpool (0.0~git20150712.4e1c556-1) unstable; urgency=medium
 
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..9bb3147
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,5 @@
+module github.com/oxtoacart/bpool
+
+go 1.12
+
+require github.com/stretchr/testify v1.3.0
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..4347755
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,7 @@
+github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
+github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=

Debdiff

[The following lists of changes regard files as different if they have different names, permissions or owners.]

Files in second set of .debs but not in first

-rw-r--r--  root/root   /usr/share/gocode/src/github.com/oxtoacart/bpool/byteslice.go
-rw-r--r--  root/root   /usr/share/gocode/src/github.com/oxtoacart/bpool/byteslice_test.go
-rw-r--r--  root/root   /usr/share/gocode/src/github.com/oxtoacart/bpool/go.mod
-rw-r--r--  root/root   /usr/share/gocode/src/github.com/oxtoacart/bpool/go.sum

No differences were encountered in the control files

More details

Full run details