本文整理汇总了Golang中github.com/darkhelmet/balance/backends.Backends.Len方法的典型用法代码示例。如果您正苦于以下问题:Golang Backends.Len方法的具体用法?Golang Backends.Len怎么用?Golang Backends.Len使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/darkhelmet/balance/backends.Backends
的用法示例。
在下文中一共展示了Backends.Len方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: httpsBalance
func httpsBalance(bind string, backends BA.Backends) {
if httpsOptions.certFile == "" || httpsOptions.keyFile == "" {
log.Fatalln("specify both -cert and -key")
}
log.Println("using https balancing")
proxy := &Proxy{
&httputil.ReverseProxy{Director: func(req *http.Request) {
backend := backends.Choose()
if backend == nil {
log.Printf("no backend for client %s", req.RemoteAddr)
panic(NoBackend{})
}
req.URL.Scheme = "http"
req.Header.Add("X-Forwarded-Proto", "https")
req.URL.Host = backend.String()
req.Header.Add(XRealIP, RealIP(req))
}},
}
log.Printf("listening on %s, balancing %d backends", bind, backends.Len())
err := http.ListenAndServeTLS(bind, httpsOptions.certFile, httpsOptions.keyFile, proxy)
if err != nil {
log.Fatalf("failed to bind: %s", err)
}
}
示例2: httpBalance
func httpBalance(bind string, backends BA.Backends) {
log.Println("using http balancing")
proxy := &httputil.ReverseProxy{Director: func(req *http.Request) {
req.URL.Host = backends.Choose()
}}
log.Printf("listening on %s, balancing %d backends", bind, backends.Len())
err := http.ListenAndServe(bind, proxy)
if err != nil {
log.Fatalf("failed to bind: %s", err)
}
}
示例3: httpBalance
func httpBalance(bind string, backends BA.Backends) error {
log.Println("using http balancing")
proxy := &Proxy{
&httputil.ReverseProxy{Director: func(req *http.Request) {
backend := backends.Choose()
if backend == nil {
log.Printf("no backend for client %s", req.RemoteAddr)
panic(NoBackend{})
}
req.URL.Scheme = "http"
req.URL.Host = backend.String()
req.Header.Add(XRealIP, RealIP(req))
}},
}
log.Printf("listening on %s, balancing %d backends", bind, backends.Len())
return http.ListenAndServe(bind, proxy)
}
示例4: tcpBalance
func tcpBalance(bind string, backends BA.Backends) {
log.Println("using tcp balancing")
ln, err := net.Listen("tcp", bind)
if err != nil {
log.Fatalf("failed to bind: %s", err)
}
log.Printf("listening on %s, balancing %d backends", bind, backends.Len())
for {
conn, err := ln.Accept()
if err != nil {
log.Printf("failed to accept: %s", err)
continue
}
go handleConnection(conn, backends.Choose())
}
}