github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/broker/network/network_cluster.go (about) 1 // Copyright (c) 2020-2021, R.I. Pienaar and the Choria Project contributors 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 5 package network 6 7 import ( 8 "fmt" 9 10 gnatsd "github.com/nats-io/nats-server/v2/server" 11 ) 12 13 func (s *Server) setupCluster() (err error) { 14 peers, err := s.choria.NetworkBrokerPeers() 15 if err != nil { 16 return fmt.Errorf("could not determine network broker peers: %s", err) 17 } 18 19 if peers.Count() == 0 { 20 s.log.Infof("Skipping clustering configuration without any peers") 21 return nil 22 } 23 24 if peers.Count() > 0 && s.config.Choria.NetworkPeerPort == 0 { 25 s.log.Info("Defaulting Choria Broker Peer port to 5222") 26 s.config.Choria.NetworkPeerPort = 5222 27 } 28 29 s.opts.Cluster.Host = s.config.Choria.NetworkListenAddress 30 s.opts.Cluster.NoAdvertise = true 31 s.opts.Cluster.Port = s.config.Choria.NetworkPeerPort 32 s.opts.Cluster.Username = s.config.Choria.NetworkPeerUser 33 s.opts.Cluster.Password = s.config.Choria.NetworkPeerPassword 34 s.opts.Cluster.PoolSize = -1 35 36 for _, p := range peers.Servers() { 37 u, err := p.URL() 38 if err != nil { 39 return fmt.Errorf("could not parse Peer configuration: %s", err) 40 } 41 42 s.log.Infof("Adding %s as network peer", u.String()) 43 s.opts.Routes = append(s.opts.Routes, u) 44 } 45 46 // Remove any host/ip that points to itself in Route 47 newroutes, err := gnatsd.RemoveSelfReference(s.opts.Cluster.Port, s.opts.Routes) 48 if err != nil { 49 s.log.Warnf("could not remove own Self from cluster configuration: %s", err) 50 } else { 51 s.opts.Routes = newroutes 52 } 53 54 return 55 }