This repository was archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathproxy.go
More file actions
135 lines (111 loc) · 3.09 KB
/
proxy.go
File metadata and controls
135 lines (111 loc) · 3.09 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
package proxy
import (
"bytes"
"io"
"net/http"
"net/http/httputil"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
type HTTPTarget struct {
Config TargetConfig
Proxy *httputil.ReverseProxy
}
type Proxy struct {
config Config
targets []*HTTPTarget
healthcheckManager *HealthcheckManager
metricResponseTime *prometheus.HistogramVec
metricRequestErrors *prometheus.CounterVec
metricResponseStatus *prometheus.CounterVec
}
func NewProxy(proxyConfig Config, healthCheckManager *HealthcheckManager) *Proxy {
proxy := &Proxy{
config: proxyConfig,
healthcheckManager: healthCheckManager,
metricResponseTime: promauto.NewHistogramVec(
prometheus.HistogramOpts{
Name: "zeroex_rpc_gateway_request_duration_seconds",
Help: "Histogram of response time for Gateway in seconds",
Buckets: []float64{
.025,
.05,
.1,
.25,
.5,
1,
2.5,
5,
10,
15,
20,
25,
30,
},
}, []string{
"provider",
"method",
}),
metricRequestErrors: promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "zeroex_rpc_gateway_request_errors_handled_total",
Help: "The total number of request errors handled by gateway",
}, []string{
"provider",
"type",
}),
metricResponseStatus: promauto.NewCounterVec(prometheus.CounterOpts{
Name: "zeroex_rpc_gateway_target_response_status_total",
Help: "Total number of responses with a statuscode label",
}, []string{
"provider",
"status_code",
}),
}
for _, target := range proxy.config.Targets {
if err := proxy.AddTarget(target); err != nil {
panic(err)
}
}
return proxy
}
func (h *Proxy) AddTarget(target TargetConfig) error {
proxy, err := NewReverseProxy(target, h.config)
if err != nil {
return err
}
proxy.BufferPool = newBufferPool()
h.targets = append(
h.targets,
&HTTPTarget{
Config: target,
Proxy: proxy,
})
return nil
}
func (h *Proxy) HasNodeProviderFailed(statusCode int) bool {
return statusCode >= http.StatusInternalServerError || statusCode == http.StatusTooManyRequests
}
func (h *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
body := &bytes.Buffer{}
if _, err := io.Copy(body, r.Body); err != nil {
http.Error(w, http.StatusText(http.StatusServiceUnavailable), http.StatusServiceUnavailable)
}
for _, target := range h.targets {
start := time.Now()
pw := NewResponseWriter()
r.Body = io.NopCloser(bytes.NewBuffer(body.Bytes()))
target.Proxy.ServeHTTP(pw, r)
if h.HasNodeProviderFailed(pw.statusCode) {
h.metricResponseTime.WithLabelValues(target.Config.Name, r.Method).Observe(time.Since(start).Seconds())
h.metricRequestErrors.WithLabelValues(target.Config.Name, "rerouted").Inc()
continue
}
w.WriteHeader(pw.statusCode)
w.Write(pw.body.Bytes()) // nolint:errcheck
h.metricResponseTime.WithLabelValues(target.Config.Name, r.Method).Observe(time.Since(start).Seconds())
return
}
http.Error(w, http.StatusText(http.StatusServiceUnavailable), http.StatusServiceUnavailable)
}