-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathhttp.go
40 lines (35 loc) · 1018 Bytes
/
http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
import (
"log"
"net/http"
"net/http/httputil"
BA "github.com/darkhelmet/balance/backends"
"github.com/gonuts/commander"
)
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))
}},
}
http.DefaultTransport.(*http.Transport).MaxIdleConnsPerHost = 100
log.Printf("listening on %s, balancing %d backends", bind, backends.Len())
return http.ListenAndServe(bind, proxy)
}
func init() {
fs := newFlagSet("http")
cmd.Subcommands = append(cmd.Subcommands, &commander.Command{
UsageLine: "http [options] [<backends>]",
Short: "performs http based load balancing",
Flag: *fs,
Run: balancer(httpBalance),
})
}