From 3fbef4fd4b38165f3c876ec041542aae6ae3bb61 Mon Sep 17 00:00:00 2001 From: Vladimir Dementyev Date: Tue, 31 Oct 2023 07:55:58 -0700 Subject: [PATCH] fix: add server_name to enats --- broker/nats.go | 2 +- enats/config.go | 1 + enats/enats.go | 47 +++++++++++++++++++++++++++++++++++++---------- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/broker/nats.go b/broker/nats.go index 44d287eb..754021e6 100644 --- a/broker/nats.go +++ b/broker/nats.go @@ -167,7 +167,7 @@ func (n *NATS) Shutdown(ctx context.Context) error { func (n *NATS) Announce() string { brokerParams := fmt.Sprintf("(history limit: %d, history ttl: %ds, sessions ttl: %ds)", n.conf.HistoryLimit, n.conf.HistoryTTL, n.conf.SessionsTTL) - return fmt.Sprintf("Starting NATS broker: %s %s", n.nconf.Servers, brokerParams) + return fmt.Sprintf("Using NATS broker: %s %s", n.nconf.Servers, brokerParams) } func (n *NATS) Epoch() string { diff --git a/enats/config.go b/enats/config.go index 2068a675..1d48bfb3 100644 --- a/enats/config.go +++ b/enats/config.go @@ -4,6 +4,7 @@ package enats type Config struct { Debug bool Trace bool + Name string ServiceAddr string ClusterAddr string ClusterName string diff --git a/enats/enats.go b/enats/enats.go index 1c93f988..34c00dcb 100644 --- a/enats/enats.go +++ b/enats/enats.go @@ -9,10 +9,12 @@ import ( "net/url" "strconv" "strings" + "sync" "time" "github.com/apex/log" "github.com/joomcode/errorx" + gonanoid "github.com/matoous/go-nanoid" "github.com/nats-io/nats-server/v2/server" "github.com/nats-io/nats.go" ) @@ -25,6 +27,9 @@ const ( type Service struct { config *Config server *server.Server + name string + + mu sync.Mutex } // LogEntry represents LoggerV2 decorator for nats server logger @@ -88,15 +93,16 @@ func (s *Service) Start() error { } opts := &server.Options{ - Host: u.Hostname(), - Port: int(port), - Debug: s.config.Debug, - Trace: s.config.Trace, - Cluster: clusterOpts, - Gateway: gatewayOpts, - Routes: routes, - NoSigs: true, - JetStream: s.config.JetStream, + Host: u.Hostname(), + Port: int(port), + Debug: s.config.Debug, + Trace: s.config.Trace, + ServerName: s.serverName(), + Cluster: clusterOpts, + Gateway: gatewayOpts, + Routes: routes, + NoSigs: true, + JetStream: s.config.JetStream, } if s.config.StoreDir != "" { @@ -132,8 +138,10 @@ func (s *Service) WaitReady() error { func (s *Service) Description() string { var builder strings.Builder + builder.WriteString(fmt.Sprintf("server_name: %s", s.serverName())) + if s.config.ClusterAddr != "" { - builder.WriteString(fmt.Sprintf("cluster: %s, cluster_name: %s", s.config.ClusterAddr, s.config.ClusterName)) + builder.WriteString(fmt.Sprintf(", cluster: %s, cluster_name: %s", s.config.ClusterAddr, s.config.ClusterName)) } if s.config.Routes != nil { @@ -269,3 +277,22 @@ func parseAddress(addr string) (string, int, error) { return uri.Hostname(), int(port), nil } + +func (s *Service) serverName() string { + s.mu.Lock() + defer s.mu.Unlock() + + if s.name != "" { + return s.name + } + + if s.config.Name != "" { + s.name = s.config.Name + return s.name + } + + suf, _ := gonanoid.Nanoid(3) // nolint: errcheck + + s.name = strings.ReplaceAll(strings.ReplaceAll(s.config.ServiceAddr, ":", "-"), "/", "") + "-" + suf + return s.name +}