-
Notifications
You must be signed in to change notification settings - Fork 318
/
worker.go
265 lines (226 loc) · 5.37 KB
/
worker.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Copyright 2019 The Vearch Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package routine
import (
"context"
"errors"
"fmt"
"runtime"
"strings"
"sync"
"github.com/vearch/vearch/util/log"
"github.com/vearch/vearch/util/atomic"
"github.com/vearch/vearch/util/multierror"
)
const (
_asyncWorkName = "[async]"
_daemonWorkName = "[daemon]"
)
var (
errUnavailable = errors.New("service is unavailable")
globalWorker *worker
)
type closeFunc func() error
func init() {
globalWorker = newWorker()
}
func LogPanic(r interface{}) {
callers := ""
for i := 0; true; i++ {
_, file, line, ok := runtime.Caller(i)
if !ok {
break
}
callers = callers + fmt.Sprintf("%v:%v\n", file, line)
}
log.Error("Recovered from panic: %#v (%v)\n%v", r, r, callers)
}
// RunWork run the func in the same routine
func RunWork(name string, f func() error, panicHandler ...func(interface{})) error {
if !globalWorker.workPrelude(name, false) {
return errUnavailable
}
defer func() {
if r := recover(); r != nil {
globalWorker.workPostlude(name, false)
if len(panicHandler) > 0 {
for _, fn := range panicHandler {
fn(r)
}
}
LogPanic(r)
} else {
globalWorker.workPostlude(name, false)
}
}()
return f()
}
// RunWorkAsync run the func in new routine
func RunWorkAsync(name string, f func(), panicHandler ...func(interface{})) error {
name = _asyncWorkName + name
if !globalWorker.workPrelude(name, false) {
return errUnavailable
}
GoWork(func() {
defer func() {
if r := recover(); r != nil {
globalWorker.workPostlude(name, false)
if len(panicHandler) > 0 {
for _, fn := range panicHandler {
fn(r)
}
}
LogPanic(r)
} else {
globalWorker.workPostlude(name, false)
}
}()
f()
})
return nil
}
// RunWorkDaemon run the func in new routine and run until stop
func RunWorkDaemon(name string, f func(), quit <-chan struct{}) error {
name = _daemonWorkName + name
if !globalWorker.workPrelude(name, true) {
return errUnavailable
}
go func() {
defer globalWorker.workPostlude(name, true)
for {
select {
case <-globalWorker.stopping:
return
case <-quit:
return
default:
func() {
defer func() {
if r := recover(); r != nil {
LogPanic(r)
}
}()
f()
}()
}
}
}()
return nil
}
// Stop stop the service and wait all worker exit
func Stop() error {
globalWorker.rwMu.Lock()
select {
case <-globalWorker.stopping:
globalWorker.rwMu.Unlock()
return nil
default:
close(globalWorker.stopping)
for _, cancel := range globalWorker.cancels {
cancel()
}
}
globalWorker.rwMu.Unlock()
globalWorker.stopWG.Wait()
globalWorker.rwMu.RLock()
defer globalWorker.rwMu.RUnlock()
merr := &multierror.MultiError{}
for _, c := range globalWorker.closers {
merr.Append(c())
}
close(globalWorker.stopped)
return merr.ErrorOrNil()
}
// ShouldStop return the service is stopping
func ShouldStop() <-chan struct{} {
return globalWorker.stopping
}
// IsStopped return the service has stopped
func IsStopped() <-chan struct{} {
return globalWorker.stopped
}
// WorkNum return current number of works
func WorkNum() int64 {
return globalWorker.numWorks.Get()
}
// AddCloser add close hook
func AddCloser(c closeFunc) {
globalWorker.rwMu.Lock()
globalWorker.closers = append(globalWorker.closers, c)
globalWorker.rwMu.Unlock()
}
// AddCancel addcancel hook
func AddCancel(cancel func()) (ctx context.Context) {
globalWorker.rwMu.Lock()
globalWorker.cancels = append(globalWorker.cancels, cancel)
globalWorker.rwMu.Unlock()
return
}
// DebugString return the debug string
func DebugString() string {
num := 0
works := make([]string, 0, 16)
globalWorker.workMap.Range(func(key, value interface{}) bool {
works = append(works, fmt.Sprintf("[%s , %d]", key, value.(*atomic.AtomicInt64).Get()))
num++
return true
})
return fmt.Sprintf("[%d]works:\n%s", num, strings.Join(works, "\n"))
}
type worker struct {
stopping chan struct{}
stopped chan struct{}
stopWG sync.WaitGroup
workMap *sync.Map
numWorks *atomic.AtomicInt64
rwMu sync.RWMutex
closers []closeFunc
cancels []func()
}
func newWorker() *worker {
return &worker{
stopping: make(chan struct{}),
stopped: make(chan struct{}),
workMap: &sync.Map{},
numWorks: atomic.NewAtomicInt64(0),
}
}
func (s *worker) workPrelude(name string, daemon bool) bool {
select {
case <-s.stopping:
return false
default:
if !daemon {
s.stopWG.Add(1)
}
wnum, _ := s.workMap.Load(name)
if wnum == nil {
wnum, _ = s.workMap.LoadOrStore(name, atomic.NewAtomicInt64(0))
}
wnum.(*atomic.AtomicInt64).Incr()
s.numWorks.Incr()
return true
}
}
func (s *worker) workPostlude(name string, daemon bool) {
if !daemon {
s.stopWG.Done()
}
if wnum, _ := s.workMap.Load(name); wnum != nil {
if wnum.(*atomic.AtomicInt64).Decr() <= 0 {
s.workMap.Delete(name)
}
}
s.numWorks.Decr()
}