forked from Cloud-Foundations/Dominator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
impl.go
175 lines (168 loc) · 4.04 KB
/
impl.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
169
170
171
172
173
174
175
package resourcepool
func newPool(max uint, metricsSubDirname string) *Pool {
pool := &Pool{
max: max,
semaphore: make(chan struct{}, max),
unused: make(map[*Resource]struct{}),
}
pool.registerMetrics(metricsSubDirname)
return pool
}
func (pool *Pool) create(allocateReleaser AllocateReleaser) *Resource {
return &Resource{
pool: pool,
allocateReleaser: allocateReleaser,
semaphore: make(chan struct{}, 1)}
}
func (pool *Pool) getSlot(cancelChannel <-chan struct{}) bool {
// Grab a slot (the right to have a resource in use).
select {
case pool.semaphore <- struct{}{}:
return true
default:
}
select {
case pool.semaphore <- struct{}{}:
return true
case <-cancelChannel:
return false
}
}
func (resource *Resource) get(cancelChannel <-chan struct{}) error {
drainSemaphore := false
defer func() {
if drainSemaphore {
<-resource.semaphore
}
}()
select {
case resource.semaphore <- struct{}{}:
drainSemaphore = true
default:
select {
case resource.semaphore <- struct{}{}:
drainSemaphore = true
case <-cancelChannel:
return ErrorPutTimeout
}
}
pool := resource.pool
if !pool.getSlot(cancelChannel) {
return ErrorResourceLimitExceeded
}
pool.lock.Lock()
defer pool.lock.Unlock()
if resource.allocated {
delete(pool.unused, resource)
pool.numUnused = uint(len(pool.unused))
drainSemaphore = false
pool.numUsed++
return nil
}
if pool.numUsed+uint(len(pool.unused))+pool.numReleasing >= pool.max {
// Need to grab a free resource and release. Be lazy: do a random pick.
var resourceToRelease *Resource
for res := range pool.unused {
resourceToRelease = res
break
}
if resourceToRelease == nil {
panic("No free resource to release")
}
if !resourceToRelease.allocated {
panic("Resource is not allocated")
}
delete(pool.unused, resourceToRelease)
pool.numUnused = uint(len(pool.unused))
resourceToRelease.allocated = false
pool.numReleasing++
resourceToRelease.releasing.Lock()
pool.lock.Unlock()
resourceToRelease.releaseError =
resourceToRelease.allocateReleaser.Release()
pool.lock.Lock()
resourceToRelease.releasing.Unlock()
pool.numReleasing--
}
resource.allocating = true
resource.allocated = true
pool.numUsed++
pool.lock.Unlock()
resource.releasing.Lock() // Wait for myself to finish releasing.
resource.releasing.Unlock()
err := resource.allocateReleaser.Allocate()
pool.lock.Lock()
resource.allocating = false
if err != nil {
resource.allocated = false
pool.numUsed--
<-pool.semaphore // Free up a slot for someone else.
return err
}
drainSemaphore = false
return nil
}
func (resource *Resource) put() {
pool := resource.pool
pool.lock.Lock()
if !resource.allocated {
pool.lock.Unlock()
return
}
if len(resource.semaphore) < 1 {
pool.lock.Unlock()
panic("Resource was not gotten")
}
if resource.releaseOnPut {
resource.release(true)
return
}
pool.unused[resource] = struct{}{}
pool.numUnused = uint(len(pool.unused))
<-resource.semaphore
pool.numUsed--
pool.lock.Unlock()
<-pool.semaphore // Free up a slot for someone else.
}
func (resource *Resource) release(haveLock bool) error {
pool := resource.pool
if !haveLock {
pool.lock.Lock()
}
if resource.allocating {
pool.lock.Unlock()
panic("Resource is allocating")
}
if !resource.allocated {
pool.lock.Unlock()
return resource.releaseError
}
delete(resource.pool.unused, resource)
pool.numUnused = uint(len(pool.unused))
resource.allocated = false
wasUsed := false
if len(resource.semaphore) > 0 {
wasUsed = true
<-resource.semaphore
pool.numUsed--
}
pool.numReleasing++
pool.lock.Unlock()
resource.releaseError = resource.allocateReleaser.Release()
pool.lock.Lock()
pool.numReleasing--
pool.lock.Unlock()
if wasUsed {
<-pool.semaphore // Free up a slot for someone else.
}
return resource.releaseError
}
func (resource *Resource) scheduleRelease() error {
resource.pool.lock.Lock()
if len(resource.semaphore) > 0 {
resource.releaseOnPut = true
resource.pool.lock.Unlock()
return nil
}
return resource.release(true)
}