-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool.go
233 lines (184 loc) · 4.74 KB
/
pool.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright 2024 FishGoddess. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package rego
import (
"context"
"errors"
"sync"
)
var (
ErrPoolIsFull = errors.New("rego: pool is full")
ErrPoolIsClosed = errors.New("rego: pool is closed")
)
type Status struct {
// Limit is the limit of acquired resources.
Limit uint64 `json:"limit"`
// Acquired is the count of acquired resources.
Acquired uint64 `json:"acquired"`
// Idle is the count of idle resources.
Idle uint64 `json:"idle"`
// Waiting is the count of waiting for a resource.
Waiting uint64 `json:"waiting"`
}
// AcquireFunc is a function acquires a new resource and returns error if failed.
type AcquireFunc[T any] func() (T, error)
// ReleaseFunc is a function releases a resource and returns error if failed.
type ReleaseFunc[T any] func(resource T) error
// DefaultReleaseFunc is a default func to release a resource.
// It does nothing to the resource.
func DefaultReleaseFunc[T any](resource T) error {
return nil
}
type Pool[T any] struct {
config
acquire AcquireFunc[T]
release ReleaseFunc[T]
resources chan T
acquired uint64
waiting uint64
closed bool
lock sync.RWMutex
}
func New[T any](acquire AcquireFunc[T], release ReleaseFunc[T], opts ...Option) *Pool[T] {
if acquire == nil || release == nil {
panic("rego: acquire or release func can't be nil")
}
conf := newDefaultConfig()
for _, opt := range opts {
opt.ApplyTo(conf)
}
pool := &Pool[T]{
config: *conf,
acquire: acquire,
release: release,
resources: make(chan T, conf.limit),
closed: false,
}
return pool
}
func (p *Pool[T]) Put(resource T) error {
p.lock.Lock()
defer p.lock.Unlock()
if p.closed {
return p.release(resource)
}
select {
case p.resources <- resource:
return nil
default:
return p.release(resource)
}
}
func (p *Pool[T]) newPoolFullErr(ctx context.Context) error {
if p.newPoolFullErrFunc == nil {
return ErrPoolIsFull
}
return p.newPoolFullErrFunc(ctx)
}
func (p *Pool[T]) newPoolClosedErr(ctx context.Context) error {
if p.newPoolClosedErrFunc == nil {
return ErrPoolIsClosed
}
return p.newPoolClosedErrFunc(ctx)
}
func (p *Pool[T]) tryToTake() (resource T, ok bool) {
select {
case resource = <-p.resources:
return resource, true
default:
return resource, false
}
}
// waitToTake waits to take an idle resource from pool.
// Record: Add ctx.Done() to select will cause a performance problem...
// The select will call runtime.selectgo if there are more than one case in it, and runtime.selectgo has two steps which is slow:
//
// sellock(scases, lockorder)
// sg := acquireSudog()
//
// We don't know what to do yet, but we think timeout mechanism should be supported even we haven't solved it.
func (p *Pool[T]) waitToTake(ctx context.Context) (resource T, err error) {
select {
case resource = <-p.resources:
return resource, nil
case <-ctx.Done():
return resource, ctx.Err()
}
}
// Take takes a resource from pool and returns an error if failed.
// You should call pool.Put to put a taken resource back to the pool.
// We recommend you to use a defer for putting a resource safely.
func (p *Pool[T]) Take(ctx context.Context) (resource T, err error) {
p.lock.Lock()
if p.closed {
p.lock.Unlock()
return resource, p.newPoolClosedErr(ctx)
}
var ok bool
if resource, ok = p.tryToTake(); ok {
p.lock.Unlock()
return resource, nil
}
if p.acquired < p.limit {
p.acquired++
p.lock.Unlock()
// Increase the acquired and unlock before acquiring resource may cause the pool becomes full in advance.
// So we should decrease the acquired if acquired failed.
defer func() {
if err != nil {
p.lock.Lock()
p.acquired--
p.lock.Unlock()
}
}()
return p.acquire()
}
if p.fastFailed {
p.lock.Unlock()
return resource, p.newPoolFullErr(ctx)
}
p.waiting++
p.lock.Unlock()
resource, err = p.waitToTake(ctx)
p.lock.Lock()
p.waiting--
p.lock.Unlock()
return resource, err
}
// Status returns the status of the pool.
func (p *Pool[T]) Status() Status {
p.lock.RLock()
defer p.lock.RUnlock()
var status Status
status.Limit = p.limit
status.Acquired = p.acquired
status.Idle = uint64(len(p.resources))
status.Waiting = p.waiting
return status
}
func (p *Pool[T]) releaseResources() error {
for i := uint64(0); i < p.acquired; i++ {
resource := <-p.resources
if err := p.release(resource); err != nil {
return err
}
}
return nil
}
// Close closes pool and releases all resources.
func (p *Pool[T]) Close() error {
p.lock.Lock()
defer p.lock.Unlock()
if p.closed {
return nil
}
if err := p.releaseResources(); err != nil {
return err
}
p.acquired = 0
p.waiting = 0
p.closed = true
close(p.resources)
return nil
}