mirror of
https://github.com/go-kratos/kratos.git
synced 2025-03-31 22:05:08 +02:00
app: fix instance nil when not registered (#2059)
* fix instance nil when not registered * fix data race
This commit is contained in:
parent
84b23fd301
commit
503ec03f37
23
app.go
23
app.go
@ -31,7 +31,7 @@ type App struct {
|
|||||||
opts options
|
opts options
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
cancel func()
|
cancel func()
|
||||||
lk sync.Mutex
|
mu sync.Mutex
|
||||||
instance *registry.ServiceInstance
|
instance *registry.ServiceInstance
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,7 +39,6 @@ type App struct {
|
|||||||
func New(opts ...Option) *App {
|
func New(opts ...Option) *App {
|
||||||
o := options{
|
o := options{
|
||||||
ctx: context.Background(),
|
ctx: context.Background(),
|
||||||
logger: log.NewHelper(log.GetLogger()),
|
|
||||||
sigs: []os.Signal{syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGINT},
|
sigs: []os.Signal{syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGINT},
|
||||||
registrarTimeout: 10 * time.Second,
|
registrarTimeout: 10 * time.Second,
|
||||||
stopTimeout: 10 * time.Second,
|
stopTimeout: 10 * time.Second,
|
||||||
@ -50,6 +49,9 @@ func New(opts ...Option) *App {
|
|||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
opt(&o)
|
opt(&o)
|
||||||
}
|
}
|
||||||
|
if o.logger != nil {
|
||||||
|
log.SetLogger(o.logger)
|
||||||
|
}
|
||||||
ctx, cancel := context.WithCancel(o.ctx)
|
ctx, cancel := context.WithCancel(o.ctx)
|
||||||
return &App{
|
return &App{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
@ -72,10 +74,10 @@ func (a *App) Metadata() map[string]string { return a.opts.metadata }
|
|||||||
|
|
||||||
// Endpoint returns endpoints.
|
// Endpoint returns endpoints.
|
||||||
func (a *App) Endpoint() []string {
|
func (a *App) Endpoint() []string {
|
||||||
if a.instance == nil {
|
if a.instance != nil {
|
||||||
return []string{}
|
|
||||||
}
|
|
||||||
return a.instance.Endpoints
|
return a.instance.Endpoints
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run executes all OnStart hooks registered with the application's Lifecycle.
|
// Run executes all OnStart hooks registered with the application's Lifecycle.
|
||||||
@ -84,6 +86,9 @@ func (a *App) Run() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
a.mu.Lock()
|
||||||
|
a.instance = instance
|
||||||
|
a.mu.Unlock()
|
||||||
eg, ctx := errgroup.WithContext(NewContext(a.ctx, a))
|
eg, ctx := errgroup.WithContext(NewContext(a.ctx, a))
|
||||||
wg := sync.WaitGroup{}
|
wg := sync.WaitGroup{}
|
||||||
for _, srv := range a.opts.servers {
|
for _, srv := range a.opts.servers {
|
||||||
@ -107,9 +112,6 @@ func (a *App) Run() error {
|
|||||||
if err := a.opts.registrar.Register(rctx, instance); err != nil {
|
if err := a.opts.registrar.Register(rctx, instance); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
a.lk.Lock()
|
|
||||||
a.instance = instance
|
|
||||||
a.lk.Unlock()
|
|
||||||
}
|
}
|
||||||
c := make(chan os.Signal, 1)
|
c := make(chan os.Signal, 1)
|
||||||
signal.Notify(c, a.opts.sigs...)
|
signal.Notify(c, a.opts.sigs...)
|
||||||
@ -120,7 +122,6 @@ func (a *App) Run() error {
|
|||||||
return ctx.Err()
|
return ctx.Err()
|
||||||
case <-c:
|
case <-c:
|
||||||
if err := a.Stop(); err != nil {
|
if err := a.Stop(); err != nil {
|
||||||
a.opts.logger.Errorf("failed to stop app: %v", err)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -134,9 +135,9 @@ func (a *App) Run() error {
|
|||||||
|
|
||||||
// Stop gracefully stops the application.
|
// Stop gracefully stops the application.
|
||||||
func (a *App) Stop() error {
|
func (a *App) Stop() error {
|
||||||
a.lk.Lock()
|
a.mu.Lock()
|
||||||
instance := a.instance
|
instance := a.instance
|
||||||
a.lk.Unlock()
|
a.mu.Unlock()
|
||||||
if a.opts.registrar != nil && instance != nil {
|
if a.opts.registrar != nil && instance != nil {
|
||||||
ctx, cancel := context.WithTimeout(NewContext(a.ctx, a), a.opts.registrarTimeout)
|
ctx, cancel := context.WithTimeout(NewContext(a.ctx, a), a.opts.registrarTimeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
@ -154,7 +154,7 @@ func TestApp_Endpoint(t *testing.T) {
|
|||||||
endpoint []string
|
endpoint []string
|
||||||
metadata map[string]string
|
metadata map[string]string
|
||||||
}{
|
}{
|
||||||
id: "3", version: "v3", name: "kratos-v3", endpoint: []string{},
|
id: "3", version: "v3", name: "kratos-v3", endpoint: nil,
|
||||||
metadata: map[string]string{},
|
metadata: map[string]string{},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -25,7 +25,7 @@ type options struct {
|
|||||||
ctx context.Context
|
ctx context.Context
|
||||||
sigs []os.Signal
|
sigs []os.Signal
|
||||||
|
|
||||||
logger *log.Helper
|
logger log.Logger
|
||||||
registrar registry.Registrar
|
registrar registry.Registrar
|
||||||
registrarTimeout time.Duration
|
registrarTimeout time.Duration
|
||||||
stopTimeout time.Duration
|
stopTimeout time.Duration
|
||||||
@ -64,9 +64,7 @@ func Context(ctx context.Context) Option {
|
|||||||
|
|
||||||
// Logger with service logger.
|
// Logger with service logger.
|
||||||
func Logger(logger log.Logger) Option {
|
func Logger(logger log.Logger) Option {
|
||||||
return func(o *options) {
|
return func(o *options) { o.logger = logger }
|
||||||
o.logger = log.NewHelper(logger)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Server with transport servers.
|
// Server with transport servers.
|
||||||
|
@ -79,7 +79,7 @@ func TestLogger(t *testing.T) {
|
|||||||
o := &options{}
|
o := &options{}
|
||||||
v := xlog.NewStdLogger(log.Writer())
|
v := xlog.NewStdLogger(log.Writer())
|
||||||
Logger(v)(o)
|
Logger(v)(o)
|
||||||
if !reflect.DeepEqual(xlog.NewHelper(v), o.logger) {
|
if !reflect.DeepEqual(v, o.logger) {
|
||||||
t.Fatalf("o.logger:%v is not equal to xlog.NewHelper(v):%v", o.logger, xlog.NewHelper(v))
|
t.Fatalf("o.logger:%v is not equal to xlog.NewHelper(v):%v", o.logger, xlog.NewHelper(v))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user