2018-06-04 16:40:04 +00:00
|
|
|
package element
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2018-09-14 12:25:35 +00:00
|
|
|
"net"
|
|
|
|
"strconv"
|
2018-06-04 16:40:04 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/memberlist"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ConnectionType defines the type of connection for the agent to use (wan, lan, local)
|
|
|
|
type ConnectionType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// LAN is a local area network connection intended for high speed, low latency networks
|
|
|
|
LAN ConnectionType = "lan"
|
|
|
|
// WAN is a wide area connection intended for long distance remote links
|
|
|
|
WAN ConnectionType = "wan"
|
|
|
|
// Local is a local connection intended for high speed local development links
|
|
|
|
Local ConnectionType = "local"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config is the agent config
|
|
|
|
type Config struct {
|
|
|
|
// ConnectionType is the connection type the agent will use
|
|
|
|
ConnectionType string
|
2018-09-14 12:25:35 +00:00
|
|
|
// ClusterAddress bind address
|
|
|
|
ClusterAddress string
|
|
|
|
// AdvertiseAddress for nat traversal
|
|
|
|
AdvertiseAddress string
|
2018-06-04 16:40:04 +00:00
|
|
|
// Peers is a local cache of peer members
|
|
|
|
Peers []string
|
2018-09-14 21:02:29 +00:00
|
|
|
// Debug output for memberlist
|
|
|
|
Debug bool
|
2018-06-04 16:40:04 +00:00
|
|
|
}
|
|
|
|
|
2018-06-07 05:17:03 +00:00
|
|
|
func (a *Agent) Config() *Config {
|
|
|
|
return a.config
|
|
|
|
}
|
|
|
|
|
2018-09-19 01:59:54 +00:00
|
|
|
func (cfg *Config) memberListConfig(a *Agent) (*memberlist.Config, error) {
|
2018-06-04 16:40:04 +00:00
|
|
|
var mc *memberlist.Config
|
|
|
|
switch cfg.ConnectionType {
|
|
|
|
case string(Local):
|
|
|
|
mc = memberlist.DefaultLocalConfig()
|
|
|
|
case string(WAN):
|
|
|
|
mc = memberlist.DefaultWANConfig()
|
|
|
|
case string(LAN):
|
|
|
|
mc = memberlist.DefaultLANConfig()
|
|
|
|
default:
|
|
|
|
return nil, ErrUnknownConnectionType
|
|
|
|
}
|
|
|
|
|
2018-09-19 01:59:54 +00:00
|
|
|
mc.Name = a.state.Self.ID
|
|
|
|
mc.Delegate = a
|
|
|
|
mc.Events = a
|
2018-06-04 16:40:04 +00:00
|
|
|
|
2018-09-14 21:02:29 +00:00
|
|
|
if !cfg.Debug {
|
|
|
|
mc.Logger = log.New(ioutil.Discard, "", 0)
|
|
|
|
}
|
2018-06-04 16:40:04 +00:00
|
|
|
|
2018-09-14 12:25:35 +00:00
|
|
|
host, port, err := net.SplitHostPort(cfg.ClusterAddress)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-06-04 16:40:04 +00:00
|
|
|
// ml overrides for connection
|
2018-09-14 12:25:35 +00:00
|
|
|
if v := host; v != "" {
|
|
|
|
mc.BindAddr = host
|
2018-06-04 16:40:04 +00:00
|
|
|
}
|
2018-09-14 12:25:35 +00:00
|
|
|
if v := port; v != "" {
|
|
|
|
mc.BindPort, _ = strconv.Atoi(port)
|
2018-06-04 16:40:04 +00:00
|
|
|
}
|
2018-09-14 12:25:35 +00:00
|
|
|
if host, port, err = net.SplitHostPort(cfg.ClusterAddress); err != nil {
|
|
|
|
return nil, err
|
2018-06-04 16:40:04 +00:00
|
|
|
}
|
2018-09-14 12:25:35 +00:00
|
|
|
if v := host; v != "" {
|
|
|
|
mc.AdvertiseAddr = host
|
|
|
|
}
|
|
|
|
if v := port; v != "" {
|
|
|
|
mc.AdvertisePort, _ = strconv.Atoi(port)
|
2018-06-04 16:40:04 +00:00
|
|
|
}
|
|
|
|
return mc, nil
|
|
|
|
}
|