1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
13 type bufferPool struct {
14 // limiter has a "true" placeholder for each in-use buffer.
16 // allocated is the number of bytes currently allocated to buffers.
18 // Pool has unused buffers.
22 func newBufferPool(count int, bufSize int) *bufferPool {
24 p.New = func() interface{} {
25 atomic.AddUint64(&p.allocated, uint64(bufSize))
26 return make([]byte, bufSize)
28 p.limiter = make(chan bool, count)
32 func (p *bufferPool) Get(size int) []byte {
34 case p.limiter <- true:
37 log.Printf("reached max buffers (%d), waiting", cap(p.limiter))
39 log.Printf("waited %v for a buffer", time.Since(t0))
41 buf := p.Pool.Get().([]byte)
43 log.Fatalf("bufferPool Get(size=%d) but max=%d", size, cap(buf))
48 func (p *bufferPool) Put(buf []byte) {
53 // Alloc returns the number of bytes allocated to buffers.
54 func (p *bufferPool) Alloc() uint64 {
55 return atomic.LoadUint64(&p.allocated)
58 // Cap returns the maximum number of buffers allowed.
59 func (p *bufferPool) Cap() int {
63 // Len returns the number of buffers in use right now.
64 func (p *bufferPool) Len() int {