diff --git a/ocis-pkg/generators/natsnames.go b/ocis-pkg/generators/natsnames.go new file mode 100644 index 00000000000..93cb28f120e --- /dev/null +++ b/ocis-pkg/generators/natsnames.go @@ -0,0 +1,38 @@ +package generators + +import ( + "os" + "strconv" +) + +type NType int + +const ( + NTYPE_BUS NType = iota + NTYPE_KEYVALUE + NTYPE_REGISTRY +) + +func (n NType) String() string { + return []string{"bus", "kv", "reg"}[n] +} + +func GenerateConnectionName(service string, ntype NType) string { + host, err := os.Hostname() + if err != nil { + host = "" + } + + return firstNRunes(host, 5) + ":" + strconv.Itoa(os.Getpid()) + ":" + service + ":" + ntype.String() +} + +func firstNRunes(s string, n int) string { + i := 0 + for j := range s { + if i == n { + return s[:j] + } + i++ + } + return s +} diff --git a/services/activitylog/pkg/command/server.go b/services/activitylog/pkg/command/server.go index 4641e6d8aed..0c59de5e547 100644 --- a/services/activitylog/pkg/command/server.go +++ b/services/activitylog/pkg/command/server.go @@ -13,6 +13,7 @@ import ( microstore "go-micro.dev/v4/store" "github.com/owncloud/ocis/v2/ocis-pkg/config/configlog" + "github.com/owncloud/ocis/v2/ocis-pkg/generators" "github.com/owncloud/ocis/v2/ocis-pkg/registry" ogrpc "github.com/owncloud/ocis/v2/ocis-pkg/service/grpc" "github.com/owncloud/ocis/v2/ocis-pkg/tracing" @@ -70,7 +71,8 @@ func Server(cfg *config.Config) *cli.Command { defer cancel() - evStream, err := stream.NatsFromConfig(cfg.Service.Name, false, stream.NatsConfig(cfg.Events)) + connName := generators.GenerateConnectionName(cfg.Service.Name, generators.NTYPE_BUS) + evStream, err := stream.NatsFromConfig(connName, false, stream.NatsConfig(cfg.Events)) if err != nil { logger.Error().Err(err).Msg("Failed to initialize event stream") return err diff --git a/services/antivirus/pkg/service/service.go b/services/antivirus/pkg/service/service.go index 55a28bd5295..68e246ac730 100644 --- a/services/antivirus/pkg/service/service.go +++ b/services/antivirus/pkg/service/service.go @@ -19,6 +19,7 @@ import ( "github.com/owncloud/reva/v2/pkg/rhttp" "go.opentelemetry.io/otel/trace" + "github.com/owncloud/ocis/v2/ocis-pkg/generators" "github.com/owncloud/ocis/v2/ocis-pkg/log" "github.com/owncloud/ocis/v2/services/antivirus/pkg/config" "github.com/owncloud/ocis/v2/services/antivirus/pkg/scanners" @@ -107,7 +108,8 @@ func (av Antivirus) Run() error { av.c.Events.TLSInsecure = false } - natsStream, err := stream.NatsFromConfig(av.c.Service.Name, false, stream.NatsConfig(av.c.Events)) + connName := generators.GenerateConnectionName(av.c.Service.Name, generators.NTYPE_BUS) + natsStream, err := stream.NatsFromConfig(connName, false, stream.NatsConfig(av.c.Events)) if err != nil { return err } diff --git a/services/audit/pkg/command/server.go b/services/audit/pkg/command/server.go index c2bb45b7bee..b58a0965fdc 100644 --- a/services/audit/pkg/command/server.go +++ b/services/audit/pkg/command/server.go @@ -10,6 +10,7 @@ import ( "github.com/urfave/cli/v2" "github.com/owncloud/ocis/v2/ocis-pkg/config/configlog" + "github.com/owncloud/ocis/v2/ocis-pkg/generators" "github.com/owncloud/ocis/v2/services/audit/pkg/config" "github.com/owncloud/ocis/v2/services/audit/pkg/config/parser" "github.com/owncloud/ocis/v2/services/audit/pkg/logging" @@ -36,7 +37,8 @@ func Server(cfg *config.Config) *cli.Command { ) defer cancel() - client, err := stream.NatsFromConfig(cfg.Service.Name, false, stream.NatsConfig(cfg.Events)) + connName := generators.GenerateConnectionName(cfg.Service.Name, generators.NTYPE_BUS) + client, err := stream.NatsFromConfig(connName, false, stream.NatsConfig(cfg.Events)) if err != nil { return err } diff --git a/services/clientlog/pkg/command/server.go b/services/clientlog/pkg/command/server.go index 4096630a376..1ba02f51bde 100644 --- a/services/clientlog/pkg/command/server.go +++ b/services/clientlog/pkg/command/server.go @@ -11,6 +11,7 @@ import ( "github.com/urfave/cli/v2" "github.com/owncloud/ocis/v2/ocis-pkg/config/configlog" + "github.com/owncloud/ocis/v2/ocis-pkg/generators" "github.com/owncloud/ocis/v2/ocis-pkg/registry" "github.com/owncloud/ocis/v2/ocis-pkg/tracing" "github.com/owncloud/ocis/v2/ocis-pkg/version" @@ -68,7 +69,8 @@ func Server(cfg *config.Config) *cli.Command { defer cancel() - s, err := stream.NatsFromConfig(cfg.Service.Name, false, stream.NatsConfig(cfg.Events)) + connName := generators.GenerateConnectionName(cfg.Service.Name, generators.NTYPE_BUS) + s, err := stream.NatsFromConfig(connName, false, stream.NatsConfig(cfg.Events)) if err != nil { return err } diff --git a/services/eventhistory/pkg/command/server.go b/services/eventhistory/pkg/command/server.go index 122d849b8c1..c8df047f11a 100644 --- a/services/eventhistory/pkg/command/server.go +++ b/services/eventhistory/pkg/command/server.go @@ -11,6 +11,7 @@ import ( microstore "go-micro.dev/v4/store" "github.com/owncloud/ocis/v2/ocis-pkg/config/configlog" + "github.com/owncloud/ocis/v2/ocis-pkg/generators" ogrpc "github.com/owncloud/ocis/v2/ocis-pkg/service/grpc" "github.com/owncloud/ocis/v2/ocis-pkg/tracing" "github.com/owncloud/ocis/v2/ocis-pkg/version" @@ -55,7 +56,8 @@ func Server(cfg *config.Config) *cli.Command { m.BuildInfo.WithLabelValues(version.GetString()).Set(1) - consumer, err := stream.NatsFromConfig(cfg.Service.Name, false, stream.NatsConfig(cfg.Events)) + connName := generators.GenerateConnectionName(cfg.Service.Name, generators.NTYPE_BUS) + consumer, err := stream.NatsFromConfig(connName, false, stream.NatsConfig(cfg.Events)) if err != nil { return err } diff --git a/services/frontend/pkg/command/events.go b/services/frontend/pkg/command/events.go index 8fae973dd57..87777131935 100644 --- a/services/frontend/pkg/command/events.go +++ b/services/frontend/pkg/command/events.go @@ -18,6 +18,7 @@ import ( "go-micro.dev/v4/metadata" "google.golang.org/protobuf/types/known/fieldmaskpb" + "github.com/owncloud/ocis/v2/ocis-pkg/generators" "github.com/owncloud/ocis/v2/ocis-pkg/log" "github.com/owncloud/ocis/v2/ocis-pkg/middleware" "github.com/owncloud/ocis/v2/ocis-pkg/registry" @@ -34,7 +35,8 @@ var _registeredEvents = []events.Unmarshaller{ // ListenForEvents listens for events and acts accordingly func ListenForEvents(ctx context.Context, cfg *config.Config, l log.Logger) error { - bus, err := stream.NatsFromConfig(cfg.Service.Name, false, stream.NatsConfig(cfg.Events)) + connName := generators.GenerateConnectionName(cfg.Service.Name, generators.NTYPE_BUS) + bus, err := stream.NatsFromConfig(connName, false, stream.NatsConfig(cfg.Events)) if err != nil { l.Error().Err(err).Msg("cannot connect to nats") return err diff --git a/services/graph/pkg/server/http/server.go b/services/graph/pkg/server/http/server.go index 17acd53ff1b..36641a76c5b 100644 --- a/services/graph/pkg/server/http/server.go +++ b/services/graph/pkg/server/http/server.go @@ -14,6 +14,7 @@ import ( "github.com/owncloud/ocis/v2/ocis-pkg/account" "github.com/owncloud/ocis/v2/ocis-pkg/cors" + "github.com/owncloud/ocis/v2/ocis-pkg/generators" "github.com/owncloud/ocis/v2/ocis-pkg/keycloak" "github.com/owncloud/ocis/v2/ocis-pkg/middleware" "github.com/owncloud/ocis/v2/ocis-pkg/registry" @@ -53,7 +54,8 @@ func Server(opts ...Option) (http.Service, error) { if options.Config.Events.Endpoint != "" { var err error - eventsStream, err = stream.NatsFromConfig(options.Config.Service.Name, false, stream.NatsConfig(options.Config.Events)) + connName := generators.GenerateConnectionName(options.Config.Service.Name, generators.NTYPE_BUS) + eventsStream, err = stream.NatsFromConfig(connName, false, stream.NatsConfig(options.Config.Events)) if err != nil { options.Logger.Error(). Err(err). diff --git a/services/notifications/pkg/command/send_email.go b/services/notifications/pkg/command/send_email.go index 52051c55149..7da6c590f04 100644 --- a/services/notifications/pkg/command/send_email.go +++ b/services/notifications/pkg/command/send_email.go @@ -3,6 +3,7 @@ package command import ( "fmt" + "github.com/owncloud/ocis/v2/ocis-pkg/generators" "github.com/owncloud/ocis/v2/services/notifications/pkg/config" "github.com/owncloud/reva/v2/pkg/events" "github.com/owncloud/reva/v2/pkg/events/stream" @@ -33,7 +34,8 @@ func SendEmail(cfg *config.Config) *cli.Command { if !daily && !weekly { return errors.New("at least one of '--daily' or '--weekly' must be set") } - s, err := stream.NatsFromConfig(cfg.Service.Name, false, stream.NatsConfig(cfg.Notifications.Events)) + connName := generators.GenerateConnectionName(cfg.Service.Name, generators.NTYPE_BUS) + s, err := stream.NatsFromConfig(connName, false, stream.NatsConfig(cfg.Notifications.Events)) if err != nil { return err } diff --git a/services/notifications/pkg/command/server.go b/services/notifications/pkg/command/server.go index b64745d4e0b..902365a2d6b 100644 --- a/services/notifications/pkg/command/server.go +++ b/services/notifications/pkg/command/server.go @@ -17,6 +17,7 @@ import ( "github.com/owncloud/reva/v2/pkg/rgrpc/todo/pool" "github.com/owncloud/ocis/v2/ocis-pkg/config/configlog" + "github.com/owncloud/ocis/v2/ocis-pkg/generators" "github.com/owncloud/ocis/v2/ocis-pkg/registry" "github.com/owncloud/ocis/v2/ocis-pkg/service/grpc" "github.com/owncloud/ocis/v2/ocis-pkg/tracing" @@ -95,7 +96,8 @@ func Server(cfg *config.Config) *cli.Command { registeredEvents[typ.String()] = e } - client, err := stream.NatsFromConfig(cfg.Service.Name, false, stream.NatsConfig(cfg.Notifications.Events)) + connName := generators.GenerateConnectionName(cfg.Service.Name, generators.NTYPE_BUS) + client, err := stream.NatsFromConfig(connName, false, stream.NatsConfig(cfg.Notifications.Events)) if err != nil { return err } diff --git a/services/policies/pkg/command/server.go b/services/policies/pkg/command/server.go index 7fcd5ec7397..071972d212f 100644 --- a/services/policies/pkg/command/server.go +++ b/services/policies/pkg/command/server.go @@ -9,6 +9,7 @@ import ( "github.com/urfave/cli/v2" "github.com/owncloud/ocis/v2/ocis-pkg/config/configlog" + "github.com/owncloud/ocis/v2/ocis-pkg/generators" "github.com/owncloud/ocis/v2/ocis-pkg/log" "github.com/owncloud/ocis/v2/ocis-pkg/service/grpc" "github.com/owncloud/ocis/v2/ocis-pkg/tracing" @@ -104,7 +105,8 @@ func Server(cfg *config.Config) *cli.Command { { - bus, err := stream.NatsFromConfig(cfg.Service.Name, false, stream.NatsConfig(cfg.Events)) + connName := generators.GenerateConnectionName(cfg.Service.Name, generators.NTYPE_BUS) + bus, err := stream.NatsFromConfig(connName, false, stream.NatsConfig(cfg.Events)) if err != nil { return err } diff --git a/services/postprocessing/pkg/command/postprocessing.go b/services/postprocessing/pkg/command/postprocessing.go index e83f746759d..3d6abe16346 100644 --- a/services/postprocessing/pkg/command/postprocessing.go +++ b/services/postprocessing/pkg/command/postprocessing.go @@ -4,6 +4,7 @@ import ( "context" "github.com/owncloud/ocis/v2/ocis-pkg/config/configlog" + "github.com/owncloud/ocis/v2/ocis-pkg/generators" "github.com/owncloud/ocis/v2/services/postprocessing/pkg/config" "github.com/owncloud/ocis/v2/services/postprocessing/pkg/config/parser" "github.com/owncloud/reva/v2/pkg/events" @@ -40,7 +41,8 @@ func RestartPostprocessing(cfg *config.Config) *cli.Command { return configlog.ReturnFatal(parser.ParseConfig(cfg)) }, Action: func(c *cli.Context) error { - stream, err := stream.NatsFromConfig(cfg.Service.Name, false, stream.NatsConfig(cfg.Postprocessing.Events)) + connName := generators.GenerateConnectionName(cfg.Service.Name, generators.NTYPE_BUS) + stream, err := stream.NatsFromConfig(connName, false, stream.NatsConfig(cfg.Postprocessing.Events)) if err != nil { return err } diff --git a/services/postprocessing/pkg/command/server.go b/services/postprocessing/pkg/command/server.go index 2713b86badf..326f74f3a78 100644 --- a/services/postprocessing/pkg/command/server.go +++ b/services/postprocessing/pkg/command/server.go @@ -11,6 +11,7 @@ import ( "github.com/urfave/cli/v2" microstore "go-micro.dev/v4/store" + "github.com/owncloud/ocis/v2/ocis-pkg/generators" "github.com/owncloud/ocis/v2/ocis-pkg/tracing" "github.com/owncloud/ocis/v2/services/postprocessing/pkg/config" "github.com/owncloud/ocis/v2/services/postprocessing/pkg/config/parser" @@ -47,7 +48,8 @@ func Server(cfg *config.Config) *cli.Command { } { - bus, err := stream.NatsFromConfig(cfg.Service.Name, false, stream.NatsConfig(cfg.Postprocessing.Events)) + connName := generators.GenerateConnectionName(cfg.Service.Name, generators.NTYPE_BUS) + bus, err := stream.NatsFromConfig(connName, false, stream.NatsConfig(cfg.Postprocessing.Events)) if err != nil { return err } diff --git a/services/proxy/pkg/command/server.go b/services/proxy/pkg/command/server.go index b2c27cb3ed1..77ec3c3db4b 100644 --- a/services/proxy/pkg/command/server.go +++ b/services/proxy/pkg/command/server.go @@ -12,6 +12,7 @@ import ( "github.com/justinas/alice" "github.com/oklog/run" "github.com/owncloud/ocis/v2/ocis-pkg/config/configlog" + "github.com/owncloud/ocis/v2/ocis-pkg/generators" "github.com/owncloud/ocis/v2/ocis-pkg/log" pkgmiddleware "github.com/owncloud/ocis/v2/ocis-pkg/middleware" "github.com/owncloud/ocis/v2/ocis-pkg/oidc" @@ -156,7 +157,8 @@ func Server(cfg *config.Config) *cli.Command { var publisher events.Stream if cfg.Events.Endpoint != "" { var err error - publisher, err = stream.NatsFromConfig(cfg.Service.Name, false, stream.NatsConfig(cfg.Events)) + connName := generators.GenerateConnectionName(cfg.Service.Name, generators.NTYPE_BUS) + publisher, err = stream.NatsFromConfig(connName, false, stream.NatsConfig(cfg.Events)) if err != nil { logger.Error(). Err(err). diff --git a/services/search/pkg/service/grpc/v0/service.go b/services/search/pkg/service/grpc/v0/service.go index da6bf40d797..9ba4f3ee7a1 100644 --- a/services/search/pkg/service/grpc/v0/service.go +++ b/services/search/pkg/service/grpc/v0/service.go @@ -22,6 +22,7 @@ import ( "go-micro.dev/v4/metadata" grpcmetadata "google.golang.org/grpc/metadata" + "github.com/owncloud/ocis/v2/ocis-pkg/generators" "github.com/owncloud/ocis/v2/ocis-pkg/log" "github.com/owncloud/ocis/v2/ocis-pkg/registry" v0 "github.com/owncloud/ocis/v2/protogen/gen/ocis/messages/search/v0" @@ -79,7 +80,8 @@ func NewHandler(opts ...Option) (searchsvc.SearchProviderHandler, func(), error) return nil, teardown, fmt.Errorf("unknown search extractor: %s", cfg.Extractor.Type) } - bus, err := stream.NatsFromConfig(cfg.Service.Name, false, stream.NatsConfig{ + connName := generators.GenerateConnectionName(cfg.Service.Name, generators.NTYPE_BUS) + bus, err := stream.NatsFromConfig(connName, false, stream.NatsConfig{ Endpoint: cfg.Events.Endpoint, Cluster: cfg.Events.Cluster, EnableTLS: cfg.Events.EnableTLS, diff --git a/services/sse/pkg/command/server.go b/services/sse/pkg/command/server.go index a48b128f609..d0b3d024191 100644 --- a/services/sse/pkg/command/server.go +++ b/services/sse/pkg/command/server.go @@ -10,6 +10,7 @@ import ( "github.com/urfave/cli/v2" "github.com/owncloud/ocis/v2/ocis-pkg/config/configlog" + "github.com/owncloud/ocis/v2/ocis-pkg/generators" "github.com/owncloud/ocis/v2/ocis-pkg/log" "github.com/owncloud/ocis/v2/ocis-pkg/tracing" "github.com/owncloud/ocis/v2/services/sse/pkg/config" @@ -52,7 +53,8 @@ func Server(cfg *config.Config) *cli.Command { } { - natsStream, err := stream.NatsFromConfig(cfg.Service.Name, true, stream.NatsConfig(cfg.Events)) + connName := generators.GenerateConnectionName(cfg.Service.Name, generators.NTYPE_BUS) + natsStream, err := stream.NatsFromConfig(connName, true, stream.NatsConfig(cfg.Events)) if err != nil { return err } diff --git a/services/storage-users/pkg/event/event.go b/services/storage-users/pkg/event/event.go index 48c2124fff4..c6282d63149 100644 --- a/services/storage-users/pkg/event/event.go +++ b/services/storage-users/pkg/event/event.go @@ -1,6 +1,7 @@ package event import ( + "github.com/owncloud/ocis/v2/ocis-pkg/generators" "github.com/owncloud/ocis/v2/services/storage-users/pkg/config" "github.com/owncloud/reva/v2/pkg/events/stream" "go-micro.dev/v4/events" @@ -8,7 +9,8 @@ import ( // NewStream prepares the requested nats stream and returns it. func NewStream(cfg *config.Config) (events.Stream, error) { - return stream.NatsFromConfig(cfg.Service.Name, false, stream.NatsConfig{ + connName := generators.GenerateConnectionName(cfg.Service.Name, generators.NTYPE_BUS) + return stream.NatsFromConfig(connName, false, stream.NatsConfig{ Endpoint: cfg.Events.Addr, Cluster: cfg.Events.ClusterID, EnableTLS: cfg.Events.EnableTLS, diff --git a/services/userlog/pkg/command/server.go b/services/userlog/pkg/command/server.go index d3b1fe0a844..96302d94ae2 100644 --- a/services/userlog/pkg/command/server.go +++ b/services/userlog/pkg/command/server.go @@ -13,6 +13,7 @@ import ( microstore "go-micro.dev/v4/store" "github.com/owncloud/ocis/v2/ocis-pkg/config/configlog" + "github.com/owncloud/ocis/v2/ocis-pkg/generators" "github.com/owncloud/ocis/v2/ocis-pkg/registry" ogrpc "github.com/owncloud/ocis/v2/ocis-pkg/service/grpc" "github.com/owncloud/ocis/v2/ocis-pkg/tracing" @@ -78,7 +79,8 @@ func Server(cfg *config.Config) *cli.Command { defer cancel() - stream, err := stream.NatsFromConfig(cfg.Service.Name, false, stream.NatsConfig(cfg.Events)) + connName := generators.GenerateConnectionName(cfg.Service.Name, generators.NTYPE_BUS) + stream, err := stream.NatsFromConfig(connName, false, stream.NatsConfig(cfg.Events)) if err != nil { return err }