-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_time.go
More file actions
84 lines (72 loc) · 2.59 KB
/
run_time.go
File metadata and controls
84 lines (72 loc) · 2.59 KB
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package task
import (
"context"
"time"
)
func (t Task) timed(dur func(time.Duration) time.Duration, e func(error) bool) Task {
return func(ctx context.Context) error {
begin := time.Now()
err := t.Run(ctx)
wait := dur(time.Since(begin))
if wait > 0 && e(err) {
er := Sleep(wait).Run(ctx)
if err == nil {
err = er
}
}
return err
}
}
func delta(d time.Duration) func(time.Duration) time.Duration {
return func(dur time.Duration) time.Duration { return d - dur }
}
// Timed wraps t into a task ensures that it is not returned before dur passed.
//
// It focuses on "How long I should wait before returning". Take a look at example
// for how it works.
//
// If you're looking for rate limiting solution, you should take a look at "rated"
// subdirectory.
func (t Task) Timed(dur time.Duration) Task {
return t.TimedF(delta(dur))
}
// TimedF is like Timed, but use function instead.
//
// The function accepts actual execution time, and returns how long it should wait.
func (t Task) TimedF(f func(time.Duration) time.Duration) Task {
return t.timed(f, func(_ error) bool { return true })
}
// TimedDone is like Timed, but limits only successful run.
//
// If you're looking for rate limiting solution, you should take a look at "rated"
// subdirectory.
func (t Task) TimedDone(dur time.Duration) Task {
return t.TimedDoneF(delta(dur))
}
// TimedDoneF is like TimedDone, but use function instead.
//
// The function accepts actual execution time, and returns how long it should wait.
func (t Task) TimedDoneF(f func(time.Duration) time.Duration) Task {
return t.timed(f, func(e error) bool { return e == nil })
}
// TimedFail is like Timed, but limits only failed run.
//
// If you're looking for rate limiting solution, you should take a look at "rated"
// subdirectory.
func (t Task) TimedFail(dur time.Duration) Task {
return t.TimedFailF(delta(dur))
}
// TimedFailF is like TimedFail, but use function instead.
//
// The function accepts actual execution time, and returns how long it should wait.
func (t Task) TimedFailF(f func(time.Duration) time.Duration) Task {
return t.timed(f, func(e error) bool { return e != nil })
}
// FixedDur creats a func to be used in TimedF, TimedDoneF and TimedFailF, which
// does not take actual execution time into consideration.
func FixedDur(dur time.Duration) func(time.Duration) time.Duration {
return func(_ time.Duration) time.Duration { return dur }
}