本文整理匯總了Golang中github.com/docker/swarm/cluster.Cluster類的典型用法代碼示例。如果您正苦於以下問題:Golang Cluster類的具體用法?Golang Cluster怎麽用?Golang Cluster使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Cluster類的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: ListenAndServe
func ListenAndServe(c *cluster.Cluster, s *scheduler.Scheduler, addr, version string, enableCors bool, tlsConfig *tls.Config) error {
context := &context{
cluster: c,
scheduler: s,
version: version,
eventsHandler: NewEventsHandler(),
}
c.Events(context.eventsHandler)
r, err := createRouter(context, enableCors)
if err != nil {
return err
}
server := &http.Server{
Addr: addr,
Handler: r,
}
l, err := net.Listen("tcp", addr)
if err != nil {
return err
}
if tlsConfig != nil {
tlsConfig.NextProtos = []string{"http/1.1"}
l = tls.NewListener(l, tlsConfig)
}
return server.Serve(l)
}
示例2: ListenAndServe
func ListenAndServe(c *cluster.Cluster, s *scheduler.Scheduler, hosts []string, enableCors bool, tlsConfig *tls.Config) error {
context := &context{
cluster: c,
scheduler: s,
eventsHandler: NewEventsHandler(),
tlsConfig: tlsConfig,
}
c.Events(context.eventsHandler)
r := createRouter(context, enableCors)
chErrors := make(chan error, len(hosts))
for _, host := range hosts {
protoAddrParts := strings.SplitN(host, "://", 2)
if len(protoAddrParts) == 1 {
protoAddrParts = append([]string{"tcp"}, protoAddrParts...)
}
go func() {
log.WithFields(log.Fields{"proto": protoAddrParts[0], "addr": protoAddrParts[1]}).Info("Listening for HTTP")
var (
l net.Listener
err error
server = &http.Server{
Addr: protoAddrParts[1],
Handler: r,
}
)
switch protoAddrParts[0] {
case "unix":
l, err = newUnixListener(protoAddrParts[1], tlsConfig)
case "tcp":
l, err = newListener("tcp", protoAddrParts[1], tlsConfig)
default:
err = fmt.Errorf("unsupported protocol: %q", protoAddrParts[0])
}
if err != nil {
chErrors <- err
} else {
chErrors <- server.Serve(l)
}
}()
}
for i := 0; i < len(hosts); i++ {
err := <-chErrors
if err != nil {
return err
}
}
return nil
}
示例3: NewPrimary
// NewPrimary creates a new API router.
func NewPrimary(cluster cluster.Cluster, tlsConfig *tls.Config, status StatusHandler, enableCors bool) *mux.Router {
// Register the API events handler in the cluster.
eventsHandler := newEventsHandler()
cluster.RegisterEventHandler(eventsHandler)
context := &context{
cluster: cluster,
eventsHandler: eventsHandler,
statusHandler: status,
tlsConfig: tlsConfig,
}
r := mux.NewRouter()
for method, mappings := range routes {
for route, fct := range mappings {
log.WithFields(log.Fields{"method": method, "route": route}).Debug("Registering HTTP route")
localRoute := route
localFct := fct
wrap := func(w http.ResponseWriter, r *http.Request) {
log.WithFields(log.Fields{"method": r.Method, "uri": r.RequestURI}).Debug("HTTP request received")
if enableCors {
writeCorsHeaders(w, r)
}
localFct(context, w, r)
}
localMethod := method
r.Path("/v{version:[0-9.]+}" + localRoute).Methods(localMethod).HandlerFunc(wrap)
r.Path(localRoute).Methods(localMethod).HandlerFunc(wrap)
}
}
return r
}
示例4: ListenAndServe
func ListenAndServe(c *cluster.Cluster, s *scheduler.Scheduler, addr, version string, enableCors bool) error {
context := &context{
cluster: c,
scheduler: s,
version: version,
eventsHandler: NewEventsHandler(),
}
c.Events(context.eventsHandler)
r, err := createRouter(context, enableCors)
if err != nil {
return err
}
server := &http.Server{
Addr: addr,
Handler: r,
}
return server.ListenAndServe()
}
示例5: 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
}
}
}
示例6: NewPrimary
// NewPrimary creates a new API router.
func NewPrimary(cluster cluster.Cluster, tlsConfig *tls.Config, status StatusHandler, debug, enableCors bool) *mux.Router {
// Register the API events handler in the cluster.
eventsHandler := newEventsHandler()
cluster.RegisterEventHandler(eventsHandler)
context := &context{
cluster: cluster,
eventsHandler: eventsHandler,
statusHandler: status,
tlsConfig: tlsConfig,
}
r := mux.NewRouter()
setupPrimaryRouter(r, context, enableCors)
if debug {
profilerSetup(r, "/debug/")
}
return r
}