-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
103 lines (88 loc) · 2.16 KB
/
main.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"os"
)
type simpleServer struct {
addr string
proxy *httputil.ReverseProxy
}
type Server interface {
Address() string
IsAlive() bool
Serve(rw http.ResponseWriter, r *http.Request)
}
func newSimpleServer(addr string) *simpleServer {
serverURL, err := url.Parse(addr)
handleError(err)
return &simpleServer{
addr: addr,
proxy: httputil.NewSingleHostReverseProxy(serverURL),
}
}
type LoadBalancer struct {
port string
roundRobinIndex int
serverList []Server
}
func newLoadBalancer(port string, serverList []Server) *LoadBalancer {
return &LoadBalancer{
port: port,
roundRobinIndex: 0,
serverList: serverList,
}
}
func handleError(err error) {
if err != nil {
fmt.Printf("error: %v\n", err)
os.Exit(1)
}
}
func (s *simpleServer) Address() string {
return s.addr
}
func (s *simpleServer) IsAlive() bool {
// Implement a real health check here.
resp, err := http.Get(s.addr)
if err != nil || resp.StatusCode != http.StatusOK {
return false
}
return true
}
func (s *simpleServer) Serve(rw http.ResponseWriter, req *http.Request) {
fmt.Printf("Forwarding request to %s\n", s.addr)
s.proxy.ServeHTTP(rw, req)
}
func (lb *LoadBalancer) getNextAvailableServer() Server {
for {
server := lb.serverList[lb.roundRobinIndex%len(lb.serverList)]
lb.roundRobinIndex++
if server.IsAlive() {
fmt.Printf("Selected server: %s\n", server.Address())
return server
}
}
}
func (lb *LoadBalancer) serveProxy(rw http.ResponseWriter, req *http.Request) {
fmt.Printf("Received request: %s\n", req.URL.Path)
targetServer := lb.getNextAvailableServer()
targetServer.Serve(rw, req)
}
func main() {
serverList := []Server{
newSimpleServer("https://www.facebook.com/"),
newSimpleServer("https://www.bing.com/"),
newSimpleServer("https://www.duckduckgo.com/"),
}
lb := newLoadBalancer("8009", serverList)
handleRedirect := func(rw http.ResponseWriter, req *http.Request) {
lb.serveProxy(rw, req)
}
http.HandleFunc("/", handleRedirect)
fmt.Printf("Load Balancer started at :%s\n", lb.port)
err := http.ListenAndServe(":"+lb.port, nil)
handleError(err)
}