This repository has been archived by the owner on Apr 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconcurrency.go
70 lines (59 loc) · 1.61 KB
/
concurrency.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
package loadshed
import (
"sync"
"sync/atomic"
"github.com/asecurityteam/rolling"
)
// WaitGroup wraps a sync.WaitGroup to make it usable as a load shedding tool.
type WaitGroup struct {
*sync.WaitGroup
concurrent *int32
}
// NewWaitGroup generates a specialised WaitGroup that tracks the number of
// concurrent operations. This implementation also satisfies the Aggregator
// interface from github.com/asecurityteam/rolling so that this can be fed into
// a calculation of system health.
func NewWaitGroup() *WaitGroup {
var w = &WaitGroup{
WaitGroup: &sync.WaitGroup{},
concurrent: new(int32),
}
return w
}
// Aggregate returns the current concurrency value
func (c *WaitGroup) Aggregate() *rolling.Aggregate {
return &rolling.Aggregate{
Source: nil,
Name: "WaitGroup",
Value: float64(atomic.LoadInt32(c.concurrent)),
}
}
// Add some number of concurrent operations.
func (c *WaitGroup) Add(delta int) {
c.WaitGroup.Add(delta)
atomic.AddInt32(c.concurrent, int32(delta))
}
// Done marks an operation as complete and removes the tracking.
func (c *WaitGroup) Done() {
c.WaitGroup.Done()
atomic.AddInt32(c.concurrent, -1)
}
// Wait for all operations to complete.
func (c *WaitGroup) Wait() {
c.WaitGroup.Wait()
}
type concurrencyDecorator struct {
wg *WaitGroup
}
func (h *concurrencyDecorator) Wrap(next func() error) func() error {
return func() error {
h.wg.Add(1)
defer h.wg.Done()
return next()
}
}
// newConcurrencyTrackingDecorator tracks concurrent actions using the
// given WaitGroup.
func newConcurrencyTrackingDecorator(wg *WaitGroup) wrapper {
return &concurrencyDecorator{wg}
}