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

Commit

Permalink
Inaccurate names
Browse files Browse the repository at this point in the history
  • Loading branch information
eitu5ami committed Dec 14, 2023
1 parent 2570bd5 commit 5d72949
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
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
File renamed without changes.

0 comments on commit 5d72949

Please sign in to comment.