本文整理匯總了Golang中github.com/docker/swarm/api.Server.SetHandler方法的典型用法代碼示例。如果您正苦於以下問題:Golang Server.SetHandler方法的具體用法?Golang Server.SetHandler怎麽用?Golang Server.SetHandler使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/docker/swarm/api.Server
的用法示例。
在下文中一共展示了Server.SetHandler方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: setupReplication
func setupReplication(c *cli.Context, cluster cluster.Cluster, server *api.Server, discovery discovery.Backend, addr string, leaderTTL time.Duration, tlsConfig *tls.Config) {
kvDiscovery, ok := discovery.(*kvdiscovery.Discovery)
if !ok {
log.Fatal("Leader election is only supported with consul, etcd and zookeeper discovery.")
}
client := kvDiscovery.Store()
p := path.Join(kvDiscovery.Prefix(), leaderElectionPath)
candidate := leadership.NewCandidate(client, p, addr, leaderTTL)
follower := leadership.NewFollower(client, p)
primary := api.NewPrimary(cluster, tlsConfig, &statusHandler{cluster, candidate, follower}, c.GlobalBool("debug"), c.Bool("cors"))
replica := api.NewReplica(primary, tlsConfig)
go func() {
for {
run(candidate, server, primary, replica)
time.Sleep(defaultRecoverTime)
}
}()
go func() {
for {
follow(follower, replica, addr)
time.Sleep(defaultRecoverTime)
}
}()
server.SetHandler(primary)
}
示例2: run
func run(candidate *leadership.Candidate, server *api.Server, primary *mux.Router, replica *api.Replica) {
electedCh, errCh := candidate.RunForElection()
for {
select {
case isElected := <-electedCh:
if isElected {
log.Info("Leader Election: Cluster leadership acquired")
server.SetHandler(primary)
} else {
log.Info("Leader Election: Cluster leadership lost")
server.SetHandler(replica)
}
case err := <-errCh:
log.Error(err)
return
}
}
}
示例3: run
func run(cl cluster.Cluster, candidate *leadership.Candidate, server *api.Server, primary *mux.Router, replica *api.Replica) {
electedCh, errCh := candidate.RunForElection()
var watchdog *cluster.Watchdog
for {
select {
case isElected := <-electedCh:
if isElected {
log.Info("Leader Election: Cluster leadership acquired")
watchdog = cluster.NewWatchdog(cl)
server.SetHandler(primary)
} else {
log.Info("Leader Election: Cluster leadership lost")
cl.UnregisterEventHandler(watchdog)
server.SetHandler(replica)
}
case err := <-errCh:
log.Error(err)
return
}
}
}
示例4: setupLeaderElection
func setupLeaderElection(server *api.Server, apiHandler http.Handler, discovery discovery.Discovery, addr string, tlsConfig *tls.Config) {
kvDiscovery, ok := discovery.(*kvdiscovery.Discovery)
if !ok {
log.Fatal("Leader election is only supported with consul, etcd and zookeeper discovery.")
}
client := kvDiscovery.Store()
candidate := leadership.NewCandidate(client, leaderElectionPath, addr)
follower := leadership.NewFollower(client, leaderElectionPath)
proxy := api.NewReverseProxy(tlsConfig)
go func() {
candidate.RunForElection()
electedCh := candidate.ElectedCh()
for isElected := range electedCh {
if isElected {
log.Info("Cluster leadership acquired")
server.SetHandler(apiHandler)
} else {
log.Info("Cluster leadership lost")
server.SetHandler(proxy)
}
}
}()
go func() {
follower.FollowElection()
leaderCh := follower.LeaderCh()
for leader := range leaderCh {
log.Infof("New leader elected: %s", leader)
if leader == addr {
proxy.SetDestination("")
} else {
proxy.SetDestination(leader)
}
}
}()
}
示例5: setupReplication
func setupReplication(c *cli.Context, cluster cluster.Cluster, server *api.Server, discovery discovery.Discovery, addr string, tlsConfig *tls.Config) {
kvDiscovery, ok := discovery.(*kvdiscovery.Discovery)
if !ok {
log.Fatal("Leader election is only supported with consul, etcd and zookeeper discovery.")
}
client := kvDiscovery.Store()
p := path.Join(kvDiscovery.Prefix(), leaderElectionPath)
candidate := leadership.NewCandidate(client, p, addr)
follower := leadership.NewFollower(client, p)
primary := api.NewPrimary(cluster, tlsConfig, &statusHandler{cluster, candidate, follower}, c.Bool("cors"))
replica := api.NewReplica(primary, tlsConfig)
go func() {
candidate.RunForElection()
electedCh := candidate.ElectedCh()
for isElected := range electedCh {
if isElected {
log.Info("Cluster leadership acquired")
server.SetHandler(primary)
} else {
log.Info("Cluster leadership lost")
server.SetHandler(replica)
}
}
}()
go func() {
follower.FollowElection()
leaderCh := follower.LeaderCh()
for leader := range leaderCh {
log.Infof("New leader elected: %s", leader)
if leader == addr {
replica.SetPrimary("")
} else {
replica.SetPrimary(leader)
}
}
}()
server.SetHandler(primary)
}