-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpoolset.go
198 lines (167 loc) · 3.72 KB
/
poolset.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
package ephemerald
import (
"context"
"sync"
"github.com/Sirupsen/logrus"
"github.com/boz/ephemerald/config"
"github.com/boz/ephemerald/params"
)
type PoolSet interface {
Checkout(name ...string) (params.Set, error)
CheckoutWith(ctx context.Context, name ...string) (params.Set, error)
ReturnAll(params.Set)
Return(name string, item Item)
WaitReady() error
Stop() error
}
// ugh.
type poolSet struct {
pools map[string]Pool
log logrus.FieldLogger
}
func NewPoolSet(log logrus.FieldLogger, ctx context.Context, configs []*config.Config) (PoolSet, error) {
pools := make(map[string]Pool)
log = log.WithField("component", "pool-set")
var err error
var pool Pool
// create pools
for _, cfg := range configs {
pool, err = NewPoolWithContext(ctx, cfg)
if err != nil {
break
}
pools[cfg.Name] = pool
}
if err == nil {
return &poolSet{
pools: pools,
log: log,
}, nil
}
// if creation failed for any pool, stop active pools
var wg sync.WaitGroup
wg.Add(len(pools))
for _, pool := range pools {
go func(pool Pool) {
defer wg.Done()
pool.Stop()
}(pool)
}
wg.Wait()
return nil, err
}
func (ps *poolSet) CheckoutWith(ctx context.Context, names ...string) (params.Set, error) {
type pscheckout struct {
name string
params params.Params
err error
}
// checkout from each pool
ch := make(chan pscheckout)
var wg sync.WaitGroup
for name, pool := range ps.poolsForCheckout(names...) {
wg.Add(1)
go func(name string, pool Pool) {
defer wg.Done()
params, err := pool.CheckoutWith(ctx)
ch <- pscheckout{name, params, err}
}(name, pool)
}
go func() {
wg.Wait()
close(ch)
}()
// collect results
set := params.Set{}
errors := make([]error, 0)
for entry := range ch {
if entry.err != nil {
ps.log.WithError(entry.err).
WithField("pool", entry.name).
Error("checkout error")
errors = append(errors, entry.err)
continue
}
set[entry.name] = entry.params
}
// if any checkout failed, return completed checkouts
if len(errors) > 0 {
ps.ReturnAll(set)
return nil, errors[0]
}
return set, nil
}
func (ps *poolSet) Checkout(names ...string) (params.Set, error) {
return ps.CheckoutWith(context.Background(), names...)
}
func (ps *poolSet) ReturnAll(set params.Set) {
var wg sync.WaitGroup
wg.Add(len(set))
for name, p := range set {
go func(name string, p params.Params) {
defer wg.Done()
ps.Return(name, p)
}(name, p)
}
wg.Wait()
}
func (ps *poolSet) Return(name string, item Item) {
if pool, ok := ps.pools[name]; ok {
pool.Return(item)
}
}
func (ps *poolSet) WaitReady() error {
type pswait struct {
name string
pool Pool
err error
}
ch := make(chan pswait)
// call WaitReady on all pools
for name, pool := range ps.pools {
go func(name string, pool Pool) {
ch <- pswait{name, pool, pool.WaitReady()}
}(name, pool)
}
// collect results
var err error
for count := 0; count < len(ps.pools); count++ {
if response := <-ch; response.err != nil {
ps.log.WithError(response.err).
WithField("pool", response.name).
Error("WaitReady failed")
if err == nil {
err = response.err
}
}
}
return err
}
func (ps *poolSet) Stop() error {
var wg sync.WaitGroup
wg.Add(len(ps.pools))
// stop all pools
for name, pool := range ps.pools {
go func(name string, pool Pool) {
defer wg.Done()
pool.Stop()
}(name, pool)
}
// wait for all pools to stop
wg.Wait()
return nil
}
func (ps *poolSet) poolsForCheckout(names ...string) map[string]Pool {
// if no names given, all pools returned
if len(names) == 0 {
return ps.pools
}
// else select the pools by name
pools := make(map[string]Pool)
for _, name := range names {
if pool, ok := ps.pools[name]; ok {
pools[name] = pool
}
}
return pools
}