Skip to content

Commit

Permalink
Log buffer pool usage and size
Browse files Browse the repository at this point in the history
  • Loading branch information
felixbuenemann committed Sep 6, 2020
1 parent 5c01baa commit 5ac70e9
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions chunk/buffer.go
Expand Up @@ -10,7 +10,8 @@ import (

// BufferPool manages a pool of buffers
type BufferPool struct {
size uint64
size int64
used int64
pool sync.Pool
}

Expand All @@ -19,7 +20,7 @@ func NewBufferPool(bufferSize int64) *BufferPool {
bp := new(BufferPool)
bp.pool = sync.Pool{
New: func() interface{} {
id := atomic.AddUint64(&bp.size, 1)
id := atomic.AddInt64(&bp.size, 1)
Log.Debugf("Allocate buffer %v", id)
buffer := bytes.NewBuffer(make([]byte, bufferSize))
return &Buffer{*buffer, id, 0, bp}
Expand All @@ -30,18 +31,22 @@ func NewBufferPool(bufferSize int64) *BufferPool {

// Get a buffer from the pool
func (bp *BufferPool) Get() *Buffer {
used := atomic.AddInt64(&bp.used, 1)
Log.Debugf("Buffer pool usage %v / %v (get)", used, bp.size)
return bp.pool.Get().(*Buffer)
}

// Put a buffer into the pool
func (bp *BufferPool) Put(buffer *Buffer) {
used := atomic.AddInt64(&bp.used, -1)
Log.Debugf("Buffer pool usage %v / %v (put)", used, bp.size)
bp.pool.Put(buffer)
}

// Buffer is a managed memory buffer with a reference counter
type Buffer struct {
bytes.Buffer
id uint64
id int64
refs int64
pool *BufferPool
}
Expand Down

0 comments on commit 5ac70e9

Please sign in to comment.