You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kratos/app.go

211 lines
4.9 KiB

4 years ago
package kratos
import (
"context"
"errors"
"os"
"os/signal"
"sync"
4 years ago
"syscall"
"time"
4 years ago
"github.com/go-kratos/kratos/v2/health"
4 years ago
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/registry"
"github.com/go-kratos/kratos/v2/transport"
4 years ago
"github.com/google/uuid"
"golang.org/x/sync/errgroup"
)
// AppInfo is application context value.
type AppInfo interface {
ID() string
Name() string
Version() string
Metadata() map[string]string
Endpoint() []string
Health() *health.Health
}
// App is an application components lifecycle manager.
4 years ago
type App struct {
opts options
ctx context.Context
cancel func()
lk sync.Mutex
4 years ago
instance *registry.ServiceInstance
health *health.Health
4 years ago
}
// New create an application lifecycle manager.
func New(opts ...Option) *App {
o := options{
ctx: context.Background(),
logger: log.NewHelper(log.GetLogger()),
sigs: []os.Signal{syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGINT},
registrarTimeout: 10 * time.Second,
stopTimeout: 10 * time.Second,
4 years ago
}
if id, err := uuid.NewUUID(); err == nil {
o.id = id.String()
4 years ago
}
for _, opt := range opts {
opt(&o)
4 years ago
}
ctx, cancel := context.WithCancel(o.ctx)
4 years ago
return &App{
ctx: ctx,
cancel: cancel,
opts: o,
health: health.New(),
4 years ago
}
}
// ID returns app instance id.
func (a *App) ID() string { return a.opts.id }
// Name returns service name.
func (a *App) Name() string { return a.opts.name }
// Version returns app version.
func (a *App) Version() string { return a.opts.version }
// Metadata returns service metadata.
func (a *App) Metadata() map[string]string { return a.opts.metadata }
// Endpoint returns endpoints.
func (a *App) Endpoint() []string {
if a.instance == nil {
return []string{}
}
return a.instance.Endpoints
}
// Health returns health
func (a *App) Health() *health.Health { return a.health }
4 years ago
// Run executes all OnStart hooks registered with the application's Lifecycle.
func (a *App) Run() error {
instance, err := a.buildInstance()
if err != nil {
return err
}
ctx := NewContext(a.ctx, a)
eg, ctx := errgroup.WithContext(ctx)
wg := sync.WaitGroup{}
4 years ago
for _, srv := range a.opts.servers {
srv := srv
eg.Go(func() error {
4 years ago
<-ctx.Done() // wait for stop signal
sctx, cancel := context.WithTimeout(NewContext(context.Background(), a), a.opts.stopTimeout)
defer cancel()
return srv.Stop(sctx)
4 years ago
})
wg.Add(1)
eg.Go(func() error {
wg.Done()
return srv.Start(ctx)
4 years ago
})
}
wg.Wait()
err = a.health.SetStatus(a.Name(), health.Status_SERVING)
if err != nil {
log.Errorf("set health status error: %v", err)
}
4 years ago
if a.opts.registrar != nil {
rctx, rcancel := context.WithTimeout(a.opts.ctx, a.opts.registrarTimeout)
defer rcancel()
3 years ago
if err = a.opts.registrar.Register(rctx, instance); err != nil {
4 years ago
return err
}
a.lk.Lock()
a.instance = instance
a.lk.Unlock()
4 years ago
}
c := make(chan os.Signal, 1)
signal.Notify(c, a.opts.sigs...)
eg.Go(func() error {
4 years ago
for {
select {
case <-ctx.Done():
err = a.health.SetStatus(a.Name(), health.Status_NOT_SERVING)
if err != nil {
log.Errorf("set health status error: %v", err)
}
4 years ago
return ctx.Err()
case <-c:
err := a.Stop()
if err != nil {
3 years ago
a.opts.logger.Errorf("failed to stop app: %v", err)
return err
}
4 years ago
}
}
})
if err := eg.Wait(); err != nil && !errors.Is(err, context.Canceled) {
4 years ago
return err
}
return nil
}
// Stop gracefully stops the application.
func (a *App) Stop() error {
err := a.health.SetStatus(a.Name(), health.Status_NOT_SERVING)
if err != nil {
log.Errorf("set health status error: %v", err)
}
a.lk.Lock()
instance := a.instance
a.lk.Unlock()
if a.opts.registrar != nil && instance != nil {
ctx, cancel := context.WithTimeout(a.opts.ctx, a.opts.registrarTimeout)
defer cancel()
if err := a.opts.registrar.Deregister(ctx, instance); err != nil {
4 years ago
return err
}
}
if a.cancel != nil {
a.cancel()
}
return nil
}
func (a *App) buildInstance() (*registry.ServiceInstance, error) {
endpoints := make([]string, 0) //nolint:gomnd
for _, e := range a.opts.endpoints {
endpoints = append(endpoints, e.String())
}
if len(endpoints) == 0 {
for _, srv := range a.opts.servers {
if r, ok := srv.(transport.Endpointer); ok {
e, err := r.Endpoint()
if err != nil {
return nil, err
}
endpoints = append(endpoints, e.String())
4 years ago
}
}
}
return &registry.ServiceInstance{
ID: a.opts.id,
Name: a.opts.name,
Version: a.opts.version,
Metadata: a.opts.metadata,
Endpoints: endpoints,
}, nil
4 years ago
}
type appKey struct{}
// NewContext returns a new Context that carries value.
func NewContext(ctx context.Context, s AppInfo) context.Context {
return context.WithValue(ctx, appKey{}, s)
}
// FromContext returns the Transport value stored in ctx, if any.
func FromContext(ctx context.Context) (s AppInfo, ok bool) {
s, ok = ctx.Value(appKey{}).(AppInfo)
return
}