-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_balancer.go
95 lines (77 loc) · 1.85 KB
/
load_balancer.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
package main
import (
"bytes"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"sync"
)
type loadBalancer struct {
mu sync.Mutex
idx int
cfg *Config
job_registry map[string]int
}
func (l *loadBalancer) lbHandler(w http.ResponseWriter, r *http.Request) {
var targetURL *url.URL
var currentBackend *Backend
var current_id int
if r.Method == "POST" {
l.mu.Lock()
for {
current_id = l.idx % len(l.cfg.Backends)
l.idx++
currentBackend = &l.cfg.Backends[current_id]
if currentBackend.isAlive() {
break
} else {
log.Printf("Backend %s is not alive", currentBackend.URL)
currentBackend.SetDead(true)
}
}
targetURL = l.get_target_url(current_id)
l.mu.Unlock()
} else {
status := status_from_url(r.URL)
job_id := l.job_registry[status.job_id]
targetURL = l.get_target_url(job_id)
}
reverseProxy := httputil.NewSingleHostReverseProxy(targetURL)
if r.Method == "POST" {
reverseProxy.ModifyResponse = func(resp *http.Response) (err error) {
b, _ := ioutil.ReadAll(resp.Body)
body := parse_body(b)
l.job_registry[body.ID] = current_id
resp.Body = ioutil.NopCloser(bytes.NewReader(b))
return nil
}
}
reverseProxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, e error) {
log.Printf("%v is dead.", targetURL)
currentBackend.SetDead(true)
l.lbHandler(w, r)
}
reverseProxy.ServeHTTP(w, r)
}
func (l *loadBalancer) get_target_url(id int) *url.URL {
targetURL, err := url.Parse(l.cfg.Backends[id].URL)
if err != nil {
log.Fatal(err.Error())
}
return targetURL
}
type queryStatus struct {
status string
job_id string
}
func status_from_url(url *url.URL) queryStatus {
split := strings.Split(url.Path, "/")
// Path starts with / therefore increasing the array index by 1
return queryStatus{
status: split[3],
job_id: split[4],
}
}