/
custom_locks.go
168 lines (142 loc) · 3.32 KB
/
custom_locks.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package custom_locks
import (
"sync"
"time"
"github.com/johnsiilver/golib/queue/fifo/unbounded/internal/spin"
)
// Unbounded indicates that the Queue should have no memory bounds.
const Unbounded = -1
const (
unlocked = int32(0)
locked = int32(1)
)
// Buffer provides a FIFO circular buffer that can grow and shrink as required.
// Buffer must not be copied after creation (which means use a pointer if
// passing between functions).
type Buffer struct {
// Max is the maximum size the Buffer can grow to. Use Unbounded if
// you wish to grow the buffer to any size. By default this will grow to 1k items.
Max int
mu sync.Mutex
lastShrink time.Time
data chan interface{}
}
// Push pushes an item onto the circular buffer. "ok" indicates if this happens.
func (c *Buffer) Push(item interface{}) (ok bool) {
c.mu.Lock()
defer c.mu.Unlock()
select {
case c.data <- item:
return true
default:
}
c.grow()
select {
case c.data <- item:
return true
default:
return false
}
}
// Force will push the item onto the buffer and blocks until the operation completes.
func (c *Buffer) Force(item interface{}) {
sleeper := spin.Sleeper{}
for {
if c.Push(item) {
return
}
sleeper.Sleep()
}
}
// Pop returns the next value off the circular buffer. If the buffer is empty
// ok will be false.
func (c *Buffer) Pop() (value interface{}, ok bool) {
c.mu.Lock()
defer c.mu.Unlock()
select {
case v := <-c.data:
c.shrink()
return v, true
default:
return nil, false
}
}
// Pull pulls an item off the circular buffer. It blocks until a value is found.
func (c *Buffer) Pull() interface{} {
sleeper := spin.Sleeper{}
for {
v, ok := c.Pop()
if !ok {
sleeper.Sleep()
continue
}
return v
}
}
// grow will double the size of the internal buffer until we hit the max size
// if the buffer is currently full.
// Note: grow must be protected with .lock/.unlock.
func (c *Buffer) grow() {
if c.Max == 0 {
c.Max = 1000
}
if cap(c.data) == c.Max {
return
}
if len(c.data) == cap(c.data) {
if c.Max == Unbounded {
ch := make(chan interface{}, cap(c.data)*2)
c.copy(ch, c.data)
c.data = ch
return
}
if cap(c.data) < c.Max {
size := cap(c.data) * 2
if size == 0 {
size = 8
}
if size > c.Max {
size = c.Max
}
ch := make(chan interface{}, size)
c.copy(ch, c.data)
c.data = ch
return
}
}
}
// shrink shrinks the size of the internal buffer if the length of the buffer
// is < 50% of the capacity. The reduction will be by 25%, but will produce
// a buffer size of no less than 8 slots.
// Note: shrink must be protected with .lock/.unlock.
func (c *Buffer) shrink() {
if cap(c.data) == 8 {
return
}
if time.Now().Sub(c.lastShrink) > 10*time.Minute {
return
}
// If the current unused capacity is > 50% of the buffer, reduce it by 25%.
if (cap(c.data) - len(c.data)) > (cap(c.data) / 2) {
size := int(float64(cap(c.data)) * .75)
if size < 8 {
size = 8
}
ch := make(chan interface{}, size)
c.copy(ch, c.data)
c.data = ch
c.lastShrink = time.Now()
}
}
func (c *Buffer) copy(dst chan<- interface{}, src chan interface{}) {
if (cap(dst) - len(dst)) < (cap(src) - len(src)) {
panic("internal error: Buffer.copy() cannot be called when dst is smaller than src")
}
if src == nil {
return
}
close(src)
for v := range src {
dst <- v
}
}