-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool.go
121 lines (102 loc) · 2.83 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
package dyno
import (
"context"
"fmt"
ddb "github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/segmentio/ksuid"
"sync/atomic"
)
// NewPool creates a new pool with a context Session connection and limit
// the context is used for all Executions
func NewPool(ctx context.Context, client *ddb.Client, limit uint64) *Pool {
return &Pool{
ctx: ctx,
client: client,
limit: limit,
id: ksuid.New().String(),
}
}
// Pool is a batch request handler that spawns a number of workers to handle requests
type Pool struct {
ctx context.Context
client *ddb.Client // dynamoDB is the client
limit uint64 // limit is the max number of active tasks that can be running at the same time
taskCount uint64 // taskCount holds the count of active tasks and uses sync.atomic for getting and setting
id string // id allows us to identify pools easily (e.g. for logging purposes)
}
// Limit returns the Pool limit
func (p *Pool) Limit() uint64 {
return p.limit
}
// ID returns the Pool id
func (p *Pool) ID() string {
return p.id
}
// String implements Stringer
func (p *Pool) String() string {
cnt := p.ActiveCount()
return fmt.Sprintf("Pool <%s> %d running", p.id, cnt)
}
// addActive adds a cnt to the active process countCh Pool.active
func (p *Pool) addActive(cnt uint64) {
atomic.AddUint64(&p.taskCount, cnt)
}
// subActive subtracts a cnt from the active process countCh Pool.active
func (p *Pool) subActive(cnt uint64) {
atomic.AddUint64(&p.taskCount, -cnt)
}
// claimSlot waits for an available execution slot
func (p *Pool) claimSlot() error {
for {
select {
case <-p.ctx.Done():
return fmt.Errorf("could not claim pool slot: %v", p.ctx.Err())
default:
if atomic.LoadUint64(&p.taskCount) < p.limit {
p.addActive(1) // increment the active count since we got a slot
return nil
}
}
}
}
// ActiveCount returns number of active operations
func (p *Pool) ActiveCount() uint64 {
return atomic.LoadUint64(&p.taskCount)
}
// Do executes one or more Operations
func (p *Pool) Do(ops ...Operation) {
for _, op := range ops {
op.SetRunning() // flag operation as waiting
if err := p.claimSlot(); err != nil {
op.SetResponse(nil, err)
return
}
go func(op Operation) {
defer func() { p.subActive(1) }() // decrement the active count
op.InvokeDynoOperation(p.ctx, p.client)
}(op)
}
return
}
// Wait returns a chan that will be passed a struct when the pool has zero active tasks
// useful for when a pool is meant to be short lived with a set number of operations to run in a batch
func (p *Pool) Wait() <-chan struct{} {
out := make(chan struct{})
go func() {
defer func() {
out <- struct{}{}
close(out)
}()
for {
select {
case <-p.ctx.Done():
return
default:
if atomic.LoadUint64(&p.taskCount) == 0 {
return
}
}
}
}()
return out
}