-
Notifications
You must be signed in to change notification settings - Fork 0
/
routine.go
73 lines (58 loc) · 1.47 KB
/
routine.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
package graceful
import (
"context"
"fmt"
"runtime"
"sync"
)
type middlewareFunc func(next func(context.Context)) func(context.Context)
type logFunc func(message string)
var middlewares []middlewareFunc
var wg = sync.WaitGroup{}
var debugLogger logFunc
// EnableDebugLogging will print the file and line number for each started go routine
func EnableDebugLogging(logger logFunc) {
debugLogger = logger
}
// DisableDebugLogging removes the logger
func DisableDebugLogging() {
debugLogger = nil
}
// Wait for all go routines to finish
func Wait() {
wg.Wait()
}
// Run a new tracked go routine
func Run(routine func()) {
doRun(context.Background(), func(context.Context) {
routine()
})
}
// RunContext runs a new tracked go routine with a context
func RunContext(ctx context.Context, routine func(context.Context)) {
doRun(ctx, routine)
}
func doRun(ctx context.Context, routine func(context.Context)) {
wg.Add(1)
if debugLogger != nil {
if _, file, line, ok := runtime.Caller(2); ok {
debugLogger(fmt.Sprintf("Go routine started in: %v:%v", file, line))
}
}
go func() {
for _, mw := range middlewares {
routine = mw(routine)
}
routine(ctx)
wg.Done()
}()
}
// AddMiddleware to the stack
func AddMiddleware(mw middlewareFunc) {
// append new middleware to the front to preserve order of execution
middlewares = append([]middlewareFunc{mw}, middlewares...)
}
// ClearMiddlewares from the stack
func ClearMiddlewares() {
middlewares = []middlewareFunc{}
}