forked from uber-go/fx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
357 lines (303 loc) · 10.3 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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package fx
import (
"context"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"time"
"go.uber.org/dig"
"go.uber.org/fx/internal/fxlog"
"go.uber.org/fx/internal/fxreflect"
"go.uber.org/fx/internal/lifecycle"
"go.uber.org/multierr"
)
const defaultTimeout = 15 * time.Second
// An Option configures an App.
type Option interface {
apply(*App)
}
type optionFunc func(*App)
func (f optionFunc) apply(app *App) { f(app) }
// Provide registers constructors with the application's dependency injection
// container. Constructors provide one or more types, can depend on other
// types available in the container, and may optionally return an error. For
// example:
//
// // Provides type *C, depends on *A and *B.
// func(*A, *B) *C
//
// // Provides type *C, depends on *A and *B, and indicates failure by
// // returning an error.
// func(*A, *B) (*C, error)
//
// // Provides type *B and *C, depends on *A, and can fail.
// func(*A) (*B, *C, error)
//
// The order in which constructors are provided doesn't matter. Constructors
// are called lazily and their results are cached for reuse.
//
// Taken together, these properties make it perfectly reasonable to Provide a
// large number of standard constructors even if only a fraction of them are
// used.
//
// See the documentation for go.uber.org/dig for further details.
func Provide(constructors ...interface{}) Option {
return provideOption(constructors)
}
type provideOption []interface{}
func (po provideOption) apply(app *App) {
app.provides = append(app.provides, po...)
}
func (po provideOption) String() string {
items := make([]string, len(po))
for i, c := range po {
items[i] = fxreflect.FuncName(c)
}
return fmt.Sprintf("fx.Provide(%s)", strings.Join(items, ", "))
}
// Invoke registers functions that are executed eagerly on application start.
// Arguments for these functions are provided from the application's
// dependency injection container.
//
// Unlike constructors, invoked functions are always executed, and they're
// always run in order. Invoked functions may have any number of returned
// values. If the final returned object is an error, it's assumed to be a
// success indicator. All other returned values are discarded.
//
// See the documentation for go.uber.org/dig for further details.
func Invoke(funcs ...interface{}) Option {
return invokeOption(funcs)
}
type invokeOption []interface{}
func (io invokeOption) apply(app *App) {
app.invokes = append(app.invokes, io...)
}
func (io invokeOption) String() string {
items := make([]string, len(io))
for i, f := range io {
items[i] = fxreflect.FuncName(f)
}
return fmt.Sprintf("fx.Invoke(%s)", strings.Join(items, ", "))
}
// Options composes a collection of Options into a single Option.
func Options(opts ...Option) Option {
return optionGroup(opts)
}
type optionGroup []Option
func (og optionGroup) apply(app *App) {
for _, opt := range og {
opt.apply(app)
}
}
func (og optionGroup) String() string {
items := make([]string, len(og))
for i, opt := range og {
items[i] = fmt.Sprint(opt)
}
return fmt.Sprintf("fx.Options(%s)", strings.Join(items, ", "))
}
// Printer is the interface required by fx's logging backend. It's implemented
// by most loggers, including the standard library's.
type Printer interface {
Printf(string, ...interface{})
}
// Logger redirects the application's log output to the provided printer.
func Logger(p Printer) Option {
return optionFunc(func(app *App) {
app.logger = &fxlog.Logger{Printer: p}
app.lifecycle = &lifecycleWrapper{lifecycle.New(app.logger)}
})
}
// NopLogger disables the application's log output.
var NopLogger = Logger(nopLogger{})
type nopLogger struct{}
func (l nopLogger) Printf(string, ...interface{}) {
return
}
// An App is a modular application built around dependency injection.
type App struct {
err error
container *dig.Container
lifecycle *lifecycleWrapper
provides []interface{}
invokes []interface{}
logger *fxlog.Logger
}
// New creates and initializes an App. All applications begin with the
// Lifecycle type available in their dependency injection container.
//
// It then executes all functions supplied via the Invoke option. Supplying
// arguments to these functions requires calling some of the constructors
// supplied by the Provide option. If any invoked function fails, an error is
// returned immediately.
func New(opts ...Option) *App {
logger := fxlog.New()
lc := &lifecycleWrapper{lifecycle.New(logger)}
app := &App{
container: dig.New(),
lifecycle: lc,
logger: logger,
}
for _, opt := range opts {
opt.apply(app)
}
for _, p := range app.provides {
app.provide(p)
}
app.provide(func() Lifecycle { return app.lifecycle })
if app.err != nil {
app.logger.Printf("Error after options were applied: %v", app.err)
return app
}
if err := app.executeInvokes(); err != nil {
app.err = err
}
return app
}
// Err returns an error that may have been encountered during the
// graph resolution.
//
// This includes things like incomplete graphs, circular dependencies,
// missing dependencies, invalid constructors, and invoke errors.
func (app *App) Err() error {
return app.err
}
// Execute invokes in order supplied to New.
//
// It might be worthwhile to consider adding context.Context to this function
// so we can handle the infinite-invokes.
//
// Returns the first error encountered.
func (app *App) executeInvokes() error {
var err error
for _, fn := range app.invokes {
fname := fxreflect.FuncName(fn)
app.logger.Printf("INVOKE\t\t%s", fname)
if _, ok := fn.(Option); ok {
err = fmt.Errorf("fx.Option should be passed to fx.New directly, not to fx.Invoke: fx.Invoke received %v", fn)
} else {
err = app.container.Invoke(fn)
}
if err != nil {
app.logger.Printf("Error during %q invoke: %v", fname, err)
break
}
}
return err
}
// Run starts the application, blocks on the signals channel, and then
// gracefully shuts the application down. It uses DefaultTimeout for the start
// and stop timeouts.
//
// See Start and Stop for application lifecycle details.
func (app *App) Run() {
startCtx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
if err := app.Start(startCtx); err != nil {
app.logger.Fatalf("ERROR\t\tFailed to start: %v", err)
}
app.logger.PrintSignal(<-app.Done())
stopCtx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
if err := app.Stop(stopCtx); err != nil {
app.logger.Fatalf("ERROR\t\tFailed to stop cleanly: %v", err)
}
}
// Start executes all the OnStart hooks of the resolved object graph
// in the instantiation order.
//
// This typically starts all the long-running goroutines, like network
// servers or message queue consumers.
//
// First, Start checks whether any errors were encountered while applying
// Options. If so, it returns immediately.
//
// By taking a dependency on the Lifecycle type, some of the executed
// constructors may register start and stop hooks. After executing all Invoke
// functions, Start executes all OnStart hooks registered with the
// application's Lifecycle, starting with the root of the dependency graph.
// This ensures that each constructor's start hooks aren't executed until all
// its dependencies' start hooks complete. If any of the start hooks return an
// error, start short-circuits.
func (app *App) Start(ctx context.Context) error {
return withTimeout(ctx, app.start)
}
// Stop gracefully stops the application. It executes any registered OnStop
// hooks in reverse order (from the leaves of the dependency tree to the
// roots), so that types are stopped before their dependencies.
//
// If the application didn't start cleanly, only hooks whose OnStart phase was
// called are executed. However, all those hooks are always executed, even if
// some fail.
func (app *App) Stop(ctx context.Context) error {
return withTimeout(ctx, app.lifecycle.Stop)
}
// Done returns a channel of signals to block on after starting the
// application.
func (app *App) Done() <-chan os.Signal {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
return c
}
func (app *App) provide(constructor interface{}) {
if app.err != nil {
return
}
app.logger.PrintProvide(constructor)
if _, ok := constructor.(Option); ok {
app.err = fmt.Errorf("fx.Option should be passed to fx.New directly, not to fx.Provide: fx.Provide received %v", constructor)
return
}
if err := app.container.Provide(constructor); err != nil {
app.err = err
}
}
func (app *App) start(ctx context.Context) error {
if app.err != nil {
// Some provides failed, short-circuit immediately.
return app.err
}
// Attempt to start cleanly.
if err := app.lifecycle.Start(ctx); err != nil {
// Start failed, roll back.
app.logger.Printf("ERROR\t\tStart failed, rolling back: %v", err)
if stopErr := app.lifecycle.Stop(ctx); stopErr != nil {
app.logger.Printf("ERROR\t\tCouldn't rollback cleanly: %v", stopErr)
return multierr.Append(err, stopErr)
}
return err
}
app.logger.Printf("RUNNING")
return nil
}
func withTimeout(ctx context.Context, f func(context.Context) error) error {
c := make(chan error, 1)
go func() { c <- f(ctx) }()
select {
case <-ctx.Done():
return ctx.Err()
case err := <-c:
return err
}
}