-
Notifications
You must be signed in to change notification settings - Fork 8
/
bp.go
70 lines (60 loc) · 1.26 KB
/
bp.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"bytes"
"container/list"
"time"
)
type qBuf struct {
when time.Time
buffer *bytes.Buffer
}
type bp struct {
makes int
get chan *bytes.Buffer
give chan *bytes.Buffer
quit chan bool
}
func makeBuffer(size int64) []byte {
return make([]byte, 0, size)
}
func newBufferPool(bufsz int64) (np *bp) {
np = new(bp)
np.get = make(chan *bytes.Buffer)
np.give = make(chan *bytes.Buffer)
np.quit = make(chan bool)
go func() {
q := new(list.List)
for {
if q.Len() == 0 {
size := bufsz + 100*1024 // allocate overhead to avoid slice growth
q.PushFront(qBuf{when: time.Now(), buffer: bytes.NewBuffer(makeBuffer(int64(size)))})
np.makes++
}
e := q.Front()
timeout := time.NewTimer(time.Minute)
select {
case b := <-np.give:
timeout.Stop()
q.PushFront(qBuf{when: time.Now(), buffer: b})
case np.get <- e.Value.(qBuf).buffer:
timeout.Stop()
q.Remove(e)
case <-timeout.C:
// free unused buffers
e := q.Front()
for e != nil {
n := e.Next()
if time.Since(e.Value.(qBuf).when) > time.Minute {
q.Remove(e)
e.Value = nil
}
e = n
}
case <-np.quit:
debugf("%d buffers of %d MB allocated", np.makes, bufsz/(1*1024*1024))
return
}
}
}()
return np
}