1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
12 log "github.com/Sirupsen/logrus"
15 type bufferPool struct {
16 // limiter has a "true" placeholder for each in-use buffer.
18 // allocated is the number of bytes currently allocated to buffers.
20 // Pool has unused buffers.
24 func newBufferPool(count int, bufSize int) *bufferPool {
26 p.New = func() interface{} {
27 atomic.AddUint64(&p.allocated, uint64(bufSize))
28 return make([]byte, bufSize)
30 p.limiter = make(chan bool, count)
34 func (p *bufferPool) Get(size int) []byte {
36 case p.limiter <- true:
39 log.Printf("reached max buffers (%d), waiting", cap(p.limiter))
41 log.Printf("waited %v for a buffer", time.Since(t0))
43 buf := p.Pool.Get().([]byte)
45 log.Fatalf("bufferPool Get(size=%d) but max=%d", size, cap(buf))
50 func (p *bufferPool) Put(buf []byte) {
55 // Alloc returns the number of bytes allocated to buffers.
56 func (p *bufferPool) Alloc() uint64 {
57 return atomic.LoadUint64(&p.allocated)
60 // Cap returns the maximum number of buffers allowed.
61 func (p *bufferPool) Cap() int {
65 // Len returns the number of buffers in use right now.
66 func (p *bufferPool) Len() int {