-
Notifications
You must be signed in to change notification settings - Fork 0
/
safe_example_test.go
100 lines (70 loc) · 2.13 KB
/
safe_example_test.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
package batch_test
import (
"context"
"errors"
"log"
"sync"
"nikand.dev/go/batch"
)
type (
SafeService struct {
sum int // state we collect to commit together
bc *batch.Coordinator[int] // [int] is the result value type, set to struct{} if don't need it
}
contextKeySafe struct{}
)
func NewSafeService() *SafeService {
s := &SafeService{}
s.bc = batch.New(s.commit)
return s
}
func (s *SafeService) commit(ctx context.Context) (int, error) {
// suppose some heavy operation here
// update a file or write to db
log.Printf("* * * commit %2d * * *", s.sum)
return s.sum, nil
}
func (s *SafeService) DoWork(ctx context.Context, data int) (int, error) {
b := batch.By(s.bc)
defer b.Exit() // it's like Mutex.Unlock, but safely works even if we didn't enter
_ = 0 // Must be called with defer to outlive panics
b.QueueIn() // let others know we are going to join
_ = data // prepare data
idx := b.Enter(true) // true for blocking, false if we want to leave instead of waiting
if idx < 0 { // we haven't entered the batch in non blocking mode
return 0, errors.New("not this time") // we have to leave in that case
}
if idx == 0 { // we are first in the batch, reset the state
s.sum = 0
log.Printf("* * * reset batch * * *")
}
log.Printf("worker %2d got in with index %2d", ctx.Value(contextKeySafe{}), idx)
s.sum += data // add our work to the batch
// only one of return/Cancel/Commit must be called and only once
res, err := b.Commit(ctx)
if err != nil { // batch failed, each worker in it will get the same error
return 0, err
}
log.Printf("worker %2d got result %v %v", ctx.Value(contextKeySafe{}), res, err)
// if we are here, all of the workers have their work committed
return res, nil
}
func ExampleBatch() {
const jobs = 5
s := NewSafeService()
// let's spin up some workers
var wg sync.WaitGroup
for j := 0; j < jobs; j++ {
j := j
wg.Add(1)
go func() {
defer wg.Done()
ctx := context.Background() // passed to commit function
ctx = context.WithValue(ctx, contextKeySafe{}, j)
res, err := s.DoWork(ctx, 1)
_, _ = res, err
}()
}
wg.Wait()
// Output:
}