From 366f906d5dbf0d77246b6bc548f8e045ceea2a4d Mon Sep 17 00:00:00 2001 From: kaiyou Date: Wed, 6 May 2020 19:19:55 +0200 Subject: [PATCH] Do not pass the config object to newCluster Decouple the config structure from the cluster management and stop passing the config object around. --- cluster.go | 16 +++++++--------- main.go | 2 +- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/cluster.go b/cluster.go index 0e1c428..d8bfd70 100644 --- a/cluster.go +++ b/cluster.go @@ -34,11 +34,9 @@ type cluster struct { const statePath = "/var/lib/wesher/state.json" -func newCluster(config *config, getMeta func(int) []byte) (*cluster, error) { - clusterKey := config.ClusterKey - +func newCluster(init bool, clusterKey []byte, bindAddr string, bindIface string, bindPort int, useIPAsName bool, getMeta func(int) []byte) (*cluster, error) { state := &ClusterState{} - if !config.Init { + if !init { loadState(state) } @@ -47,7 +45,7 @@ func newCluster(config *config, getMeta func(int) []byte) (*cluster, error) { return nil, err } - bindAddr, err := computeBindAddr(config.BindAddr, config.BindIface) + bindAddr, err = computeBindAddr(bindAddr, bindIface) if err != nil { return nil, err } @@ -56,10 +54,10 @@ func newCluster(config *config, getMeta func(int) []byte) (*cluster, error) { mlConfig.LogOutput = logrus.StandardLogger().WriterLevel(logrus.DebugLevel) mlConfig.SecretKey = clusterKey mlConfig.BindAddr = bindAddr - mlConfig.BindPort = config.ClusterPort - mlConfig.AdvertisePort = config.ClusterPort - if config.UseIPAsName && config.BindAddr != "0.0.0.0" { - mlConfig.Name = config.BindAddr + mlConfig.BindPort = bindPort + mlConfig.AdvertisePort = bindPort + if useIPAsName && bindAddr != "0.0.0.0" { + mlConfig.Name = bindAddr } ml, err := memberlist.Create(mlConfig) diff --git a/main.go b/main.go index 572d407..53bc7aa 100644 --- a/main.go +++ b/main.go @@ -42,7 +42,7 @@ func main() { }, limit) } - cluster, err := newCluster(config, getMeta) + cluster, err := newCluster(config.Init, config.ClusterKey, config.BindAddr, config.BindIface, config.ClusterPort, config.UseIPAsName, getMeta) if err != nil { logrus.WithError(err).Fatal("could not create cluster") }