-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
201 lines (189 loc) · 5.33 KB
/
proxy.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
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
package server
import (
"bytes"
"context"
"errors"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"net/http/httptest"
"net/http/httputil"
"net/url"
"sync"
"sync/atomic"
"time"
"go-common/library/log"
)
type BroadcastProxy struct {
backend []string
probePath string
reverseProxy []*httputil.ReverseProxy
bestClientIndex int32
probeSample int
wg sync.WaitGroup
ctx context.Context
cancel context.CancelFunc
}
func NewBroadcastProxy(backend []string, probePath string, maxIdleConns int, probeSample int) (*BroadcastProxy, error) {
if len(backend) == 0 {
return nil, errors.New("Require at least one backend")
}
proxy := new(BroadcastProxy)
if probeSample > 0 {
proxy.probeSample = probeSample
} else {
proxy.probeSample = 1
}
for _, addr := range backend {
proxy.backend = append(proxy.backend, addr)
proxy.probePath = probePath
p := httputil.NewSingleHostReverseProxy(&url.URL{
Scheme: "http",
Host: addr,
})
p.Transport = &http.Transport{
DisableKeepAlives: false,
MaxIdleConns: maxIdleConns,
MaxIdleConnsPerHost: maxIdleConns,
}
proxy.reverseProxy = append(proxy.reverseProxy, p)
}
proxy.ctx, proxy.cancel = context.WithCancel(context.Background())
proxy.wg.Add(1)
go func() {
defer proxy.wg.Done()
proxy.mainProbeProcess()
}()
return proxy, nil
}
func (proxy *BroadcastProxy) Close() {
proxy.cancel()
proxy.wg.Wait()
}
func (proxy *BroadcastProxy) HandleRequest(w http.ResponseWriter, r *http.Request) {
t1 := time.Now()
i := atomic.LoadInt32(&proxy.bestClientIndex)
proxy.reverseProxy[i].ServeHTTP(w, r)
t2 := time.Now()
log.V(3).Info("proxy process req:%s,backend id:%d, timecost:%s", r.RequestURI, i, t2.Sub(t1).String())
}
func (proxy *BroadcastProxy) RequestAllBackend(method, uri string, requestBody []byte) ([]string, []error) {
responseCollection := make([]string, len(proxy.reverseProxy))
errCollection := make([]error, len(proxy.reverseProxy))
var wg sync.WaitGroup
for i := range proxy.reverseProxy {
wg.Add(1)
go func(index int) {
defer wg.Done()
req, err := http.NewRequest(method, uri, bytes.NewReader(requestBody))
if err != nil {
errCollection[index] = err
return
}
httpRecorder := httptest.NewRecorder()
proxy.reverseProxy[index].ServeHTTP(httpRecorder, req)
resp := httpRecorder.Result()
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
errCollection[index] = errors.New(fmt.Sprintf("http response:%s", resp.Status))
return
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
errCollection[index] = err
return
}
responseCollection[index] = string(body)
}(i)
}
wg.Wait()
return responseCollection, errCollection
}
func (proxy *BroadcastProxy) mainProbeProcess() {
var wg sync.WaitGroup
interval := make([]time.Duration, len(proxy.backend))
for {
fast := -1
for i, probe := range proxy.reverseProxy {
wg.Add(1)
go func(p *httputil.ReverseProxy, index int) {
defer wg.Done()
interval[index] = proxy.unitProbeProcess(p, index)
}(probe, i)
}
wg.Wait()
for i, v := range interval {
if fast < 0 {
fast = i
} else {
if v < interval[fast] {
fast = i
}
}
}
atomic.StoreInt32(&proxy.bestClientIndex, int32(fast))
log.Info("[probe result]best server id:%d,addr:%s", fast, proxy.backend[fast])
for i, d := range interval {
log.Info("[probe log]server id:%d,addr:%s,avg time cost:%fms", i, proxy.backend[i], 1000*d.Seconds())
}
select {
case <-time.After(time.Second):
case <-proxy.ctx.Done():
return
}
}
}
func (proxy *BroadcastProxy) unitProbeProcess(p *httputil.ReverseProxy, backendIndex int) time.Duration {
var (
wg sync.WaitGroup
duration int64
)
for i := 0; i < proxy.probeSample; i++ {
wg.Add(1)
go func() {
defer wg.Done()
timeout := time.Second
<-time.After(time.Duration(rand.Intn(proxy.probeSample)) * time.Millisecond)
if timeCost, err := proxy.checkMonitorPing(p, backendIndex, timeout); err == nil {
atomic.AddInt64(&duration, timeCost.Nanoseconds())
} else {
atomic.AddInt64(&duration, timeout.Nanoseconds())
}
}()
}
wg.Wait()
return time.Duration(duration/int64(proxy.probeSample)) * time.Nanosecond
}
func (proxy *BroadcastProxy) checkMonitorPing(p *httputil.ReverseProxy, backendIndex int, timeout time.Duration) (time.Duration, error) {
req, err := http.NewRequest("GET", proxy.probePath, nil)
if err != nil {
return timeout, err
}
ctx, cancel := context.WithTimeout(proxy.ctx, timeout)
defer cancel()
req = req.WithContext(ctx)
recorder := httptest.NewRecorder()
beginTime := time.Now()
p.ServeHTTP(recorder, req)
resp := recorder.Result()
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Error("probe:server id:%d,addr:%s,send requset error:%s", backendIndex,
proxy.backend[backendIndex], resp.Status)
return timeout, errors.New("http response:" + resp.Status)
}
result, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Error("probe:server id:%d,addr:%s,read response error:%v", backendIndex,
proxy.backend[backendIndex], err)
return timeout, err
}
if !bytes.Equal(result, []byte("pong")) {
log.Error("probe:server id:%d,addr:%s,not match response:%s", backendIndex,
proxy.backend[backendIndex], result)
return timeout, err
}
endTime := time.Now()
return endTime.Sub(beginTime), nil
}