2018-02-08 19:33:52 +01:00
|
|
|
package cluster
|
|
|
|
|
|
|
|
import (
|
2018-03-01 22:12:53 +01:00
|
|
|
"io/ioutil"
|
|
|
|
baselog "log"
|
|
|
|
|
2018-02-22 19:48:46 +01:00
|
|
|
"github.com/lbryio/lbry.go/errors"
|
2018-02-08 19:33:52 +01:00
|
|
|
|
|
|
|
"github.com/hashicorp/serf/serf"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Connect(nodeName, addr string, port int) (*serf.Serf, <-chan serf.Event, error) {
|
|
|
|
conf := serf.DefaultConfig()
|
|
|
|
conf.MemberlistConfig.BindPort = port
|
|
|
|
conf.MemberlistConfig.AdvertisePort = port
|
|
|
|
conf.NodeName = nodeName
|
|
|
|
|
2018-03-01 22:12:53 +01:00
|
|
|
nullLogger := baselog.New(ioutil.Discard, "", 0)
|
|
|
|
conf.Logger = nullLogger
|
|
|
|
|
2018-02-08 19:33:52 +01:00
|
|
|
eventCh := make(chan serf.Event)
|
|
|
|
conf.EventCh = eventCh
|
|
|
|
|
|
|
|
cluster, err := serf.Create(conf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Prefix("couldn't create cluster", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = cluster.Join([]string{addr}, true)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("couldn't join cluster, starting own: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cluster, eventCh, nil
|
|
|
|
}
|