-
Notifications
You must be signed in to change notification settings - Fork 10
/
component.go
177 lines (150 loc) · 3.43 KB
/
component.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package daemon
import (
"fmt"
"sync"
"time"
log "github.com/Sirupsen/logrus"
)
// A StartFunc starts a component
type StartFunc func(ErrorSink) Component
// A component is simply something that can be stopped
type Component interface {
// Stop the component. The implementation of this component
// should be synchronous: When it returns, all resources of
// the component should have been released, and any activity
// associated with the component has ceased. The component
// supervisor may call this function at most once.
Stop()
}
// Supervise a component, restarting it upon errors
func Restart(interval time.Duration, start StartFunc) StartFunc {
f := func(errs ErrorSink, stop <-chan struct{}, finished chan<- struct{}) {
defer close(finished)
stopped := false
for {
errs := NewErrorSink()
tStart := time.Now()
d := start(errs)
select {
case <-stop:
stopped = true
case err := <-errs:
log.WithError(err).Error()
}
if d != nil {
t := time.AfterFunc(10*time.Second, func() {
errs.Post(fmt.Errorf("timed out stopping component"))
})
d.Stop()
t.Stop()
}
if stopped {
return
}
wait := tStart.Add(interval).Sub(time.Now())
if wait > 0 {
t := time.NewTimer(wait)
select {
case <-stop:
return
case <-t.C:
}
t.Stop()
}
}
}
return func(errs ErrorSink) Component {
stop := make(chan struct{})
finished := make(chan struct{})
go f(errs, stop, finished)
return &stopper{stop: stop, finished: finished}
}
}
func SimpleComponent(f func(stop <-chan struct{}, errs ErrorSink)) StartFunc {
return func(errs ErrorSink) Component {
stop := make(chan struct{})
finished := make(chan struct{})
go func() {
defer close(finished)
f(stop, errs)
}()
return &stopper{stop: stop, finished: finished}
}
}
type stopper struct {
stop chan<- struct{}
finished <-chan struct{}
stopped sync.Once
}
func (stopper *stopper) Stop() {
stopper.stopped.Do(func() {
close(stopper.stop)
})
<-stopper.finished
}
// Because Component only has a single method, it's convenient to have
// a way to dress up a stop function as a Component. This also takes
// care of ensuring that the stop function is only called once.
func StopFunc(f func()) Component {
return &stopFunc{f: f}
}
type stopFunc struct {
f func()
stopped sync.Once
}
func (sf *stopFunc) Stop() {
sf.stopped.Do(sf.f)
}
func Aggregate(startFuncs ...StartFunc) StartFunc {
if len(startFuncs) == 1 {
return startFuncs[0]
}
return func(errs ErrorSink) Component {
stopFuncs := make([]func(), len(startFuncs))
for i := range startFuncs {
stopFuncs[i] = startFuncs[i](errs).Stop
}
return StopFunc(func() { Par(stopFuncs...) })
}
}
func Par(funcs ...func()) {
done := make(chan struct{}, len(funcs))
for _, f := range funcs {
go func(f func()) {
defer func() { done <- struct{}{} }()
f()
}(f)
}
for range funcs {
<-done
}
}
func Reset(reset <-chan struct{}, startFunc StartFunc) StartFunc {
return SimpleComponent(func(stop <-chan struct{}, errs ErrorSink) {
// Wait for an initial reset signal
select {
case <-stop:
return
case <-reset:
}
for {
// Drain redundant restart signals
select {
case <-reset:
continue
default:
}
c := startFunc(errs)
select {
case <-stop:
c.Stop()
return
case <-reset:
}
c.Stop()
}
})
}
func NullStartFunc(ErrorSink) Component {
return StopFunc(func() {})
}