Skip to content

Commit

Permalink
server: add node id
Browse files Browse the repository at this point in the history
  • Loading branch information
andydunstall committed Apr 6, 2024
1 parent 4e4ed7f commit 23bf992
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
14 changes: 14 additions & 0 deletions cli/server/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ connections to upstream listeners, announcing to the cluster the node is
leaving...`,
)

cmd.Flags().StringVar(
&conf.Cluster.NodeID,
"cluster.node-id",
"",
`
A unique identifier for the node in the cluster.
By default a random ID will be generated for the node.`,
)

cmd.Flags().StringSliceVar(
&conf.Cluster.Members,
"cluster.members",
Expand Down Expand Up @@ -168,6 +178,10 @@ Such as you can enable 'gossip' logs with '--log.subsystems gossip'.`,
os.Exit(1)
}

if conf.Cluster.NodeID == "" {
conf.Cluster.NodeID = netmap.GenerateNodeID()
}

run(&conf, logger)
}

Expand Down
2 changes: 2 additions & 0 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type ServerConfig struct {
}

type ClusterConfig struct {
NodeID string `json:"node_id"`

Members []string `json:"members"`
}

Expand Down
23 changes: 23 additions & 0 deletions server/netmap/node.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package netmap

import (
"crypto/rand"
"math/big"
)

var (
alphaNumericChars = []byte("abcdefghijklmnopqrstuvwxyz1234567890")
)

// Node represents the known state about a node in the cluster.
type Node struct {
// ID is a unique identifier for the node in the cluster.
Expand All @@ -15,3 +24,17 @@ type Node struct {
// active endpoint ID to the number of listeners for that endpoint.
Endpoints map[string]int
}

func GenerateNodeID() string {
b := make([]byte, 10)
for i := range b {
n, err := rand.Int(rand.Reader, big.NewInt(int64(len(alphaNumericChars))))
if err != nil {
// We don't expect to ever get an error so panic rather than try to
// handle.
panic("failed to generate random number: " + err.Error())
}
b[i] = alphaNumericChars[n.Int64()]
}
return string(b)
}

0 comments on commit 23bf992

Please sign in to comment.