-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnaxt.go
134 lines (117 loc) · 3.23 KB
/
naxt.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
package next
import (
"context"
"fmt"
"os"
"syscall"
"time"
prom "github.com/go-kratos/kratos/contrib/metrics/prometheus/v2"
"github.com/nextmicro/next/config"
"github.com/nextmicro/next/pkg/env"
metric "github.com/nextmicro/next/pkg/metrics"
"github.com/nextmicro/next/registry"
"github.com/go-kratos/kratos/v2"
_ "github.com/nextmicro/next/middleware/bbr"
_ "github.com/nextmicro/next/middleware/circuitbreaker"
_ "github.com/nextmicro/next/middleware/logging"
_ "github.com/nextmicro/next/middleware/metadata"
_ "github.com/nextmicro/next/middleware/metrics"
_ "github.com/nextmicro/next/middleware/recovery"
_ "github.com/nextmicro/next/middleware/tracing"
"github.com/nextmicro/next/runtime"
)
type Next struct {
*kratos.App
opt Options
}
// New create an application lifecycle manager.
func New(opts ...Option) (*Next, error) {
opt := buildOptions(opts...)
// register runtime
run := runtime.NewRuntime()
if err := run.Init(
runtime.Loader(opt.Loader...),
); err != nil {
return nil, fmt.Errorf("runtime init error: %w", err)
}
// start runtime
if err := run.Start(opt.Ctx); err != nil {
return nil, fmt.Errorf("runtime start error: %w", err)
}
// register runtime stop
opt.AfterStop = append(opt.AfterStop, run.Stop)
kOpts := []kratos.Option{
kratos.ID(opt.ID),
kratos.Name(opt.Name),
kratos.Version(opt.Version),
kratos.Metadata(opt.Metadata),
kratos.Endpoint(opt.Endpoints...),
kratos.Context(opt.Ctx),
kratos.Server(opt.Servers...),
kratos.Signal(opt.Sigs...),
kratos.Registrar(opt.Registrar),
kratos.RegistrarTimeout(opt.RegistrarTimeout),
kratos.StopTimeout(opt.StopTimeout),
}
for _, beforeStart := range opt.BeforeStart {
kOpts = append(kOpts, kratos.BeforeStart(beforeStart))
}
for _, beforeStop := range opt.BeforeStop {
kOpts = append(kOpts, kratos.BeforeStop(beforeStop))
}
for _, afterStart := range opt.AfterStart {
kOpts = append(kOpts, kratos.AfterStart(afterStart))
}
for _, afterStop := range opt.AfterStop {
kOpts = append(kOpts, kratos.AfterStop(afterStop))
}
next := &Next{
App: kratos.New(kOpts...),
opt: opt,
}
// init metrics
next.initMetrics()
return next, nil
}
func (app *Next) initMetrics() {
// build app metrics.
prom.NewGauge(metric.BuildInfoGauge).With(
app.ID(),
app.Name(),
app.Version(),
env.DeployEnvironment(),
env.GoVersion(),
env.AppVersion(),
env.StartTime(),
env.BuildTime(),
).Set(float64(time.Now().UnixNano() / 1e6))
}
// buildOptions build options
func buildOptions(options ...Option) Options {
var opts = make([]Option, 0, len(options)+4)
opt := Options{
Ctx: context.Background(),
Sigs: []os.Signal{syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGKILL},
Registrar: registry.DefaultRegistry,
RegistrarTimeout: 10 * time.Second,
StopTimeout: 10 * time.Second,
}
c := config.ApplicationConfig()
if c.GetId() != "" {
opts = append(opts, ID(c.GetId()))
}
if c.GetName() != "" {
opts = append(opts, Name(c.GetName()))
}
if c.GetVersion() != "" {
opts = append(opts, Version(c.GetVersion()))
}
if c.GetMetadata() != nil {
opts = append(opts, Metadata(c.GetMetadata()))
}
opts = append(opts, options...)
for _, o := range opts {
o(&opt)
}
return opt
}