Prefer RegisterTTL set through Init

This commit is contained in:
Asim 2016-01-27 12:23:18 +00:00
parent 61094fefe8
commit 013d1de2c4
6 changed files with 19 additions and 30 deletions

View File

@ -22,8 +22,7 @@ type Options struct {
Registry registry.Registry Registry registry.Registry
Transport transport.Transport Transport transport.Transport
// Registration options // Register loop interval
RegisterTTL time.Duration
RegisterInterval time.Duration RegisterInterval time.Duration
// Before and After funcs // Before and After funcs
@ -125,7 +124,7 @@ func Action(a func(*cli.Context)) Option {
func RegisterTTL(t time.Duration) Option { func RegisterTTL(t time.Duration) Option {
return func(o *Options) { return func(o *Options) {
o.RegisterTTL = t o.Server.Init(server.RegisterTTL(t))
} }
} }

View File

@ -44,7 +44,7 @@ func TLSConfig(t *tls.Config) Option {
} }
} }
func WithTTL(t time.Duration) RegisterOption { func RegisterTTL(t time.Duration) RegisterOption {
return func(o *RegisterOptions) { return func(o *RegisterOptions) {
o.TTL = t o.TTL = t
} }

View File

@ -26,6 +26,8 @@ type Options struct {
HdlrWrappers []HandlerWrapper HdlrWrappers []HandlerWrapper
SubWrappers []SubscriberWrapper SubWrappers []SubscriberWrapper
RegisterTTL time.Duration
// Debug Handler which can be set by a user // Debug Handler which can be set by a user
DebugHandler debug.DebugHandler DebugHandler debug.DebugHandler
@ -34,10 +36,6 @@ type Options struct {
Context context.Context Context context.Context
} }
type RegisterOptions struct {
TTL time.Duration
}
func newOptions(opt ...Option) Options { func newOptions(opt ...Option) Options {
opts := Options{ opts := Options{
Codecs: make(map[string]codec.NewCodec), Codecs: make(map[string]codec.NewCodec),
@ -160,6 +158,13 @@ func Metadata(md map[string]string) Option {
} }
} }
// Register the service with a TTL
func RegisterTTL(t time.Duration) Option {
return func(o *Options) {
o.RegisterTTL = t
}
}
// Adds a handler Wrapper to a list of options passed into the server // Adds a handler Wrapper to a list of options passed into the server
func WrapHandler(w HandlerWrapper) Option { func WrapHandler(w HandlerWrapper) Option {
return func(o *Options) { return func(o *Options) {
@ -173,10 +178,3 @@ func WrapSubscriber(w SubscriberWrapper) Option {
o.SubWrappers = append(o.SubWrappers, w) o.SubWrappers = append(o.SubWrappers, w)
} }
} }
// Register the service with a TTL
func RegisterTTL(t time.Duration) RegisterOption {
return func(o *RegisterOptions) {
o.TTL = t
}
}

View File

@ -155,15 +155,7 @@ func (s *rpcServer) Subscribe(sb Subscriber) error {
return nil return nil
} }
func (s *rpcServer) Register(opts ...RegisterOption) error { func (s *rpcServer) Register() error {
var options RegisterOptions
for _, o := range opts {
o(&options)
}
// create registry options
rOpts := []registry.RegisterOption{registry.WithTTL(options.TTL)}
// parse address for host, port // parse address for host, port
config := s.Options() config := s.Options()
var advt, host string var advt, host string
@ -228,6 +220,9 @@ func (s *rpcServer) Register(opts ...RegisterOption) error {
} }
log.Infof("Registering node: %s", node.Id) log.Infof("Registering node: %s", node.Id)
// create registry options
rOpts := []registry.RegisterOption{registry.RegisterTTL(config.RegisterTTL)}
if err := config.Registry.Register(service, rOpts...); err != nil { if err := config.Registry.Register(service, rOpts...); err != nil {
return err return err
} }

View File

@ -45,7 +45,7 @@ type Server interface {
NewHandler(interface{}, ...HandlerOption) Handler NewHandler(interface{}, ...HandlerOption) Handler
NewSubscriber(string, interface{}, ...SubscriberOption) Subscriber NewSubscriber(string, interface{}, ...SubscriberOption) Subscriber
Subscribe(Subscriber) error Subscribe(Subscriber) error
Register(...RegisterOption) error Register() error
Deregister() error Deregister() error
Start() error Start() error
Stop() error Stop() error
@ -86,8 +86,6 @@ type HandlerOption func(*HandlerOptions)
type SubscriberOption func(*SubscriberOptions) type SubscriberOption func(*SubscriberOptions)
type RegisterOption func(*RegisterOptions)
var ( var (
DefaultAddress = ":0" DefaultAddress = ":0"
DefaultName = "go-server" DefaultName = "go-server"

View File

@ -41,8 +41,7 @@ func (s *service) run(exit chan bool) {
for { for {
select { select {
case <-t.C: case <-t.C:
if err := s.opts.Server.Register(server.RegisterTTL(s.opts.RegisterTTL)); err != nil { s.opts.Server.Register()
}
case <-exit: case <-exit:
t.Stop() t.Stop()
return return
@ -102,7 +101,7 @@ func (s *service) Start() error {
return err return err
} }
if err := s.opts.Server.Register(server.RegisterTTL(s.opts.RegisterTTL)); err != nil { if err := s.opts.Server.Register(); err != nil {
return err return err
} }