-
Notifications
You must be signed in to change notification settings - Fork 244
/
Copy pathagent.go
85 lines (73 loc) · 1.98 KB
/
agent.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
// (c) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package txs
import (
"context"
"errors"
"fmt"
)
// TxSequence provides an interface to return a channel of transactions.
// The sequence is responsible for closing the channel when there are no further
// transactions.
type TxSequence[T any] interface {
Chan() <-chan T
}
// Worker defines the interface for issuance and confirmation of transactions.
// The caller is responsible for calling Close to cleanup resources used by the
// worker at the end of the simulation.
type Worker[T any] interface {
IssueTx(ctx context.Context, tx T) error
ConfirmTx(ctx context.Context, tx T) error
Close(ctx context.Context) error
}
// Execute the work of the given agent.
type Agent[T any] interface {
Execute(ctx context.Context) error
}
// issueNAgent issues and confirms a batch of N transactions at a time.
type issueNAgent[T any] struct {
sequence TxSequence[T]
worker Worker[T]
n uint64
}
// NewIssueNAgent creates a new issueNAgent
func NewIssueNAgent[T any](sequence TxSequence[T], worker Worker[T], n uint64) Agent[T] {
return &issueNAgent[T]{
sequence: sequence,
worker: worker,
n: n,
}
}
// Execute issues txs in batches of N and waits for them to confirm
func (a issueNAgent[T]) Execute(ctx context.Context) error {
if a.n == 0 {
return errors.New("batch size n cannot be equal to 0")
}
txChan := a.sequence.Chan()
for {
var (
txs = make([]T, 0, a.n)
tx T
ok bool
)
for i := uint64(0); i < a.n; i++ {
select {
case tx, ok = <-txChan:
if !ok {
return a.worker.Close(ctx)
}
case <-ctx.Done():
return ctx.Err()
}
if err := a.worker.IssueTx(ctx, tx); err != nil {
return fmt.Errorf("failed to issue transaction %d: %w", len(txs), err)
}
txs = append(txs, tx)
}
for i, tx := range txs {
if err := a.worker.ConfirmTx(ctx, tx); err != nil {
return fmt.Errorf("failed to await transaction %d: %w", i, err)
}
}
}
}