-
Notifications
You must be signed in to change notification settings - Fork 0
/
msa.go
229 lines (190 loc) · 5.37 KB
/
msa.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
package msa
import (
"context"
"log"
"os"
"os/signal"
"time"
"github.com/go-god/gdi"
"github.com/go-god/gdi/factory"
"github.com/go-god/msa/config"
"github.com/go-god/msa/provides"
)
// initializer init interface
type initializer interface {
Init() error
}
// starter start interface
type starter interface {
Start() error
}
// stoppable stop interface
type stoppable interface {
Stop()
}
// Engine application engine
type Engine struct {
interruptSignals []os.Signal // interrupt signals
gracefulWait time.Duration // graceful exit time
signal chan os.Signal // recv interrupt signals
injectValues []*gdi.Object // inject objects
injector gdi.Injector // dip inject interface
invokeFunc []interface{} // invoke func
providers []provides.Provider // all provides
stopCh chan struct{} // stop chan,if you call Stop() application will exit
// config provider these are optional parameters
configDir string // config dirname
configFile string // config file
configInterface config.ConfigInterface // config read interface
configProvider provides.ConfigProvider // all provides.ConfigProvider
}
// engine default engine
var engine *Engine
// Start create an engine and run application.
func Start(opts ...Option) {
engine = New(opts...)
engine.Start()
}
// Stop if receive active exit signal,the application will exit
func Stop() {
engine.Stop()
}
// LoadConf get key from configInterface,obj must be a pointer
func LoadConf(key string, obj interface{}) error {
return engine.LoadConf(key, obj)
}
// IsSet check configInterface is set key
func IsSet(key string) bool {
return engine.IsSet(key)
}
// New create an application for msa engine
func New(opts ...Option) *Engine {
e := &Engine{
gracefulWait: 5 * time.Second,
signal: make(chan os.Signal, 1),
interruptSignals: InterruptSignals,
stopCh: make(chan struct{}, 1),
injector: defaultInjector(),
}
for _, o := range opts {
o(e)
}
// if opts has no ConfigInterface will use it
if e.configInterface == nil {
e.configInterface = defaultConfig()
}
// if the configuration file directory and file, regenerate a config interface.
e.resetConfInterface()
return e
}
// Start run app
func (e *Engine) Start() {
// load all provides
e.loadProvides()
// invoke inject objects
e.invokeInjects()
// run init and start action
e.run()
// wait exit signal
e.waitExitSignal()
}
// Stop if receive active exit signal,the application will exit
func (e *Engine) Stop() {
close(e.stopCh)
e.shutdown()
}
// LoadConf get key from configInterface,obj must be a pointer
func (e *Engine) LoadConf(key string, obj interface{}) error {
return e.configInterface.GetValue(key, obj)
}
// IsSet configInterface is set key
func (e *Engine) IsSet(key string) bool {
return e.configInterface.IsSet(key)
}
func (e *Engine) run() {
for _, val := range e.injectValues {
if initStream, ok := val.Value.(initializer); ok {
if err := initStream.Init(); err != nil {
panic("init error: " + err.Error())
}
}
}
for _, val := range e.injectValues {
if startStream, ok := val.Value.(starter); ok {
if err := startStream.Start(); err != nil {
panic("start error: " + err.Error())
}
}
}
log.Println("msa started successfully")
}
// loadProvides load providers and config inject providers
func (e *Engine) loadProvides() {
for _, p := range e.providers {
provides.Register(p)
}
if e.configProvider != nil {
// register all providers from configProvider
configProviders := e.configProvider.Provide(e.configInterface)
for _, p := range configProviders {
provides.Register(p)
}
}
if provideObjects := provides.ProvideObjects(); len(provideObjects) > 0 {
e.injectValues = append(e.injectValues, provideObjects...)
}
}
func (e *Engine) waitExitSignal() {
// We'll accept graceful shutdowns when quit via SIGINT (Ctrl+C)
// receive signal to exit main goroutine
// Block until we receive our signal.
signal.Notify(e.signal, e.interruptSignals...)
select {
case sig := <-e.signal:
signal.Stop(e.signal)
log.Println("receive exit signal: ", sig.String())
e.shutdown()
case <-e.stopCh:
log.Println("receive stop signal")
}
}
func (e *Engine) invokeInjects() {
// init inject objects
if err := e.injector.Provide(e.injectValues...); err != nil {
panic("provide inject objects error: " + err.Error())
}
// invoke objects
if err := e.injector.Invoke(e.invokeFunc...); err != nil {
panic("inject invoke error: " + err.Error())
}
}
// shutdown graceful stop application
func (e *Engine) shutdown() {
defer log.Println("msa exit successfully")
for _, val := range e.injectValues {
if s, ok := val.Value.(stoppable); ok {
s.Stop()
}
}
ctx, cancel := context.WithTimeout(context.Background(), e.gracefulWait)
defer cancel()
<-ctx.Done()
}
func (e *Engine) resetConfInterface() {
var confOptions []config.Option
if e.configDir != "" {
confOptions = append(confOptions, config.WithConfigDir(e.configDir))
}
if e.configFile != "" {
confOptions = append(confOptions, config.WithConfigFile(e.configFile))
}
if len(confOptions) > 0 {
e.configInterface = config.New(confOptions...)
}
}
func defaultInjector() gdi.Injector {
return factory.CreateDI(factory.FbInject)
}
func defaultConfig() config.ConfigInterface {
return config.New()
}