10 type bufferPool struct {
11 // limiter has a "true" placeholder for each in-use buffer.
13 // allocated is the number of bytes currently allocated to buffers.
15 // Pool has unused buffers.
19 func newBufferPool(count int, bufSize int) *bufferPool {
21 p.New = func() interface{} {
22 atomic.AddUint64(&p.allocated, uint64(bufSize))
23 return make([]byte, bufSize)
25 p.limiter = make(chan bool, count)
29 func (p *bufferPool) Get(size int) []byte {
31 case p.limiter <- true:
34 log.Printf("reached max buffers (%d), waiting", cap(p.limiter))
36 log.Printf("waited %v for a buffer", time.Since(t0))
38 buf := p.Pool.Get().([]byte)
40 log.Fatalf("bufferPool Get(size=%d) but max=%d", size, cap(buf))
45 func (p *bufferPool) Put(buf []byte) {
50 // Alloc returns the number of bytes allocated to buffers.
51 func (p *bufferPool) Alloc() uint64 {
52 return atomic.LoadUint64(&p.allocated)
55 // Cap returns the maximum number of buffers allowed.
56 func (p *bufferPool) Cap() int {
60 // Len returns the number of buffers in use right now.
61 func (p *bufferPool) Len() int {