load.go94 lines, 2.4 KB
| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "math/rand/v2" |
| 6 | "sync" |
| 7 | "sync/atomic" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/google/uuid" |
| 11 | ) |
| 12 | |
| 13 | // A sent transfer: the key we sent it with, and what the API said. |
| 14 | type sent struct { |
| 15 | key string |
| 16 | id uuid.UUID // the transfer's ID, or uuid.Nil: we never got an answer |
| 17 | replayed bool // a retry found it already there |
| 18 | } |
| 19 | |
| 20 | // A load sends transfers between customers at a steady rate, like clients |
| 21 | // do, whatever happens to the system meanwhile, and keeps track of each one. |
| 22 | type load struct { |
| 23 | client *client |
| 24 | customers []customer |
| 25 | elsewhere customer // a wallet in another currency: no transfer to it can complete |
| 26 | |
| 27 | mu sync.Mutex |
| 28 | sent []sent |
| 29 | started atomic.Int64 |
| 30 | } |
| 31 | |
| 32 | // run sends rate transfers a second for d, then waits for every one of them |
| 33 | // to get a final answer, or to give up. |
| 34 | func (l *load) run(ctx context.Context, rate int, d time.Duration) { |
| 35 | var wg sync.WaitGroup |
| 36 | defer wg.Wait() |
| 37 | tick := time.NewTicker(time.Second / time.Duration(rate)) |
| 38 | defer tick.Stop() |
| 39 | for end := time.After(d); ; { |
| 40 | select { |
| 41 | case <-ctx.Done(): |
| 42 | return |
| 43 | case <-end: |
| 44 | return |
| 45 | case <-tick.C: |
| 46 | // Each one keeps trying after the load stops, for two minutes: |
| 47 | // longer than any fault we inject. |
| 48 | wg.Go(func() { |
| 49 | ctx, cancel := context.WithTimeout(ctx, 2*time.Minute) |
| 50 | defer cancel() |
| 51 | l.transfer(ctx) |
| 52 | }) |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // transfer sends between one and five dollars from a customer to another, |
| 58 | // or, once in fifty, to the wallet in another currency. |
| 59 | func (l *load) transfer(ctx context.Context) { |
| 60 | l.started.Add(1) |
| 61 | n := len(l.customers) |
| 62 | i := rand.N(n) |
| 63 | from, to := l.customers[i], l.customers[(i+1+rand.N(n-1))%n].wallet |
| 64 | if rand.N(50) == 0 { |
| 65 | to = l.elsewhere.wallet // held, then refused: the saga gives the money back |
| 66 | } |
| 67 | amount := 100 + rand.N(401) |
| 68 | r := request{ |
| 69 | path: "/v1/transfers", |
| 70 | token: from.token, |
| 71 | key: uuid.NewString(), |
| 72 | body: map[string]any{"from": from.wallet, "to": to, "amount": amount, "currency": "USD"}, |
| 73 | } |
| 74 | var t struct { |
| 75 | ID uuid.UUID `json:"id"` |
| 76 | } |
| 77 | replayed, err := l.client.send(ctx, r, &t) |
| 78 | s := sent{key: r.key} |
| 79 | if err == nil { |
| 80 | s.id, s.replayed = t.ID, replayed |
| 81 | } |
| 82 | l.mu.Lock() |
| 83 | defer l.mu.Unlock() |
| 84 | l.sent = append(l.sent, s) |
| 85 | } |
| 86 | |
| 87 | // wallets returns the IDs of every wallet in the load. |
| 88 | func (l *load) wallets() []uuid.UUID { |
| 89 | ids := []uuid.UUID{l.elsewhere.wallet} |
| 90 | for _, c := range l.customers { |
| 91 | ids = append(ids, c.wallet) |
| 92 | } |
| 93 | return ids |
| 94 | } |