-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathservicer.go
170 lines (148 loc) · 3.01 KB
/
servicer.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
package stf
import (
"errors"
"sync"
)
var (
ErrServiceAlreadyStarted = errors.New("Service already started")
ErrServiceNotStarted = errors.New("Service not started")
)
type Servicer interface {
Start() error
Stop() error
Wait() error
}
type multiServ struct {
ss []Servicer
}
func (m *multiServ) Start() error {
for _, s := range m.ss {
if err := s.Start(); err != nil {
return err
}
}
return nil
}
func (m *multiServ) Stop() error {
var err error
for _, s := range m.ss {
if er := s.Stop(); er != nil {
err = er
}
}
return err
}
func (m *multiServ) Wait() error {
errC := make(chan error, len(m.ss))
for _, s := range m.ss {
go func(s Servicer) {
errC <- s.Wait()
}(s)
}
return <-errC
}
// Combine servicers into one servicer
func MultiServicer(ss ...Servicer) Servicer {
return &multiServ{ss}
}
// Mutex
const (
_ACTION_START = iota
_ACTION_STOP
)
type safeMixin struct {
mu sync.Mutex
started bool
}
func (t *safeMixin) safeDo(action int, f func() error) error {
t.mu.Lock()
defer t.mu.Unlock()
if t.started && action == _ACTION_START {
return ErrServiceAlreadyStarted
}
if !t.started && action == _ACTION_STOP {
return ErrServiceNotStarted
}
t.started = (action == _ACTION_START)
return f()
}
func (t *safeMixin) IsStarted() bool {
return t.started
}
// Mutex retry
// type safeErrorMixin struct {
// safeMixin
// errorMixin
// errC chan error
// maxRetry int
// f func() error
// startFunc func() error
// stopFunc func() error
// }
// func (t *safeErrorMixin) safeRetryDo(maxRetry int, dur time.Duration,
// startFunc func() error, stopFunc func() error) error {
// return t.safeDo(action, func() error {
// t.maxRetry = maxRetry
// t.startFunc = startFunc
// t.stopFunc = stopFunc
// t.errC = make(chan error, 1)
// return t.doWithRetry()
// })
// }
// func (t *safeErrorMixin) doWithRetry() error {
// if err := t.startFunc(); err != nil {
// return err
// }
// go func() {
// leftRetry := t.maxRetry
// for leftRetry > 0 {
// startTime := time.Now()
// err := t.Wait()
// if err != nil {
// leftRetry -= 1
// if time.Since(startTime) > 20*time.Second {
// leftRetry = t.maxRetry
// }
// t.stopFunc()
// // t.Stop()
// }
// }
// }()
// }
// // if exit retry again
// func (t *safeErrorMixin) Wait() error {
// err := t.errorMixin.Wait()
// // errC := GoFunc(t.errorMixin.Wait)
// // select {
// // case <- errC:
// // }
// return err
// }
// func (t *mutexRetryMixin) Wait() error {
// }
// Mixin helper to easy write Servicer
type errorMixin struct {
errC chan error
once *sync.Once
wg *sync.WaitGroup
err error
}
// this func must be called before use other functions
func (e *errorMixin) resetError() {
e.once = &sync.Once{}
e.wg = &sync.WaitGroup{}
e.wg.Add(1)
}
func (e *errorMixin) Wait() error {
e.wg.Wait()
return e.err
}
func (e *errorMixin) doneError(err error) {
e.once.Do(func() {
e.err = err
e.wg.Done()
})
}
func (e *errorMixin) doneNilError() {
e.doneError(nil)
}