-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
48 lines (42 loc) · 1005 Bytes
/
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
package main
import (
"fmt"
"log"
"net/http"
)
var (
serverList = []*server{
newServer("http://127.0.0.1:5001"),
newServer("http://127.0.0.1:5002"),
newServer("http://127.0.0.1:5003"),
newServer("http://127.0.0.1:5004"),
newServer("http://127.0.0.1:5005"),
}
lastServedIndex = 0
)
func main() {
http.HandleFunc("/",forwardRequest)
log.Fatal(http.ListenAndServe(":8888", nil))
}
func forwardRequest(res http.ResponseWriter, req *http.Request){
server, err := getActiveServer()
if err != nil {
fmt.Fprintf(res, "Couldn't process the request: %s", err.Error())
}
server.ReverseProxy.ServeHTTP(res, req)
}
func getActiveServer() (*server, error){
for i:=0; i<len(serverList); i++ {
server := getServer()
if server.isActive {
return server, nil
}
}
return nil, fmt.Errorf("no active servers remaining")
}
func getServer() *server {
nextIndex := (lastServedIndex+1)%len(serverList)
server := serverList[lastServedIndex]
lastServedIndex = nextIndex
return server
}