-
Notifications
You must be signed in to change notification settings - Fork 0
/
looping.go
97 lines (85 loc) · 1.98 KB
/
looping.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
86
87
88
89
90
91
92
93
94
95
96
97
// 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 ctxroutines
import "context"
type loopRunner struct {
Runner
cb func(error)
term func(error) (err error, term bool)
}
func (r *loopRunner) Run() (err error) {
for {
var term bool
select {
case <-r.Context().Done():
return r.Context().Err()
default:
}
err = r.Runner.Run()
err, term = r.term(err)
if term {
return
}
if err != nil {
r.cb(err)
}
}
}
// Retry creates a Runner runs r until it returns nil
func Retry(r Runner) (ret Runner) {
return RetryWithCB(r, func(error) {})
}
// RetryWithCB creates a Runner runs r until it returns nil
//
// It calls cb if r returns error.
//
// You have to call Cancel() to release resources.
func RetryWithCB(r Runner, cb func(error)) (ret Runner) {
return &loopRunner{
Runner: r,
cb: cb,
term: func(e error) (err error, term bool) {
if e == nil || e == context.Canceled {
return e, true
}
return e, false
},
}
}
// RetryWithChan is shortcut for RetryWithCB(r, func(e error) { ch <- e })
//
// You have to call Cancel() to release resources.
func RetryWithChan(r Runner, ch chan<- error) (ret Runner) {
return RetryWithCB(r, func(e error) { ch <- e })
}
// TilErr creates a Runner runs r until it returns any error
//
// You have to call Cancel() to release resources.
func TilErr(r Runner) (ret Runner) {
return &loopRunner{
Runner: r,
cb: func(error) {},
term: func(e error) (err error, term bool) {
if e == nil {
return
}
return e, true
},
}
}
// Loop creates a Runner that runs r until canceled
//
// You have to call Cancel() to release resources.
func Loop(r Runner) (ret Runner) {
return &loopRunner{
Runner: r,
cb: func(error) {},
term: func(e error) (err error, term bool) {
if e != context.Canceled {
return
}
return e, true
},
}
}