/
optimistic.go
64 lines (52 loc) · 1.18 KB
/
optimistic.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
package main
import (
"log/slog"
"sync"
"sync/atomic"
"time"
retry "github.com/avast/retry-go/v4"
"github.com/hako/durafmt"
cli "github.com/urfave/cli/v2"
)
func optimisticAction(cCtx *cli.Context) error {
user := cCtx.String("user")
amount := cCtx.Float64("amount")
executions := cCtx.Int("executions")
repo, cleanup, err := initializeOptimistic()
if err != nil {
return err
}
defer cleanup()
var retries, errs uint64
start := time.Now()
var wg sync.WaitGroup
wg.Add(executions)
for i := 0; i < executions; i++ {
go func() {
defer wg.Done()
rerr := retry.Do(
func() error {
return repo.Deposit(cCtx.Context, user, amount)
},
retry.OnRetry(func(u uint, err error) {
atomic.AddUint64(&retries, 1)
}),
retry.MaxJitter(1*time.Second),
retry.DelayType(retry.RandomDelay),
retry.Attempts(10),
)
if rerr != nil {
atomic.AddUint64(&errs, 1)
}
}()
}
wg.Wait()
slog.Info("Done!",
slog.Int("executions", executions),
slog.Uint64("errors", errs),
slog.Int("success", executions-int(errs)),
slog.Uint64("retries", retries),
slog.String("duration", durafmt.ParseShort(time.Since(start)).String()),
)
return nil
}