feat: change the names of the connections to the registry

This commit is contained in:
Juan Pablo Villafáñez
2025-09-08 17:32:35 +02:00
committed by Jörn Friedrich Dreyer
parent ca2dc823ef
commit 925444a0b3
4 changed files with 23 additions and 3 deletions
+12
View File
@@ -10,6 +10,7 @@ import (
type storeOptionsKey struct{}
type defaultTTLKey struct{}
type serviceNameKey struct{}
// StoreOptions sets the options for the underlying store
func StoreOptions(opts []store.Option) registry.Option {
@@ -30,3 +31,14 @@ func DefaultTTL(t time.Duration) registry.Option {
o.Context = context.WithValue(o.Context, defaultTTLKey{}, t)
}
}
// ServiceName links the service name to the registry if possible.
// The name will be part of the connection name to the Nats registry
func ServiceName(name string) registry.Option {
return func(o *registry.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, serviceNameKey{}, name)
}
}
+7 -1
View File
@@ -13,6 +13,7 @@ import (
natsjskv "github.com/go-micro/plugins/v4/store/nats-js-kv"
"github.com/nats-io/nats.go"
"github.com/opencloud-eu/opencloud/pkg/generators"
"go-micro.dev/v4/registry"
"go-micro.dev/v4/server"
"go-micro.dev/v4/store"
@@ -186,6 +187,11 @@ func (n *storeregistry) storeOptions(opts registry.Options) []store.Option {
storeoptions = append(storeoptions, natsjskv.DefaultTTL(defaultTTL))
}
serviceName := "_oc" // use "_oc" as default service name if nothing else is provided
if name, ok := opts.Context.Value(serviceNameKey{}).(string); ok {
serviceName = name
}
addr := []string{"127.0.0.1:9233"}
if len(opts.Addrs) > 0 {
addr = opts.Addrs
@@ -195,7 +201,7 @@ func (n *storeregistry) storeOptions(opts registry.Options) []store.Option {
storeoptions = append(storeoptions, store.Nodes(addr...))
natsOptions := nats.GetDefaultOptions()
natsOptions.Name = "nats-js-kv-registry"
natsOptions.Name = generators.GenerateConnectionName(serviceName, generators.NTYPE_REGISTRY)
natsOptions.User, natsOptions.Password = getAuth()
natsOptions.ReconnectedCB = func(_ *nats.Conn) {
if err := n.Init(); err != nil {
+2 -1
View File
@@ -7,6 +7,7 @@ import (
"path/filepath"
"github.com/opencloud-eu/opencloud/pkg/config/defaults"
"github.com/opencloud-eu/opencloud/pkg/generators"
"github.com/opencloud-eu/opencloud/pkg/log"
"github.com/opencloud-eu/opencloud/services/sharing/pkg/config"
)
@@ -135,7 +136,7 @@ func SharingConfigFromStruct(cfg *config.Config, logger log.Logger) (map[string]
"tls-insecure": cfg.Events.TLSInsecure,
"tls-root-ca-cert": cfg.Events.TLSRootCaCertPath,
"enable-tls": cfg.Events.EnableTLS,
"name": "sharing-eventsmiddleware",
"name": generators.GenerateConnectionName(cfg.Service.Name, generators.NTYPE_BUS),
"username": cfg.Events.AuthUsername,
"password": cfg.Events.AuthPassword,
},
@@ -5,6 +5,7 @@ import (
"strconv"
"strings"
"github.com/opencloud-eu/opencloud/pkg/generators"
"github.com/opencloud-eu/opencloud/services/storage-users/pkg/config"
)
@@ -57,7 +58,7 @@ func StorageUsersConfigFromStruct(cfg *config.Config) map[string]interface{} {
"tls-insecure": cfg.Events.TLSInsecure,
"tls-root-ca-cert": cfg.Events.TLSRootCaCertPath,
"enable-tls": cfg.Events.EnableTLS,
"name": "storage-users-eventsmiddleware",
"name": generators.GenerateConnectionName(cfg.Service.Name, generators.NTYPE_BUS),
"username": cfg.Events.AuthUsername,
"password": cfg.Events.AuthPassword,
},