-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.go
More file actions
205 lines (175 loc) · 4.63 KB
/
Copy pathmodels.go
File metadata and controls
205 lines (175 loc) · 4.63 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import (
"context"
"fmt"
"log"
"net"
"net/http"
"net/http/httputil"
"net/url"
"sync"
"sync/atomic"
"time"
)
type Backend struct {
URL *url.URL
Alive bool
mux sync.RWMutex
ReverseProxy *httputil.ReverseProxy
}
func (b *Backend) CheckAlive() {
log.Print("LoadBalancer.ServerPool.Backend.CheckAlive() => Checking ", b.URL.Host)
var isAlive bool = true
timeout := 2 * time.Second
if conn, err := net.DialTimeout("tcp", b.URL.String(), timeout); err != nil {
log.Println("Site unreachable, error: ", err)
isAlive = false
} else {
isAlive = true
defer conn.Close()
}
b.SetAlive(isAlive)
}
func (b *Backend) SetAlive(alive bool) {
log.Printf("LoadBalancer.ServerPool.Backend.SetAlive() => %s [%v]\n", b.URL, alive)
b.mux.Lock()
b.Alive = alive
b.mux.Unlock()
}
func (b *Backend) IsAlive() (alive bool) {
b.mux.RLock()
alive = b.Alive
b.mux.RUnlock()
return
}
type ServerPool struct {
backends []*Backend
current uint64
}
func (s *ServerPool) AddBackend(backend *Backend) {
s.backends = append(s.backends, backend)
}
func (s *ServerPool) NextIndex() int {
index := 0
if len(s.backends) != 0 {
index = int(atomic.AddUint64(&s.current, uint64(1)) % uint64(len(s.backends)))
}
return index
}
func (s *ServerPool) MarkBackendStatus(backendUrl *url.URL, alive bool) {
for _, backend := range s.backends {
if backend.URL.String() == backendUrl.String() {
backend.SetAlive(alive)
break
}
}
}
func (s *ServerPool) GetNextPeer() *Backend {
next := s.NextIndex()
l := len(s.backends) + next
for i := next; i < l; i++ {
idx := i % len(s.backends)
if s.backends[idx].IsAlive() {
if i != next {
atomic.StoreUint64(&s.current, uint64(idx))
}
return s.backends[idx]
}
}
return nil
}
func (s *ServerPool) HealthCheck() {
for _, backend := range s.backends {
backend.CheckAlive()
}
}
type LoadBalancer struct {
serverPool ServerPool
}
func (lb *LoadBalancer) getAttemptsFromContext(req *http.Request) int {
attempts := 1
if val, ok := req.Context().Value(Attempts).(int); ok {
attempts = val
}
return attempts
}
func (lb *LoadBalancer) getRetryFromContext(req *http.Request) int {
retries := 0
if val, ok := req.Context().Value(Retry).(int); ok {
retries = val
}
return retries
}
func (lb *LoadBalancer) balance(writer http.ResponseWriter, req *http.Request) {
log.Print("Balancing...")
attempts := lb.getAttemptsFromContext(req)
if attempts > 3 {
log.Printf("%s(%s) Max attempts reached, terminating\n", req.RemoteAddr, req.URL.Path)
http.Error(writer, "Service not available", http.StatusServiceUnavailable)
return
}
peer := lb.serverPool.GetNextPeer()
if peer != nil {
log.Print("Serving: ", req)
peer.ReverseProxy.ServeHTTP(writer, req)
return
}
http.Error(writer, "Service not available", http.StatusServiceUnavailable)
}
func (lb *LoadBalancer) healthCheck() {
ticker := time.NewTicker(time.Minute * 2)
for {
select {
case <-ticker.C:
log.Println("Starting health check...")
lb.serverPool.HealthCheck()
}
}
}
func (lb *LoadBalancer) AddConnection(tok string) error {
log.Print("Adding Connection => ", tok)
serverUrl, err := url.Parse(tok)
if err != nil {
log.Printf("LoadBalancer.AddConnection() => token: %s, error: %s", tok, err)
return err
}
log.Print("Parsed url => ", serverUrl)
proxy := httputil.NewSingleHostReverseProxy(serverUrl)
proxy.ErrorHandler = func(writer http.ResponseWriter, request *http.Request, e error) {
log.Printf("[%s] %s\n", serverUrl.Host, e.Error())
retries := lb.getRetryFromContext(request)
if retries < 3 {
select {
case <-time.After(10 * time.Millisecond):
ctx := context.WithValue(request.Context(), Retry, retries+1)
proxy.ServeHTTP(writer, request.WithContext(ctx))
}
return
}
// after 3 retries, mark this backend as down
lb.serverPool.MarkBackendStatus(serverUrl, false)
// if the same request routing for few attempts with different backends, increase the count
attempts := lb.getAttemptsFromContext(request)
log.Printf("%s(%s) Attempting retry %d\n", request.RemoteAddr, request.URL.Path, attempts)
ctx := context.WithValue(request.Context(), Attempts, attempts+1)
lb.balance(writer, request.WithContext(ctx))
}
backend := &Backend{
URL: serverUrl,
Alive: true,
ReverseProxy: proxy,
}
backend.CheckAlive()
if backend.IsAlive() {
log.Printf("Configured server: %s\n", serverUrl)
lb.serverPool.AddBackend(backend)
} else {
return fmt.Errorf("LoadBalancer.AddConnection() => Unable to healthcheck server, token: %s", tok)
}
return nil
}
func NewLoadBalancer() *LoadBalancer {
return &LoadBalancer{
serverPool: ServerPool{},
}
}