forked from lightningnetwork/lnd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_buffer.go
48 lines (40 loc) · 1.34 KB
/
read_buffer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package pool
import (
"time"
"github.com/lightningnetwork/lnd/buffer"
)
const (
// DefaultReadBufferGCInterval is the default interval that a Read will
// perform a sweep to see which expired buffer.Reads can be released to
// the runtime.
DefaultReadBufferGCInterval = 15 * time.Second
// DefaultReadBufferExpiryInterval is the default, minimum interval that
// must elapse before a Read will release a buffer.Read. The maximum
// time before the buffer can be released is equal to the expiry
// interval plus the gc interval.
DefaultReadBufferExpiryInterval = 30 * time.Second
)
// ReadBuffer is a pool of buffer.Read items, that dynamically allocates and
// reclaims buffers in response to load.
type ReadBuffer struct {
pool *Recycle
}
// NewReadBuffer returns a freshly instantiated ReadBuffer, using the given
// gcInterval and expieryInterval.
func NewReadBuffer(gcInterval, expiryInterval time.Duration) *ReadBuffer {
return &ReadBuffer{
pool: NewRecycle(
func() interface{} { return new(buffer.Read) },
100, gcInterval, expiryInterval,
),
}
}
// Take returns a fresh buffer.Read to the caller.
func (p *ReadBuffer) Take() *buffer.Read {
return p.pool.Take().(*buffer.Read)
}
// Return returns the buffer.Read to the pool, so that it can be cycled or
// released.
func (p *ReadBuffer) Return(buf *buffer.Read) {
p.pool.Return(buf)
}