From 0c6ca19602708f9f36129f62febeee5a30a08595 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Fri, 19 Jul 2024 12:37:08 +0200 Subject: [PATCH] naming is hard MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jörn Friedrich Dreyer --- ocis-pkg/natsjsregistry/options.go | 8 ++++---- ocis-pkg/natsjsregistry/registry.go | 32 +++++++++++++++++------------ ocis-pkg/registry/registry.go | 6 +++--- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/ocis-pkg/natsjsregistry/options.go b/ocis-pkg/natsjsregistry/options.go index dfcda616a5..533d0bd871 100644 --- a/ocis-pkg/natsjsregistry/options.go +++ b/ocis-pkg/natsjsregistry/options.go @@ -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) } } diff --git a/ocis-pkg/natsjsregistry/registry.go b/ocis-pkg/natsjsregistry/registry.go index 97094d757d..d9dcd5f932 100644 --- a/ocis-pkg/natsjsregistry/registry.go +++ b/ocis-pkg/natsjsregistry/registry.go @@ -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"} diff --git a/ocis-pkg/registry/registry.go b/ocis-pkg/registry/registry.go index 130b3d6305..d8df986ad7 100644 --- a/ocis-pkg/registry/registry.go +++ b/ocis-pkg/registry/registry.go @@ -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)