Clean up verifyUniqueListners

The first pass over this was a hackjob in the sense that it was not elegant.  Fixed.

Pointed out by: subcionscious in the middle of the night
pull/1915/head
Sean Chittenden 2016-04-04 11:03:49 -07:00
parent 140cde6f42
commit 829478793b
1 changed files with 25 additions and 42 deletions

View File

@ -310,61 +310,44 @@ func (c *Command) readConfig() *Config {
// verifyUniqueListeners checks to see if an address was used more than once in // verifyUniqueListeners checks to see if an address was used more than once in
// the config // the config
func (config *Config) verifyUniqueListeners() error { func (config *Config) verifyUniqueListeners() error {
listeners := []struct {
host string
port int
descr string
}{
{config.Addresses.RPC, config.Ports.RPC, "RPC"},
{config.Addresses.DNS, config.Ports.DNS, "DNS"},
{config.Addresses.HTTP, config.Ports.HTTP, "HTTP"},
{config.Addresses.HTTPS, config.Ports.HTTPS, "HTTPS"},
{config.AdvertiseAddr, config.Ports.Server, "Server RPC"},
{config.AdvertiseAddr, config.Ports.SerfLan, "Serf LAN"},
{config.AdvertiseAddr, config.Ports.SerfWan, "Serf WAN"},
}
type key struct { type key struct {
host string host string
port int port int
} }
const numUniqueAddrs = 7 m := make(map[key]string, len(listeners))
m := make(map[key]string, numUniqueAddrs)
testFunc := func(k key, descr string) error { for _, l := range listeners {
if k.host == "" { if l.host == "" {
k.host = "0.0.0.0" l.host = "0.0.0.0"
} else if strings.HasPrefix(k.host, "unix") { } else if strings.HasPrefix(l.host, "unix") {
// Don't compare ports on unix sockets // Don't compare ports on unix sockets
k.port = 0 l.port = 0
} }
if k.host == "0.0.0.0" && k.port <= 0 { if l.host == "0.0.0.0" && l.port <= 0 {
return nil continue
} }
k := key{l.host, l.port}
v, ok := m[k] v, ok := m[k]
if ok { if ok {
return fmt.Errorf("%s address already configured for %s", descr, v) return fmt.Errorf("%s address already configured for %s", l.descr, v)
} }
m[k] = descr m[k] = l.descr
return nil
} }
if err := testFunc(key{config.Addresses.RPC, config.Ports.RPC}, "RPC"); err != nil {
return err
}
if err := testFunc(key{config.Addresses.DNS, config.Ports.DNS}, "DNS"); err != nil {
return err
}
if err := testFunc(key{config.Addresses.HTTP, config.Ports.HTTP}, "HTTP"); err != nil {
return err
}
if err := testFunc(key{config.Addresses.HTTPS, config.Ports.HTTPS}, "HTTPS"); err != nil {
return err
}
if err := testFunc(key{config.AdvertiseAddr, config.Ports.Server}, "Server RPC"); err != nil {
return err
}
if err := testFunc(key{config.AdvertiseAddr, config.Ports.SerfLan}, "Serf LAN"); err != nil {
return err
}
if err := testFunc(key{config.AdvertiseAddr, config.Ports.SerfWan}, "Serf WAN"); err != nil {
return err
}
return nil return nil
} }