/
pool.go
56 lines (46 loc) · 1.27 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
// Package sem provides safe concurrent access to a bounded pool of resources.
package sem
import (
"context"
"sync"
"golang.org/x/sync/semaphore"
)
// Pool provides concurrent access to a fixed bounded pool of resources
type Pool struct {
items []interface{}
mu sync.Mutex // protects items
sem *semaphore.Weighted
}
// NewPool builds a pool providing concurrent access to the given items.
func NewPool(items ...interface{}) *Pool {
return &Pool{
items: items,
sem: semaphore.NewWeighted(int64(len(items))),
}
}
// Acquire n items from the pool. Blocks until the requested items are available
// or the context is cancelled.
func (p *Pool) Acquire(ctx context.Context, n int) ([]interface{}, error) {
// Acquire weight n from the semaphore.
if err := p.sem.Acquire(ctx, int64(n)); err != nil {
return nil, err
}
// Take n items from the pool.
p.mu.Lock()
if len(p.items) < n {
panic("assertion failure: not enough items in pool")
}
items := p.items[:n]
p.items = p.items[n:]
p.mu.Unlock()
return items, nil
}
// Release returns items to the pool.
func (p *Pool) Release(items []interface{}) {
// Return items to the pool.
p.mu.Lock()
p.items = append(p.items, items...)
p.mu.Unlock()
// Release weight in the semaphore.
p.sem.Release(int64(len(items)))
}