Skip to content

Commit

Permalink
gossip: refactor gossip config
Browse files Browse the repository at this point in the history
  • Loading branch information
andydunstall committed May 2, 2024
1 parent aeed802 commit 4f790eb
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 50 deletions.
2 changes: 1 addition & 1 deletion cli/server/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func run(conf *config.Config, logger log.Logger) error {
networkMap.Metrics().Register(registry)
adminServer.AddStatus("/netmap", netmap.NewStatus(networkMap))

gossiper, err := gossip.NewGossip(networkMap, conf, logger)
gossiper, err := gossip.NewGossip(networkMap, conf.Gossip, logger)
if err != nil {
return fmt.Errorf("gossip: %w", err)
}
Expand Down
48 changes: 4 additions & 44 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/andydunstall/pico/pkg/log"
"github.com/andydunstall/pico/server/gossip"
"github.com/spf13/pflag"
)

Expand Down Expand Up @@ -56,21 +57,6 @@ func (c *AdminConfig) Validate() error {
return nil
}

type GossipConfig struct {
// BindAddr is the address to bind to listen for gossip traffic.
BindAddr string `json:"bind_addr" yaml:"bind_addr"`

// AdvertiseAddr is the address to advertise to other nodes.
AdvertiseAddr string `json:"advertise_addr" yaml:"advertise_addr"`
}

func (c *GossipConfig) Validate() error {
if c.BindAddr == "" {
return fmt.Errorf("missing bind addr")
}
return nil
}

type ClusterConfig struct {
// NodeID is a unique identifier for this node in the cluster.
NodeID string `json:"node_id" yaml:"node_id"`
Expand Down Expand Up @@ -100,7 +86,7 @@ type Config struct {
Proxy ProxyConfig `json:"proxy" yaml:"proxy"`
Upstream UpstreamConfig `json:"upstream" yaml:"upstream"`
Admin AdminConfig `json:"admin" yaml:"admin"`
Gossip GossipConfig `json:"gossip" yaml:"gossip"`
Gossip gossip.Config `json:"gossip" yaml:"gossip"`
Cluster ClusterConfig `json:"cluster" yaml:"cluster"`
Server ServerConfig `json:"server" yaml:"server"`
Log log.Config `json:"log" yaml:"log"`
Expand Down Expand Up @@ -204,34 +190,6 @@ private IP will be used, such as a bind address of ':8002' may have an
advertise address of '10.26.104.14:8002'.`,
)

fs.StringVar(
&c.Gossip.BindAddr,
"gossip.bind-addr",
":7000",
`
The host/port to listen for inter-node gossip traffic.
If the host is unspecified it defaults to all listeners, such as
'--gossip.bind-addr :7000' will listen on '0.0.0.0:7000'`,
)

fs.StringVar(
&c.Gossip.AdvertiseAddr,
"gossip.advertise-addr",
"",
`
Gossip listen address to advertise to other nodes in the cluster. This is the
address other nodes will used to gossip with the node.
Such as if the listen address is ':7000', the advertised address may be
'10.26.104.45:7000' or 'node1.cluster:7000'.
By default, if the bind address includes an IP to bind to that will be used.
If the bind address does not include an IP (such as ':7000') the nodes
private IP will be used, such as a bind address of ':7000' may have an
advertise address of '10.26.104.14:7000'.`,
)

fs.IntVar(
&c.Server.GracefulShutdownTimeout,
"server.graceful-shutdown-timeout",
Expand Down Expand Up @@ -286,5 +244,7 @@ Note each node propagates membership information to the other known nodes,
so the initial set of configured members only needs to be a subset of nodes.`,
)

c.Gossip.RegisterFlags(fs)

c.Log.RegisterFlags(fs)
}
52 changes: 52 additions & 0 deletions server/gossip/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package gossip

import (
"fmt"

"github.com/spf13/pflag"
)

type Config struct {
// BindAddr is the address to bind to listen for gossip traffic.
BindAddr string `json:"bind_addr" yaml:"bind_addr"`

// AdvertiseAddr is the address to advertise to other nodes.
AdvertiseAddr string `json:"advertise_addr" yaml:"advertise_addr"`
}

func (c *Config) Validate() error {
if c.BindAddr == "" {
return fmt.Errorf("missing bind addr")
}
return nil
}

func (c *Config) RegisterFlags(fs *pflag.FlagSet) {
fs.StringVar(
&c.BindAddr,
"gossip.bind-addr",
":7000",
`
The host/port to listen for inter-node gossip traffic.
If the host is unspecified it defaults to all listeners, such as
'--gossip.bind-addr :7000' will listen on '0.0.0.0:7000'`,
)

fs.StringVar(
&c.AdvertiseAddr,
"gossip.advertise-addr",
"",
`
Gossip listen address to advertise to other nodes in the cluster. This is the
address other nodes will used to gossip with the node.
Such as if the listen address is ':7000', the advertised address may be
'10.26.104.45:7000' or 'node1.cluster:7000'.
By default, if the bind address includes an IP to bind to that will be used.
If the bind address does not include an IP (such as ':7000') the nodes
private IP will be used, such as a bind address of ':7000' may have an
advertise address of '10.26.104.14:7000'.`,
)
}
9 changes: 4 additions & 5 deletions server/gossip/gossip.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/andydunstall/kite"
"github.com/andydunstall/pico/pkg/log"
"github.com/andydunstall/pico/server/config"
"github.com/andydunstall/pico/server/netmap"
)

Expand All @@ -24,23 +23,23 @@ type Gossip struct {
// updates.
gossiper *kite.Kite

conf *config.Config
conf Config

logger log.Logger
}

func NewGossip(
networkMap *netmap.NetworkMap,
conf *config.Config,
conf Config,
logger log.Logger,
) (*Gossip, error) {
logger = logger.WithSubsystem("gossip")

syncer := newSyncer(networkMap, logger)
gossiper, err := kite.New(
kite.WithNodeID(networkMap.LocalNode().ID),
kite.WithBindAddr(conf.Gossip.BindAddr),
kite.WithAdvertiseAddr(conf.Gossip.AdvertiseAddr),
kite.WithBindAddr(conf.BindAddr),
kite.WithAdvertiseAddr(conf.AdvertiseAddr),
kite.WithWatcher(syncer),
kite.WithLogger(logger.WithSubsystem("gossip.kite")),
)
Expand Down

0 comments on commit 4f790eb

Please sign in to comment.