Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Merge pull request #140 from 0xProject/marcin/cleanups
Browse files Browse the repository at this point in the history
Clean ups
  • Loading branch information
eitu5ami authored Dec 14, 2023
2 parents 2570bd5 + bee4b10 commit f949bd6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
5 changes: 2 additions & 3 deletions internal/proxy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ type ProxyConfig struct { // nolint:revive
}

type TargetConnectionHTTP struct {
URL string `yaml:"url"`
Compression bool `yaml:"compression"`
DisableKeepAlives bool `yaml:"disableKeepAlives"`
URL string `yaml:"url"`
Compression bool `yaml:"compression"`
}

type TargetConfigConnection struct {
Expand Down
30 changes: 15 additions & 15 deletions internal/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ func NewProxy(proxyConfig Config, healthCheckManager *HealthcheckManager) *Proxy
return proxy
}

func (h *Proxy) AddTarget(target TargetConfig) error {
proxy, err := NewReverseProxy(target, h.config)
func (p *Proxy) AddTarget(target TargetConfig) error {
proxy, err := NewReverseProxy(target, p.config)
if err != nil {
return err
}

h.targets = append(
h.targets,
p.targets = append(
p.targets,
&HTTPTarget{
Config: target,
Proxy: proxy,
Expand All @@ -99,11 +99,11 @@ func (h *Proxy) AddTarget(target TargetConfig) error {
return nil
}

func (h *Proxy) HasNodeProviderFailed(statusCode int) bool {
func (p *Proxy) HasNodeProviderFailed(statusCode int) bool {
return statusCode >= http.StatusInternalServerError || statusCode == http.StatusTooManyRequests
}

func (h *Proxy) copyHeaders(dst http.ResponseWriter, src http.ResponseWriter) {
func (p *Proxy) copyHeaders(dst http.ResponseWriter, src http.ResponseWriter) {
for k, v := range src.Header() {
if len(v) == 0 {
continue
Expand All @@ -113,14 +113,14 @@ func (h *Proxy) copyHeaders(dst http.ResponseWriter, src http.ResponseWriter) {
}
}

func (h *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (p *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 {
for _, target := range p.targets {
start := time.Now()

pw := NewResponseWriter()
Expand All @@ -132,20 +132,20 @@ func (h *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
target.Proxy.ServeHTTP(pw, r)
}

if h.HasNodeProviderFailed(pw.statusCode) {
h.metricResponseTime.WithLabelValues(target.Config.Name, r.Method).Observe(time.Since(start).Seconds())
h.metricResponseStatus.WithLabelValues(target.Config.Name, strconv.Itoa(pw.statusCode)).Inc()
h.metricRequestErrors.WithLabelValues(target.Config.Name, "rerouted").Inc()
if p.HasNodeProviderFailed(pw.statusCode) {
p.metricResponseTime.WithLabelValues(target.Config.Name, r.Method).Observe(time.Since(start).Seconds())
p.metricResponseStatus.WithLabelValues(target.Config.Name, strconv.Itoa(pw.statusCode)).Inc()
p.metricRequestErrors.WithLabelValues(target.Config.Name, "rerouted").Inc()

continue
}
h.copyHeaders(w, pw)
p.copyHeaders(w, pw)

w.WriteHeader(pw.statusCode)
w.Write(pw.body.Bytes()) // nolint:errcheck

h.metricResponseStatus.WithLabelValues(target.Config.Name, strconv.Itoa(pw.statusCode)).Inc()
h.metricResponseTime.WithLabelValues(target.Config.Name, r.Method).Observe(time.Since(start).Seconds())
p.metricResponseStatus.WithLabelValues(target.Config.Name, strconv.Itoa(pw.statusCode)).Inc()
p.metricResponseTime.WithLabelValues(target.Config.Name, r.Method).Observe(time.Since(start).Seconds())

return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ func NewReverseProxy(targetConfig TargetConfig, config Config) (*httputil.Revers
Proxy: http.ProxyFromEnvironment,
DialContext: conntrackDialer,
ForceAttemptHTTP2: true,
DisableKeepAlives: targetConfig.Connection.HTTP.DisableKeepAlives,
MaxIdleConns: 100,
IdleConnTimeout: 30 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
Expand Down

0 comments on commit f949bd6

Please sign in to comment.