-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathload_balancer.go
94 lines (87 loc) · 1.64 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
package boorufetch
import (
"fmt"
"io"
"log"
"net/http"
"time"
)
type request struct {
url string
res chan<- response
}
type response struct {
r io.ReadCloser
err error
}
type loadBalancer struct {
queue chan request
scheme, host string
}
func newLoadBalancer(scheme, host string) *loadBalancer {
l := &loadBalancer{
queue: make(chan request),
scheme: scheme,
host: host,
}
for i := 0; i < FetcherCount; i++ {
go func() {
for {
req := <-l.queue
// Retry in case of throttling
try:
for i := 0; i < 3; i++ {
r, err := http.Get(req.url)
res := response{
err: err,
}
if r != nil {
switch r.StatusCode {
// 404 returns a valid response and is handled by tbe
// caller
case 200, 201, 404:
res.r = r.Body
case 429, 500, 502, 503, 504:
if r.Body != nil {
r.Body.Close()
}
s := fmt.Sprintf(
`GET %s returned status code %d; try %d/3`,
req.url,
r.StatusCode,
i+1,
)
if i != 3 {
s += `; retrying in 10s`
}
log.Printf(s)
time.Sleep(time.Second * 10)
continue try
default:
if r.Body != nil {
r.Body.Close()
}
if res.err == nil {
res.err = fmt.Errorf(
`GET %s returned status code %d`,
req.url,
r.StatusCode,
)
}
}
}
req.res <- res
break try
}
}
}()
}
return l
}
// Fetch resource by URL
func (l *loadBalancer) Fetch(url string) (io.ReadCloser, error) {
ch := make(chan response)
l.queue <- request{url, ch}
r := <-ch
return r.r, r.err
}