/
task.go
75 lines (60 loc) · 1.27 KB
/
task.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
package loadtester
import (
"context"
"time"
)
//
// interfaces
//
// Doer is a basic task unit
//
// If your Doer also implements Retryer
// then note that Doer can be run again in a different thread
// if your worker size is greater than one.
//
// If you want your task to have a retry upper bound then implement
// DoRetryChecker
type Doer interface {
Do(ctx context.Context, workerID int) error
}
type Retryer interface {
Retry(ctx context.Context, workerID int, prevErr error) error
}
// DoRetryer interface is useful for tasks that have no retry count upper bound
//
// If you need to have a retry upper bound, then have your task implement DoRetryChecker
type DoRetryer interface {
Doer
Retryer
}
type RetryChecker interface {
CanRetry(ctx context.Context, workerID int, prevErr error) bool
}
type DoRetryChecker interface {
DoRetryer
RetryChecker
}
//
// structs
//
type retryTask struct {
DoRetryer
err error
}
func (rt *retryTask) Do(ctx context.Context, workerID int) error {
return rt.DoRetryer.Retry(ctx, workerID, rt.err)
}
type taskMeta struct {
IntervalID time.Time
SampleSize int
//
// rate gauges:
//
NumIntervalTasks int
Lag time.Duration
}
type taskWithMeta struct {
doer Doer
enqueueTime time.Time
meta taskMeta
}