Skip to content
This repository has been archived by the owner on Dec 28, 2024. It is now read-only.

Commit

Permalink
fix: add server_name to enats
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Nov 1, 2023
1 parent 5e584c9 commit 3fbef4f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
2 changes: 1 addition & 1 deletion broker/nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions enats/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package enats
type Config struct {
Debug bool
Trace bool
Name string
ServiceAddr string
ClusterAddr string
ClusterName string
Expand Down
47 changes: 37 additions & 10 deletions enats/enats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
Expand Down Expand Up @@ -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 != "" {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

0 comments on commit 3fbef4f

Please sign in to comment.