1
0
mirror of https://github.com/go-kratos/kratos.git synced 2025-01-14 02:33:03 +02:00
kratos/app.go

133 lines
2.7 KiB
Go
Raw Normal View History

2021-02-17 11:14:47 +02:00
package kratos
import (
"context"
"errors"
"os"
"os/signal"
2021-05-28 09:08:25 +02:00
"sync"
2021-02-17 11:14:47 +02:00
"syscall"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/registry"
2021-05-28 09:08:25 +02:00
"github.com/go-kratos/kratos/v2/transport"
2021-02-17 11:14:47 +02:00
"github.com/google/uuid"
"golang.org/x/sync/errgroup"
)
// App is an application components lifecycle manager
type App struct {
opts options
ctx context.Context
cancel func()
instance *registry.ServiceInstance
log *log.Helper
}
// New create an application lifecycle manager.
func New(opts ...Option) *App {
options := options{
ctx: context.Background(),
logger: log.DefaultLogger,
sigs: []os.Signal{syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGINT},
}
if id, err := uuid.NewUUID(); err == nil {
options.id = id.String()
}
for _, o := range opts {
o(&options)
}
ctx, cancel := context.WithCancel(options.ctx)
return &App{
2021-05-28 09:08:25 +02:00
ctx: ctx,
cancel: cancel,
opts: options,
log: log.NewHelper(options.logger),
2021-02-17 11:14:47 +02:00
}
}
// Run executes all OnStart hooks registered with the application's Lifecycle.
func (a *App) Run() error {
instance, err := a.buildInstance()
2021-05-28 09:08:25 +02:00
if err != nil {
return err
}
2021-06-19 09:40:28 +02:00
eg, ctx := errgroup.WithContext(a.ctx)
2021-05-28 09:08:25 +02:00
wg := sync.WaitGroup{}
2021-02-17 11:14:47 +02:00
for _, srv := range a.opts.servers {
srv := srv
2021-05-28 09:08:25 +02:00
eg.Go(func() error {
2021-02-17 11:14:47 +02:00
<-ctx.Done() // wait for stop signal
return srv.Stop(ctx)
2021-02-17 11:14:47 +02:00
})
2021-05-28 09:08:25 +02:00
wg.Add(1)
eg.Go(func() error {
wg.Done()
return srv.Start(ctx)
2021-02-17 11:14:47 +02:00
})
}
2021-05-28 09:08:25 +02:00
wg.Wait()
2021-02-28 14:40:05 +02:00
if a.opts.registrar != nil {
2021-05-28 09:08:25 +02:00
if err := a.opts.registrar.Register(a.opts.ctx, instance); err != nil {
2021-02-17 11:14:47 +02:00
return err
}
2021-05-28 09:08:25 +02:00
a.instance = instance
2021-02-17 11:14:47 +02:00
}
c := make(chan os.Signal, 1)
signal.Notify(c, a.opts.sigs...)
2021-05-28 09:08:25 +02:00
eg.Go(func() error {
2021-02-17 11:14:47 +02:00
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-c:
a.Stop()
}
}
})
2021-05-28 09:08:25 +02:00
if err := eg.Wait(); err != nil && !errors.Is(err, context.Canceled) {
2021-02-17 11:14:47 +02:00
return err
}
return nil
}
// Stop gracefully stops the application.
func (a *App) Stop() error {
2021-05-28 09:08:25 +02:00
if a.opts.registrar != nil && a.instance != nil {
2021-02-28 14:40:05 +02:00
if err := a.opts.registrar.Deregister(a.opts.ctx, a.instance); err != nil {
2021-02-17 11:14:47 +02:00
return err
}
}
if a.cancel != nil {
a.cancel()
}
return nil
}
func (a *App) buildInstance() (*registry.ServiceInstance, error) {
var endpoints []string
for _, e := range a.opts.endpoints {
endpoints = append(endpoints, e.String())
}
if len(endpoints) == 0 {
for _, srv := range a.opts.servers {
2021-05-28 09:30:55 +02:00
if r, ok := srv.(transport.Endpointer); ok {
2021-05-28 09:08:25 +02:00
e, err := r.Endpoint()
if err != nil {
return nil, err
}
endpoints = append(endpoints, e.String())
2021-02-17 11:14:47 +02:00
}
}
}
2021-02-17 11:14:47 +02:00
return &registry.ServiceInstance{
ID: a.opts.id,
Name: a.opts.name,
Version: a.opts.version,
Metadata: a.opts.metadata,
Endpoints: endpoints,
2021-05-28 09:08:25 +02:00
}, nil
2021-02-17 11:14:47 +02:00
}