本文整理匯總了Golang中github.com/hashicorp/memberlist.Create函數的典型用法代碼示例。如果您正苦於以下問題:Golang Create函數的具體用法?Golang Create怎麽用?Golang Create使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Create函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: NewGossipNetwork
func NewGossipNetwork(netConf Config, executor UpdateExecutor) *Network {
conf := memberlist.DefaultLocalConfig()
network := &Network{
executor: executor,
version: netConf.LocalVersion,
}
conf.BindPort = netConf.LocalPort
conf.Name = netConf.Name
conf.Delegate = network
list, err := memberlist.Create(conf)
if err != nil {
panic("Failed to create memberlist: " + err.Error())
}
n := 0
for i := 0; i < 3; i++ {
n, err = list.Join(netConf.RootNodes)
if n > 0 {
break
}
}
if n == 0 {
panic("Can't connect to any of the root nodes: " + err.Error())
}
network.members = list
return network
}
示例2: New
//New returns a new instance of the cluster, created using the given Config.
func New(config *Config) (*Cluster, error) {
c := &Cluster{
Config: config,
name: fmt.Sprintf("%d", config.ID),
}
memberlistConfig := memberlist.DefaultLANConfig()
memberlistConfig.Name = c.name
memberlistConfig.BindAddr = config.Host
memberlistConfig.BindPort = config.Port
//TODO Cosmin temporarily disabling any logging from memberlist, we might want to enable it again using logrus?
memberlistConfig.LogOutput = ioutil.Discard
ml, err := memberlist.Create(memberlistConfig)
if err != nil {
logger.WithField("error", err).Error("Error when creating the internal memberlist of the cluster")
return nil, err
}
c.memberlist = ml
memberlistConfig.Delegate = c
memberlistConfig.Conflict = c
memberlistConfig.Events = c
return c, nil
}
示例3: clusterInit
func (nDB *NetworkDB) clusterInit() error {
config := memberlist.DefaultLANConfig()
config.Name = nDB.config.NodeName
config.BindAddr = nDB.config.BindAddr
if nDB.config.BindPort != 0 {
config.BindPort = nDB.config.BindPort
}
config.ProtocolVersion = memberlist.ProtocolVersionMax
config.Delegate = &delegate{nDB: nDB}
config.Events = &eventDelegate{nDB: nDB}
config.LogOutput = &logWriter{}
var err error
if len(nDB.config.Keys) > 0 {
for i, key := range nDB.config.Keys {
logrus.Debugf("Encryption key %d: %s", i+1, hex.EncodeToString(key)[0:5])
}
nDB.keyring, err = memberlist.NewKeyring(nDB.config.Keys, nDB.config.Keys[0])
if err != nil {
return err
}
config.Keyring = nDB.keyring
}
nDB.networkBroadcasts = &memberlist.TransmitLimitedQueue{
NumNodes: func() int {
return len(nDB.nodes)
},
RetransmitMult: config.RetransmitMult,
}
mlist, err := memberlist.Create(config)
if err != nil {
return fmt.Errorf("failed to create memberlist: %v", err)
}
nDB.stopCh = make(chan struct{})
nDB.memberlist = mlist
nDB.mConfig = config
for _, trigger := range []struct {
interval time.Duration
fn func()
}{
{reapInterval, nDB.reapState},
{config.GossipInterval, nDB.gossip},
{config.PushPullInterval, nDB.bulkSyncTables},
} {
t := time.NewTicker(trigger.interval)
go nDB.triggerFunc(trigger.interval, t.C, nDB.stopCh, trigger.fn)
nDB.tickers = append(nDB.tickers, t)
}
return nil
}
示例4: benchmarkCluster
func benchmarkCluster(b *testing.B, num int, timeoutForAllJoins time.Duration, lowestPort int) {
startTime := time.Now()
var nodes []*memberlist.Memberlist
eventC := make(chan memberlist.NodeEvent, num)
addr := "127.0.0.1"
var firstMemberName string
for i := 0; i < num; i++ {
c := memberlist.DefaultLANConfig()
port := lowestPort + i
c.Name = fmt.Sprintf("%s:%d", addr, port)
c.BindAddr = addr
c.BindPort = port
c.ProbeInterval = 20 * time.Millisecond
c.ProbeTimeout = 100 * time.Millisecond
c.GossipInterval = 20 * time.Millisecond
c.PushPullInterval = 200 * time.Millisecond
c.LogOutput = ioutil.Discard
if i == 0 {
c.Events = &memberlist.ChannelEventDelegate{eventC}
firstMemberName = c.Name
}
newMember, err := memberlist.Create(c)
if err != nil {
log.WithField("error", err).Fatal("Unexpected error when creating the memberlist")
}
nodes = append(nodes, newMember)
defer newMember.Shutdown()
if i > 0 {
numContacted, err := newMember.Join([]string{firstMemberName})
if numContacted == 0 || err != nil {
log.WithField("error", err).Fatal("Unexpected fatal error when node wanted to join the cluster")
}
}
}
if convergence(nodes, num, eventC, timeoutForAllJoins) {
endTime := time.Now()
log.WithField("durationSeconds", endTime.Sub(startTime).Seconds()).Info("Cluster convergence reached")
}
b.StartTimer()
sendMessagesInCluster(nodes, b.N)
b.StopTimer()
}
示例5: New
func New(cfg Config) *MemberlistTransport {
sd := &SyncerDelegate{}
var mlCfg *memberlist.Config = cfg.Config
mlCfg.Delegate = sd
ml, err := memberlist.Create(mlCfg)
if err != nil {
panic(err.Error())
}
n := &MemberlistTransport{
Memberlist: ml,
subscribers: make(map[libsyncer.MessageType][]Callback),
}
sd.Callback = n.receiveMessage
return n
}
示例6: Join
func (c *Cluster) Join() error {
nodeMeta, err := proto.Marshal(c.LocalNodeMeta)
if err != nil {
return err
}
c.nodeMetaBuffer = nodeMeta
m, err := memberlist.Create(c.config)
if err != nil {
return err
}
c.Members = m
_, err = m.Join([]string{c.existing})
return err
}
示例7: setupMemberlist
func (t *Tracker) setupMemberlist() (err error) {
conf := memberlist.DefaultLANConfig()
conf.LogOutput = ioutil.Discard
conf.Name = fmt.Sprintf("%s:%s", t.cfg.UUID().FullString(), t.iface)
conf.BindAddr = t.iface
conf.BindPort = t.port
conf.Delegate = newDelegate(t.adport, t.cfg)
conf.Events = t.evtHandler
t.memberlist, err = memberlist.Create(conf)
return
}
示例8: main
func main() {
fmt.Println("main A")
config := memberlist.DefaultLANConfig()
config.BindAddr = "192.168.50.25"
list, err := memberlist.Create(config)
n, err := list.Join([]string{"192.168.50.25"})
if err != nil {
panic("Failed to join cluster: " + err.Error())
}
log.Println("@n:", n)
if err != nil {
panic("Failed to create memberlist: " + err.Error())
}
for {
checkCluster(list)
time.Sleep(time.Second)
}
}
示例9: NewDistributedEventEmitter
func NewDistributedEventEmitter(cluster []string, bindAddr string) *DistributedEventEmitter {
dee := DistributedEventEmitter{}
c := memberlist.DefaultLANConfig()
c.Name = bindAddr
c.BindAddr = bindAddr
ml, err := memberlist.Create(c)
dee.ml = ml
if err != nil {
panic("Failed to create memberlist: " + err.Error())
}
_, err = dee.ml.Join(cluster)
if err != nil {
panic("Failed to join cluster: " + err.Error())
}
h, err := os.Hostname()
if err != nil {
panic("Failed to get hostname" + err.Error())
}
fmt.Sprintf(dee.nodeId, "%s:%d", h, os.Getpid())
dee.listeners = make(map[string][]func([]byte))
a, err := net.ResolveUDPAddr("udp", mcastAddr)
if err != nil {
panic("Error converting mcast addr: " + err.Error())
}
dee.sub, err = net.ListenMulticastUDP("udp", nil, a)
dee.sub.SetReadBuffer(maxDGSize)
if err != nil {
panic("Failed listen to UDP mcast: " + err.Error())
}
go dee.readLoop(dee.sub)
return &dee
}
示例10: CreateMemberlistAgent
func CreateMemberlistAgent(opdata *utilities.OPData, observer *Observer) *MemberlistAgent {
ma := new(MemberlistAgent)
fmt.Println("c1")
c := memberlist.DefaultLocalConfig()
fmt.Println("c3")
c.Name = opdata.Name()
fmt.Println("c4")
c.BindAddr = opdata.Ovip
c.BindPort = opdata.Serfport
c.Events = observer
fmt.Println("c5")
list, err := memberlist.Create(c)
fmt.Println("c6")
if err != nil {
panic("Failed to create memberlist: " + err.Error())
}
ma.list = list
ma.conf = c
fmt.Println("MMBL created")
return ma
}
示例11: NewAgent
func NewAgent(c *Config) (*Agent, error) {
var err error
mlConfig := memberlist.DefaultLANConfig()
mlConfig.Name = c.Name
mlConfig.BindAddr, mlConfig.BindPort, err = parseHostPort(c.BindAddr)
if err != nil {
return nil, err
}
if c.AdvertiseAddr != "" {
mlConfig.AdvertiseAddr, mlConfig.AdvertisePort, err = parseHostPort(c.AdvertiseAddr)
if err != nil {
return nil, err
}
} else {
mlConfig.AdvertiseAddr = mlConfig.BindAddr
mlConfig.AdvertisePort = mlConfig.BindPort
}
agent := &Agent{}
agent.config = mlConfig
mlConfig.Delegate = &Delegate{agent}
ml, err := memberlist.Create(mlConfig)
if err != nil {
log.Fatalf("create memberlist: %s", err.Error())
}
agent.memberlist = ml
agent.broadcasts = &memberlist.TransmitLimitedQueue{
NumNodes: func() int {
return agent.memberlist.NumMembers()
},
RetransmitMult: mlConfig.RetransmitMult,
}
return agent, nil
}
示例12: Setup
// Setup creates a new instance of memberlist, assigns it to list, and
// sets the local nodes meta data as the rpc address.
func (m *Memberlist) Setup(t *Toystore) {
memberConfig := memberlist.DefaultLocalConfig()
memberConfig.BindAddr = t.Host
memberConfig.Name = t.Host
// Set IndirectChecks to 0 so we see a local view of membership.
// I.e. we don't care about nodes hidden by partitions.
memberConfig.IndirectChecks = 0
// This is set really low for testing purposes. Should be ~100ms.
memberConfig.GossipInterval = time.Millisecond * 20
// Sets delegate to handle membership change events.
memberConfig.Events = &MemberlistEvents{t}
list, err := memberlist.Create(memberConfig)
if err != nil {
panic(err)
}
m.list = list
n := m.list.LocalNode()
n.Meta = []byte(t.rpcAddress())
if err != nil {
panic("Failed to create memberlist: " + err.Error())
}
}
示例13: New
func New(cfg *Config) (*tribe, error) {
cfg.MemberlistConfig.Name = cfg.Name
cfg.MemberlistConfig.BindAddr = cfg.BindAddr
cfg.MemberlistConfig.BindPort = cfg.BindPort
logger := logger.WithFields(log.Fields{
"port": cfg.MemberlistConfig.BindPort,
"addr": cfg.MemberlistConfig.BindAddr,
"name": cfg.MemberlistConfig.Name,
})
tribe := &tribe{
agreements: map[string]*agreement.Agreement{},
members: map[string]*agreement.Member{},
taskStateResponses: map[string]*taskStateQueryResponse{},
taskStartStopCache: newCache(),
msgBuffer: make([]msg, 512),
intentBuffer: []msg{},
logger: logger.WithField("_name", cfg.MemberlistConfig.Name),
tags: map[string]string{
agreement.RestPort: strconv.Itoa(cfg.RestAPIPort),
agreement.RestProtocol: cfg.RestAPIProto,
agreement.RestInsecureSkipVerify: cfg.RestAPIInsecureSkipVerify,
},
pluginWorkQueue: make(chan worker.PluginRequest, 999),
taskWorkQueue: make(chan worker.TaskRequest, 999),
workerQuitChan: make(chan struct{}),
workerWaitGroup: &sync.WaitGroup{},
config: cfg,
EventManager: gomit.NewEventController(),
}
tribe.broadcasts = &memberlist.TransmitLimitedQueue{
NumNodes: func() int {
return len(tribe.memberlist.Members())
},
RetransmitMult: memberlist.DefaultLANConfig().RetransmitMult,
}
//configure delegates
cfg.MemberlistConfig.Delegate = &delegate{tribe: tribe}
cfg.MemberlistConfig.Events = &memberDelegate{tribe: tribe}
ml, err := memberlist.Create(cfg.MemberlistConfig)
if err != nil {
logger.Error(err)
return nil, err
}
tribe.memberlist = ml
if cfg.Seed != "" {
_, err := ml.Join([]string{cfg.Seed})
if err != nil {
logger.WithFields(log.Fields{
"seed": cfg.Seed,
}).Error(errMemberlistJoin)
return nil, errMemberlistJoin
}
logger.WithFields(log.Fields{
"seed": cfg.Seed,
}).Infoln("tribe started")
return tribe, nil
}
logger.WithFields(log.Fields{
"seed": "none",
}).Infoln("tribe started")
return tribe, nil
}
示例14: Create
// Create creates a new Serf instance, starting all the background tasks
// to maintain cluster membership information.
//
// After calling this function, the configuration should no longer be used
// or modified by the caller.
func Create(conf *Config) (*Serf, error) {
conf.Init()
if conf.ProtocolVersion < ProtocolVersionMin {
return nil, fmt.Errorf("Protocol version '%d' too low. Must be in range: [%d, %d]",
conf.ProtocolVersion, ProtocolVersionMin, ProtocolVersionMax)
} else if conf.ProtocolVersion > ProtocolVersionMax {
return nil, fmt.Errorf("Protocol version '%d' too high. Must be in range: [%d, %d]",
conf.ProtocolVersion, ProtocolVersionMin, ProtocolVersionMax)
}
serf := &Serf{
config: conf,
logger: log.New(conf.LogOutput, "", log.LstdFlags),
members: make(map[string]*memberState),
queryResponse: make(map[LamportTime]*QueryResponse),
shutdownCh: make(chan struct{}),
state: SerfAlive,
}
// Check that the meta data length is okay
if len(serf.encodeTags(conf.Tags)) > memberlist.MetaMaxSize {
return nil, fmt.Errorf("Encoded length of tags exceeds limit of %d bytes", memberlist.MetaMaxSize)
}
// Check if serf member event coalescing is enabled
if conf.CoalescePeriod > 0 && conf.QuiescentPeriod > 0 && conf.EventCh != nil {
c := &memberEventCoalescer{
lastEvents: make(map[string]EventType),
latestEvents: make(map[string]coalesceEvent),
}
conf.EventCh = coalescedEventCh(conf.EventCh, serf.shutdownCh,
conf.CoalescePeriod, conf.QuiescentPeriod, c)
}
// Check if user event coalescing is enabled
if conf.UserCoalescePeriod > 0 && conf.UserQuiescentPeriod > 0 && conf.EventCh != nil {
c := &userEventCoalescer{
events: make(map[string]*latestUserEvents),
}
conf.EventCh = coalescedEventCh(conf.EventCh, serf.shutdownCh,
conf.UserCoalescePeriod, conf.UserQuiescentPeriod, c)
}
// Listen for internal Serf queries. This is setup before the snapshotter, since
// we want to capture the query-time, but the internal listener does not passthrough
// the queries
outCh, err := newSerfQueries(serf, serf.logger, conf.EventCh, serf.shutdownCh)
if err != nil {
return nil, fmt.Errorf("Failed to setup serf query handler: %v", err)
}
conf.EventCh = outCh
// Try access the snapshot
var oldClock, oldEventClock, oldQueryClock LamportTime
var prev []*PreviousNode
if conf.SnapshotPath != "" {
eventCh, snap, err := NewSnapshotter(conf.SnapshotPath,
snapshotSizeLimit,
conf.RejoinAfterLeave,
serf.logger,
&serf.clock,
conf.EventCh,
serf.shutdownCh)
if err != nil {
return nil, fmt.Errorf("Failed to setup snapshot: %v", err)
}
serf.snapshotter = snap
conf.EventCh = eventCh
prev = snap.AliveNodes()
oldClock = snap.LastClock()
oldEventClock = snap.LastEventClock()
oldQueryClock = snap.LastQueryClock()
serf.eventMinTime = oldEventClock + 1
serf.queryMinTime = oldQueryClock + 1
}
// Setup the various broadcast queues, which we use to send our own
// custom broadcasts along the gossip channel.
serf.broadcasts = &memberlist.TransmitLimitedQueue{
NumNodes: func() int {
return len(serf.members)
},
RetransmitMult: conf.MemberlistConfig.RetransmitMult,
}
serf.eventBroadcasts = &memberlist.TransmitLimitedQueue{
NumNodes: func() int {
return len(serf.members)
},
RetransmitMult: conf.MemberlistConfig.RetransmitMult,
}
serf.queryBroadcasts = &memberlist.TransmitLimitedQueue{
NumNodes: func() int {
return len(serf.members)
//.........這裏部分代碼省略.........
示例15: benchmarkCluster
func benchmarkCluster(b *testing.B, num int, timeoutForAllJoins time.Duration, lowestPort int) {
log.WithField("num", b.N).Fatal("Unexpected error when creating the memberlist")
startTime := time.Now()
var nodes []*memberlist.Memberlist
eventC := make(chan memberlist.NodeEvent, num)
addr := "127.0.0.1"
var firstMemberName string
for i := 0; i < num; i++ {
c := memberlist.DefaultLANConfig()
port := lowestPort + i
c.Name = fmt.Sprintf("%s:%d", addr, port)
c.BindAddr = addr
c.BindPort = port
c.ProbeInterval = 20 * time.Millisecond
c.ProbeTimeout = 100 * time.Millisecond
c.GossipInterval = 20 * time.Millisecond
c.PushPullInterval = 200 * time.Millisecond
c.LogOutput = ioutil.Discard
if i == 0 {
c.Events = &memberlist.ChannelEventDelegate{eventC}
firstMemberName = c.Name
}
newMember, err := memberlist.Create(c)
if err != nil {
log.WithField("error", err).Fatal("Unexpected error when creating the memberlist")
}
nodes = append(nodes, newMember)
defer newMember.Shutdown()
if i >= 0 {
num, err := newMember.Join([]string{firstMemberName})
if num == 0 || err != nil {
log.WithField("error", err).Fatal("Unexpected fatal error when node wanted to join the cluster")
}
}
}
breakTimer := time.After(timeoutForAllJoins)
numJoins := 0
WAIT:
for {
select {
case e := <-eventC:
l := log.WithFields(log.Fields{
"node": *e.Node,
"numJoins": numJoins,
"numMembers": nodes[0].NumMembers(),
})
if e.Event == memberlist.NodeJoin {
l.Info("Node join")
numJoins++
if numJoins == num {
l.Info("All nodes joined")
break WAIT
}
} else {
l.Info("Node leave")
}
case <-breakTimer:
break WAIT
}
}
if numJoins != num {
log.WithFields(log.Fields{
"joinCounter": numJoins,
"num": num,
}).Error("Timeout before completing all joins")
}
convergence := false
for !convergence {
convergence = true
for idx, node := range nodes {
numSeenByNode := node.NumMembers()
if numSeenByNode != num {
log.WithFields(log.Fields{
"index": idx,
"expected": num,
"actual": numSeenByNode,
}).Debug("Wrong number of nodes")
convergence = false
break
}
}
}
endTime := time.Now()
if numJoins == num {
log.WithField("durationSeconds", endTime.Sub(startTime).Seconds()).Info("Cluster convergence reached")
}
b.StartTimer()
for senderID, node := range nodes {
for receiverID, member := range node.Members() {
//.........這裏部分代碼省略.........