naming is hard

Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
This commit is contained in:
Jörn Friedrich Dreyer
2024-07-19 12:37:08 +02:00
parent 0c0866711c
commit 0c6ca19602
3 changed files with 26 additions and 20 deletions

View File

@@ -9,7 +9,7 @@ import (
)
type storeOptionsKey struct{}
type expiryKey struct{}
type defaultTTLKey struct{}
// StoreOptions sets the options for the underlying store
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
func ServiceExpiry(t time.Duration) registry.Option {
// DefaultTTL allows setting a default register TTL for services
func DefaultTTL(t time.Duration) registry.Option {
return func(o *registry.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, expiryKey{}, t)
o.Context = context.WithValue(o.Context, defaultTTLKey{}, t)
}
}

View File

@@ -40,22 +40,22 @@ func NewRegistry(opts ...registry.Option) registry.Registry {
for _, o := range opts {
o(&options)
}
exp, _ := options.Context.Value(expiryKey{}).(time.Duration)
defaultTTL, _ := options.Context.Value(defaultTTLKey{}).(time.Duration)
n := &storeregistry{
opts: options,
typ: _registryName,
expiry: exp,
opts: options,
typ: _registryName,
defaultTTL: defaultTTL,
}
n.store = natsjskv.NewStore(n.storeOptions(options)...)
return n
}
type storeregistry struct {
opts registry.Options
store store.Store
typ string
expiry time.Duration
lock sync.RWMutex
opts registry.Options
store store.Store
typ string
defaultTTL time.Duration
lock sync.RWMutex
}
// Init inits the registry
@@ -76,7 +76,7 @@ func (n *storeregistry) Options() registry.Options {
}
// 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()
defer n.lock.RUnlock()
@@ -84,6 +84,12 @@ func (n *storeregistry) Register(s *registry.Service, _ ...registry.RegisterOpti
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()
if s.Metadata == nil {
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{
Key: s.Name + _serviceDelimiter + unique,
Value: b,
Expiry: n.expiry,
Expiry: options.TTL,
})
}
@@ -180,8 +186,8 @@ func (n *storeregistry) storeOptions(opts registry.Options) []store.Option {
natsjskv.EncodeKeys(),
}
if ttl, ok := opts.Context.Value(expiryKey{}).(time.Duration); ok {
storeoptions = append(storeoptions, natsjskv.DefaultTTL(ttl))
if defaultTTL, ok := opts.Context.Value(defaultTTLKey{}).(time.Duration); ok {
storeoptions = append(storeoptions, natsjskv.DefaultTTL(defaultTTL))
}
addr := []string{"127.0.0.1:9233"}

View File

@@ -36,7 +36,7 @@ type Config struct {
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
DisableCache bool `mapstructure:"disable_cache"`
TTL time.Duration `mapstructure:"ttl"`
RegisterTTL time.Duration `mapstructure:"register_ttl"`
}
// 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
_reg = natsjsregistry.NewRegistry(
mRegistry.Addrs(cfg.Addresses...),
natsjsregistry.ServiceExpiry(cfg.TTL),
natsjsregistry.DefaultTTL(cfg.RegisterTTL),
)
case "memory":
_reg = memr.NewRegistry()
@@ -121,7 +121,7 @@ func getEnvs(opts ...Option) *Config {
cfg.Addresses = s
}
cfg.TTL = GetRegisterTTL()
cfg.RegisterTTL = GetRegisterTTL()
for _, o := range opts {
o(cfg)