From eb3048f2df349faf9364c9069435d97e3d4a8184 Mon Sep 17 00:00:00 2001 From: gotjosh Date: Wed, 24 Feb 2021 15:35:16 +0000 Subject: [PATCH] Address review comments Signed-off-by: gotjosh --- api/v1/api.go | 2 +- api/v2/api.go | 2 +- cluster/cluster.go | 35 ++++++++++++++++++++--------------- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/api/v1/api.go b/api/v1/api.go index 1a02d172..94d77c24 100644 --- a/api/v1/api.go +++ b/api/v1/api.go @@ -216,7 +216,7 @@ func getClusterStatus(p cluster.ClusterPeer) *clusterStatus { for _, n := range p.Peers() { s.Peers = append(s.Peers, peerStatus{ - Name: n.String(), + Name: n.Name(), Address: n.Address(), }) } diff --git a/api/v2/api.go b/api/v2/api.go index 0cc1aead..7af6f580 100644 --- a/api/v2/api.go +++ b/api/v2/api.go @@ -179,7 +179,7 @@ func (api *API) getStatusHandler(params general_ops.GetStatusParams) middleware. peers := []*open_api_models.PeerStatus{} for _, n := range api.peer.Peers() { address := n.Address() - name := n.String() + name := n.Name() peers = append(peers, &open_api_models.PeerStatus{ Name: &name, Address: &address, diff --git a/cluster/cluster.go b/cluster/cluster.go index 2cb1f1dc..31b3ce5c 100644 --- a/cluster/cluster.go +++ b/cluster/cluster.go @@ -33,22 +33,20 @@ import ( "github.com/prometheus/client_golang/prometheus" ) -// ClusterPeer helps consumers gather information about the peer(s) in the cluster +// Peer is a single peer in a gossip cluster. type ClusterPeer interface { // Name returns the unique identifier of this peer in the cluster. Name() string - // Address returns the IP address of this peer in the cluster. - Address() string // Status returns a status string representing the peer state. Status() string // Peers returns the peer nodes in the cluster. - Peers() []Node + Peers() []ClusterMember } -// Node interface that represents node peers in a cluster -type Node interface { - // String returns the name of the node - String() string +// ClusterMember interface that represents node peers in a cluster +type ClusterMember interface { + // Name returns the name of the node + Name() string // Address returns the IP address of the node Address() string } @@ -565,10 +563,6 @@ func (p *Peer) Name() string { return p.mlist.LocalNode().Name } -func (p *Peer) Address() string { - return p.mlist.LocalNode().Addr.String() -} - // ClusterSize returns the current number of alive members in the cluster. func (p *Peer) ClusterSize() int { return p.mlist.NumMembers() @@ -615,11 +609,22 @@ func (p *Peer) Self() *memberlist.Node { return p.mlist.LocalNode() } +// Member represents a member in the cluster. +type Member struct { + *memberlist.Node +} +// Name implements cluster.ClusterMember +func (m Member) Name() string { return m.Node.Name } +// Address implements cluster.ClusterMember +func (m Member) Address() string { return m.Node.Address() } + // Peers returns the peers in the cluster. -func (p *Peer) Peers() []Node { - peers := make([]Node, 0, len(p.mlist.Members())) +func (p *Peer) Peers() []ClusterMember { + peers := make([]ClusterMember, 0, len(p.mlist.Members())) for _, member := range p.mlist.Members() { - peers = append(peers, member) + peers = append(peers, Member{ + Node: member, + }) } return peers }