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 #144 from 0xProject/marcin/cleanups
Browse files Browse the repository at this point in the history
Hello http.TimeoutHandler
  • Loading branch information
eitu5ami authored Dec 15, 2023
2 parents 6ed6c54 + 5851c39 commit 552da58
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
16 changes: 13 additions & 3 deletions internal/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func NewProxy(proxyConfig Config, healthCheckManager *HealthcheckManager) *Proxy
}

func (p *Proxy) AddTarget(target TargetConfig) error {
proxy, err := NewReverseProxy(target, p.config)
proxy, err := NewReverseProxy(target)
if err != nil {
return err
}
Expand Down Expand Up @@ -113,6 +113,16 @@ func (p *Proxy) copyHeaders(dst http.ResponseWriter, src http.ResponseWriter) {
}
}

func (p *Proxy) timeoutHandler(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
http.TimeoutHandler(next,
p.config.Proxy.UpstreamTimeout,
http.StatusText(http.StatusGatewayTimeout)).ServeHTTP(w, r)
}

return http.HandlerFunc(fn)
}

func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
body := &bytes.Buffer{}

Expand All @@ -127,9 +137,9 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
r.Body = io.NopCloser(bytes.NewBuffer(body.Bytes()))

if !target.Config.Connection.HTTP.Compression && strings.Contains(r.Header.Get(headers.ContentEncoding), "gzip") {
middleware.Gunzip(target.Proxy).ServeHTTP(pw, r)
p.timeoutHandler(middleware.Gunzip(target.Proxy)).ServeHTTP(pw, r)
} else {
target.Proxy.ServeHTTP(pw, r)
p.timeoutHandler(target.Proxy).ServeHTTP(pw, r)
}

if p.HasNodeProviderFailed(pw.statusCode) {
Expand Down
3 changes: 2 additions & 1 deletion internal/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http/httptest"
"strconv"
"testing"
"time"

"github.com/go-http-utils/headers"
"github.com/prometheus/client_golang/prometheus"
Expand All @@ -17,7 +18,7 @@ import (
func createConfig() Config {
return Config{
Proxy: ProxyConfig{
UpstreamTimeout: 0,
UpstreamTimeout: time.Second * 3,
},
HealthChecks: HealthCheckConfig{
Interval: 0,
Expand Down
3 changes: 1 addition & 2 deletions internal/proxy/reverseproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/pkg/errors"
)

func NewReverseProxy(targetConfig TargetConfig, config Config) (*httputil.ReverseProxy, error) {
func NewReverseProxy(targetConfig TargetConfig) (*httputil.ReverseProxy, error) {
target, err := url.Parse(targetConfig.Connection.HTTP.URL)
if err != nil {
return nil, errors.Wrap(err, "cannot parse url")
Expand Down Expand Up @@ -42,7 +42,6 @@ func NewReverseProxy(targetConfig TargetConfig, config Config) (*httputil.Revers
IdleConnTimeout: 30 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
ResponseHeaderTimeout: config.Proxy.UpstreamTimeout,
}

conntrack.PreRegisterDialerMetrics(targetConfig.Name)
Expand Down

0 comments on commit 552da58

Please sign in to comment.