Merge pull request #1021 from unistack-org/registry_check
add RegisterCheck web server option for internal health checks
This commit is contained in:
commit
0489ae91e9
@ -25,6 +25,9 @@ type Options struct {
|
||||
RegisterTTL time.Duration
|
||||
RegisterInterval time.Duration
|
||||
|
||||
// RegisterCheck runs a check function before registering the service
|
||||
RegisterCheck func(context.Context) error
|
||||
|
||||
Server *http.Server
|
||||
Handler http.Handler
|
||||
|
||||
@ -62,6 +65,10 @@ func newOptions(opts ...Option) Options {
|
||||
o(&opt)
|
||||
}
|
||||
|
||||
if opt.RegisterCheck == nil {
|
||||
opt.RegisterCheck = DefaultRegisterCheck
|
||||
}
|
||||
|
||||
return opt
|
||||
}
|
||||
|
||||
@ -228,3 +235,10 @@ func StaticDir(d string) Option {
|
||||
o.StaticDir = d
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterCheck run func before registry service
|
||||
func RegisterCheck(fn func(context.Context) error) Option {
|
||||
return func(o *Options) {
|
||||
o.RegisterCheck = fn
|
||||
}
|
||||
}
|
||||
|
@ -123,6 +123,13 @@ func (s *service) register() error {
|
||||
srv := s.genSrv()
|
||||
srv.Endpoints = s.srv.Endpoints
|
||||
s.srv = srv
|
||||
|
||||
// use RegisterCheck func before register
|
||||
if err := s.opts.RegisterCheck(s.opts.Context); err != nil {
|
||||
log.Logf("Server %s-%s register check error: %s", s.opts.Name, s.opts.Id, err)
|
||||
return err
|
||||
}
|
||||
|
||||
return r.Register(s.srv, registry.RegisterTTL(s.opts.RegisterTTL))
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
@ -32,7 +33,8 @@ var (
|
||||
DefaultRegisterInterval = time.Second * 30
|
||||
|
||||
// static directory
|
||||
DefaultStaticDir = "html"
|
||||
DefaultStaticDir = "html"
|
||||
DefaultRegisterCheck = func(context.Context) error { return nil }
|
||||
)
|
||||
|
||||
// NewService returns a new web.Service
|
||||
|
Loading…
Reference in New Issue
Block a user