8 log "github.com/Sirupsen/logrus"
11 type bufferPool struct {
12 // limiter has a "true" placeholder for each in-use buffer.
14 // allocated is the number of bytes currently allocated to buffers.
16 // Pool has unused buffers.
20 func newBufferPool(count int, bufSize int) *bufferPool {
22 p.New = func() interface{} {
23 atomic.AddUint64(&p.allocated, uint64(bufSize))
24 return make([]byte, bufSize)
26 p.limiter = make(chan bool, count)
30 func (p *bufferPool) Get(size int) []byte {
32 case p.limiter <- true:
35 log.Printf("reached max buffers (%d), waiting", cap(p.limiter))
37 log.Printf("waited %v for a buffer", time.Since(t0))
39 buf := p.Pool.Get().([]byte)
41 log.Fatalf("bufferPool Get(size=%d) but max=%d", size, cap(buf))
46 func (p *bufferPool) Put(buf []byte) {
51 // Alloc returns the number of bytes allocated to buffers.
52 func (p *bufferPool) Alloc() uint64 {
53 return atomic.LoadUint64(&p.allocated)
56 // Cap returns the maximum number of buffers allowed.
57 func (p *bufferPool) Cap() int {
61 // Len returns the number of buffers in use right now.
62 func (p *bufferPool) Len() int {