-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.go
153 lines (122 loc) · 2.45 KB
/
app.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
package rwe
import (
"context"
mathRand "math/rand"
"os"
"os/signal"
"sync"
"sync/atomic"
"syscall"
"time"
"github.com/benbjohnson/clock"
"github.com/uptrace/go-realworld-example-app/xconfig"
"github.com/sirupsen/logrus"
"golang.org/x/exp/rand"
)
var Clock = clock.New()
var (
WaitGroup sync.WaitGroup
ExitCh = make(chan struct{})
exiting uint32
)
func Exiting() bool {
return atomic.LoadUint32(&exiting) == 1
}
func Running() bool {
return !Exiting()
}
//------------------------------------------------------------------------------
var (
Config *xconfig.Config
Ctx context.Context
)
func Init(ctx context.Context, cfg *xconfig.Config) context.Context {
if Config != nil {
panic("not reached")
}
rand.Seed(uint64(time.Now().UnixNano()))
mathRand.Seed(time.Now().UnixNano())
Config = cfg
Ctx = ctx
callOnInit(ctx)
setupOtel(ctx)
return ctx
}
//------------------------------------------------------------------------------
type hookFn func(context.Context)
var onInit []hookFn
func OnInit(fn hookFn) {
if Ctx != nil {
fn(Ctx)
return
}
onInit = append(onInit, fn)
}
func callOnInit(ctx context.Context) {
run(ctx, onInit)
onInit = nil
}
//------------------------------------------------------------------------------
var (
primarily []hookFn
secondary []hookFn
)
func Exit(ctx context.Context) {
if !atomic.CompareAndSwapUint32(&exiting, 0, 1) {
return
}
close(ExitCh)
if waitTimeout(&WaitGroup, 30*time.Second) {
logrus.WithContext(ctx).Info("waitTimeout")
}
run(ctx, primarily)
run(ctx, secondary)
}
func waitTimeout(wg *sync.WaitGroup, timeout time.Duration) bool {
c := make(chan struct{})
go func() {
defer close(c)
wg.Wait()
}()
select {
case <-c:
return false // completed normally
case <-time.After(timeout):
return true // timed out
}
}
func run(ctx context.Context, hookFns []hookFn) {
var wg sync.WaitGroup
wg.Add(len(hookFns))
for _, h := range hookFns {
go func(h hookFn) {
defer wg.Done()
h(ctx)
}(h)
}
wg.Wait()
}
func OnExit(h hookFn) {
primarily = append(primarily, h)
}
func OnExitSecondary(h hookFn) {
secondary = append(secondary, h)
}
//------------------------------------------------------------------------------
func WaitExitSignal() os.Signal {
ch := make(chan os.Signal, 3)
signal.Notify(
ch,
syscall.SIGINT,
syscall.SIGQUIT,
syscall.SIGTERM,
)
return <-ch
}
func IsDebug() bool {
switch Config.Env {
case "prod":
return false
}
return true
}