/
concurrencylimiter.go
168 lines (142 loc) · 2.91 KB
/
concurrencylimiter.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 app
import (
"context"
"sync"
"github.com/pkg/errors"
)
var errQueueFull = errors.New("queue full")
// concurrencyLimiter is a locking mechanism that allows setting a limit
// on the number of simultaneous locks for a given arbitrary ID.
type concurrencyLimiter struct {
maxLock int
maxQueue int
lockCount map[string]int
queueCount map[string]int
pending map[string]*pendingList
mx sync.Mutex
}
type pendingReq struct {
lockCh chan bool
prev, next *pendingReq
}
type pendingList struct{ head, tail *pendingReq }
func (list *pendingList) newReq() *pendingReq {
req := &pendingReq{
lockCh: make(chan bool),
prev: list.tail,
}
if list.head == nil {
list.head, list.tail = req, req
} else {
list.tail.next, list.tail = req, req
}
return req
}
func (list *pendingList) remove(req *pendingReq) bool {
if req == nil || list == nil {
return false
}
defer func() { req.prev, req.next = nil, nil }()
if list.head == req {
list.head = req.next
if list.head != nil {
list.head.prev = nil
}
if list.tail == req {
list.tail = nil
}
return true
}
if list.tail == req {
list.tail = req.prev
if list.tail != nil {
list.tail.next = nil
}
return true
}
if req.next != nil {
req.next.prev = req.prev
}
if req.prev != nil {
req.prev.next = req.next
return true
}
return false
}
func (list *pendingList) pop() *pendingReq {
if list == nil {
return nil
}
req := list.head
list.remove(req)
return req
}
func newConcurrencyLimiter(maxLock, maxQueue int) *concurrencyLimiter {
return &concurrencyLimiter{
maxLock: maxLock,
maxQueue: maxQueue,
lockCount: make(map[string]int, 100),
queueCount: make(map[string]int, 100),
pending: make(map[string]*pendingList, 100),
}
}
// Lock will acquire a lock for the given ID. It may return an err
// if the context expires before the lock is given.
func (l *concurrencyLimiter) Lock(ctx context.Context, id string) (err error) {
l.mx.Lock()
c := l.lockCount[id]
if c < l.maxLock {
l.lockCount[id] = c + 1
l.mx.Unlock()
return nil
}
list := l.pending[id]
if list == nil {
list = &pendingList{}
l.pending[id] = list
}
if l.queueCount[id] == l.maxQueue {
l.mx.Unlock()
return errQueueFull
}
// need to queue
req := list.newReq()
l.queueCount[id]++
l.mx.Unlock()
select {
case <-ctx.Done():
close(req.lockCh)
l.mx.Lock()
if list.remove(req) {
l.queueCount[id]--
}
l.mx.Unlock()
return ctx.Err()
case req.lockCh <- true:
}
return nil
}
// Unlock releases a lock for the given ID. It panics
// if there are no remaining locks.
func (l *concurrencyLimiter) Unlock(id string) {
l.mx.Lock()
if l.lockCount[id] == 0 {
l.mx.Unlock()
panic("not locked")
}
list := l.pending[id]
for {
req := list.pop()
if req == nil {
break
}
l.queueCount[id]--
if <-req.lockCh {
// keep lock count
l.mx.Unlock()
return
}
}
l.lockCount[id]--
l.mx.Unlock()
}