@@ -9,7 +9,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type storeOptionsKey struct{}
|
type storeOptionsKey struct{}
|
||||||
type expiryKey struct{}
|
type defaultTTLKey struct{}
|
||||||
|
|
||||||
// StoreOptions sets the options for the underlying store
|
// StoreOptions sets the options for the underlying store
|
||||||
func StoreOptions(opts []store.Option) registry.Option {
|
func StoreOptions(opts []store.Option) registry.Option {
|
||||||
@@ -21,12 +21,12 @@ func StoreOptions(opts []store.Option) registry.Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServiceExpiry allows setting an expiry time for service registrations
|
// DefaultTTL allows setting a default register TTL for services
|
||||||
func ServiceExpiry(t time.Duration) registry.Option {
|
func DefaultTTL(t time.Duration) registry.Option {
|
||||||
return func(o *registry.Options) {
|
return func(o *registry.Options) {
|
||||||
if o.Context == nil {
|
if o.Context == nil {
|
||||||
o.Context = context.Background()
|
o.Context = context.Background()
|
||||||
}
|
}
|
||||||
o.Context = context.WithValue(o.Context, expiryKey{}, t)
|
o.Context = context.WithValue(o.Context, defaultTTLKey{}, t)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,22 +40,22 @@ func NewRegistry(opts ...registry.Option) registry.Registry {
|
|||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
o(&options)
|
o(&options)
|
||||||
}
|
}
|
||||||
exp, _ := options.Context.Value(expiryKey{}).(time.Duration)
|
defaultTTL, _ := options.Context.Value(defaultTTLKey{}).(time.Duration)
|
||||||
n := &storeregistry{
|
n := &storeregistry{
|
||||||
opts: options,
|
opts: options,
|
||||||
typ: _registryName,
|
typ: _registryName,
|
||||||
expiry: exp,
|
defaultTTL: defaultTTL,
|
||||||
}
|
}
|
||||||
n.store = natsjskv.NewStore(n.storeOptions(options)...)
|
n.store = natsjskv.NewStore(n.storeOptions(options)...)
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
type storeregistry struct {
|
type storeregistry struct {
|
||||||
opts registry.Options
|
opts registry.Options
|
||||||
store store.Store
|
store store.Store
|
||||||
typ string
|
typ string
|
||||||
expiry time.Duration
|
defaultTTL time.Duration
|
||||||
lock sync.RWMutex
|
lock sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init inits the registry
|
// Init inits the registry
|
||||||
@@ -76,7 +76,7 @@ func (n *storeregistry) Options() registry.Options {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Register adds a service to the registry
|
// Register adds a service to the registry
|
||||||
func (n *storeregistry) Register(s *registry.Service, _ ...registry.RegisterOption) error {
|
func (n *storeregistry) Register(s *registry.Service, opts ...registry.RegisterOption) error {
|
||||||
n.lock.RLock()
|
n.lock.RLock()
|
||||||
defer n.lock.RUnlock()
|
defer n.lock.RUnlock()
|
||||||
|
|
||||||
@@ -84,6 +84,12 @@ func (n *storeregistry) Register(s *registry.Service, _ ...registry.RegisterOpti
|
|||||||
return errors.New("wont store nil service")
|
return errors.New("wont store nil service")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var options registry.RegisterOptions
|
||||||
|
options.TTL = n.defaultTTL
|
||||||
|
for _, o := range opts {
|
||||||
|
o(&options)
|
||||||
|
}
|
||||||
|
|
||||||
unique := uuid.New().String()
|
unique := uuid.New().String()
|
||||||
if s.Metadata == nil {
|
if s.Metadata == nil {
|
||||||
s.Metadata = make(map[string]string)
|
s.Metadata = make(map[string]string)
|
||||||
@@ -97,7 +103,7 @@ func (n *storeregistry) Register(s *registry.Service, _ ...registry.RegisterOpti
|
|||||||
return n.store.Write(&store.Record{
|
return n.store.Write(&store.Record{
|
||||||
Key: s.Name + _serviceDelimiter + unique,
|
Key: s.Name + _serviceDelimiter + unique,
|
||||||
Value: b,
|
Value: b,
|
||||||
Expiry: n.expiry,
|
Expiry: options.TTL,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -180,8 +186,8 @@ func (n *storeregistry) storeOptions(opts registry.Options) []store.Option {
|
|||||||
natsjskv.EncodeKeys(),
|
natsjskv.EncodeKeys(),
|
||||||
}
|
}
|
||||||
|
|
||||||
if ttl, ok := opts.Context.Value(expiryKey{}).(time.Duration); ok {
|
if defaultTTL, ok := opts.Context.Value(defaultTTLKey{}).(time.Duration); ok {
|
||||||
storeoptions = append(storeoptions, natsjskv.DefaultTTL(ttl))
|
storeoptions = append(storeoptions, natsjskv.DefaultTTL(defaultTTL))
|
||||||
}
|
}
|
||||||
|
|
||||||
addr := []string{"127.0.0.1:9233"}
|
addr := []string{"127.0.0.1:9233"}
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ type Config struct {
|
|||||||
Username string `mapstructure:"username"`
|
Username string `mapstructure:"username"`
|
||||||
Password string `mapstructure:"password"`
|
Password string `mapstructure:"password"`
|
||||||
DisableCache bool `mapstructure:"disable_cache"`
|
DisableCache bool `mapstructure:"disable_cache"`
|
||||||
TTL time.Duration `mapstructure:"ttl"`
|
RegisterTTL time.Duration `mapstructure:"register_ttl"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Option allows configuring the registry
|
// Option allows configuring the registry
|
||||||
@@ -63,7 +63,7 @@ func GetRegistry(opts ...Option) mRegistry.Registry {
|
|||||||
case "natsjs", "nats-js", "nats-js-kv": // for backwards compatibility - we will stick with one of those
|
case "natsjs", "nats-js", "nats-js-kv": // for backwards compatibility - we will stick with one of those
|
||||||
_reg = natsjsregistry.NewRegistry(
|
_reg = natsjsregistry.NewRegistry(
|
||||||
mRegistry.Addrs(cfg.Addresses...),
|
mRegistry.Addrs(cfg.Addresses...),
|
||||||
natsjsregistry.ServiceExpiry(cfg.TTL),
|
natsjsregistry.DefaultTTL(cfg.RegisterTTL),
|
||||||
)
|
)
|
||||||
case "memory":
|
case "memory":
|
||||||
_reg = memr.NewRegistry()
|
_reg = memr.NewRegistry()
|
||||||
@@ -121,7 +121,7 @@ func getEnvs(opts ...Option) *Config {
|
|||||||
cfg.Addresses = s
|
cfg.Addresses = s
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg.TTL = GetRegisterTTL()
|
cfg.RegisterTTL = GetRegisterTTL()
|
||||||
|
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
o(cfg)
|
o(cfg)
|
||||||
|
|||||||
Reference in New Issue
Block a user