fix buffer recycling

pull/981/head v3.14
Darien Raymond 2018-03-16 16:22:22 +07:00
parent a8a68c2e70
commit 000e0804e8
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
2 changed files with 11 additions and 7 deletions

View File

@ -21,14 +21,14 @@ const (
)
var (
pool [numPools]*sync.Pool
pool [numPools]sync.Pool
poolSize [numPools]uint32
)
func init() {
size := uint32(Size)
for i := 0; i < numPools; i++ {
pool[i] = &sync.Pool{
pool[i] = sync.Pool{
New: createAllocFunc(size),
}
poolSize[i] = size
@ -52,6 +52,7 @@ func freeBytes(b []byte) {
ps := poolSize[i]
if size >= ps {
pool[i].Put(b)
return
}
}
}

View File

@ -24,11 +24,13 @@ func TestAuthenticationReaderWriter(t *testing.T) {
aead, err := cipher.NewGCM(block)
assert(err, IsNil)
rawPayload := make([]byte, 8192*10)
const payloadSize = 1024 * 80
rawPayload := make([]byte, payloadSize)
rand.Read(rawPayload)
payload := buf.NewSize(8192 * 10)
payload := buf.NewSize(payloadSize)
payload.Append(rawPayload)
assert(payload.Len(), Equals, payloadSize)
cache := buf.NewSize(160 * 1024)
iv := make([]byte, 12)
@ -45,7 +47,6 @@ func TestAuthenticationReaderWriter(t *testing.T) {
assert(writer.WriteMultiBuffer(buf.NewMultiBufferValue(payload)), IsNil)
assert(cache.Len(), Equals, 82658)
assert(writer.WriteMultiBuffer(buf.MultiBuffer{}), IsNil)
assert(err, IsNil)
reader := NewAuthenticationReader(&AEADAuthenticator{
AEAD: aead,
@ -57,14 +58,16 @@ func TestAuthenticationReaderWriter(t *testing.T) {
var mb buf.MultiBuffer
for mb.Len() < len(rawPayload) {
for mb.Len() < payloadSize {
mb2, err := reader.ReadMultiBuffer()
assert(err, IsNil)
mb.AppendMulti(mb2)
}
mbContent := make([]byte, 8192*10)
assert(mb.Len(), Equals, payloadSize)
mbContent := make([]byte, payloadSize)
mb.Read(mbContent)
assert(mbContent, Equals, rawPayload)