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
Transport transport.Transport
// Registration options
RegisterTTL time.Duration
// Register loop interval
RegisterInterval time.Duration
// Before and After funcs
@ -125,7 +124,7 @@ func Action(a func(*cli.Context)) Option {
func RegisterTTL(t time.Duration) Option {
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) {
o.TTL = t
}

View File

@ -26,6 +26,8 @@ type Options struct {
HdlrWrappers []HandlerWrapper
SubWrappers []SubscriberWrapper
RegisterTTL time.Duration
// Debug Handler which can be set by a user
DebugHandler debug.DebugHandler
@ -34,10 +36,6 @@ type Options struct {
Context context.Context
}
type RegisterOptions struct {
TTL time.Duration
}
func newOptions(opt ...Option) Options {
opts := Options{
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
func WrapHandler(w HandlerWrapper) Option {
return func(o *Options) {
@ -173,10 +178,3 @@ func WrapSubscriber(w SubscriberWrapper) Option {
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
}
func (s *rpcServer) Register(opts ...RegisterOption) error {
var options RegisterOptions
for _, o := range opts {
o(&options)
}
// create registry options
rOpts := []registry.RegisterOption{registry.WithTTL(options.TTL)}
func (s *rpcServer) Register() error {
// parse address for host, port
config := s.Options()
var advt, host string
@ -228,6 +220,9 @@ func (s *rpcServer) Register(opts ...RegisterOption) error {
}
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 {
return err
}

View File

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

View File

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