Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[minor] Add dial timeout option #20

Merged
merged 3 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ type Log struct {

// Transport exposes a subset of Transport parameters. reference: https://github.com/golang/go/blob/master/src/net/http/transport.go#L95
type Transport struct {
DialContext DialContext `yaml:"dialContext,omitempty"`
TLSHandshakeTimeout time.Duration `yaml:"tlsHandshakeTimeout,omitempty"`
DisableKeepAlives bool `yaml:"disableKeepAlives,omitempty"`
DisableCompression bool `yaml:"disableCompression,omitempty"`
Expand All @@ -302,6 +303,11 @@ type Transport struct {
ForceAttemptHTTP2 bool `yaml:"forceAttemptHTTP2,omitempty"`
}

// DialContext exposes a subset of DialContext parameters. reference: https://github.com/golang/go/blob/master/src/net/http/transport.go#L318
type DialContext struct {
Timeout time.Duration `yaml:"timeout"`
}

// OriginLog represents log configuration from origin
type OriginLog struct {
StatusCode StatusCode `yaml:"statusCode"`
Expand Down
3 changes: 3 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ func TestNew(t *testing.T) {
WriteBufferSize: 0,
ReadBufferSize: 0,
ForceAttemptHTTP2: true,
DialContext: DialContext{
Timeout: 1 * time.Second,
},
},
OriginLog: OriginLog{
StatusCode: StatusCode{
Expand Down
12 changes: 11 additions & 1 deletion handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httputil"
"strings"
Expand Down Expand Up @@ -88,14 +89,23 @@ func New(cfg config.Proxy, bp httputil.BufferPool, prov service.Authorizationd)
ModifyResponse: modifyResponse,
Transport: &transport{
prov: prov,
RoundTripper: transportFromCfg(cfg.Transport),
RoundTripper: updateDialContext(transportFromCfg(cfg.Transport), cfg.Transport.DialContext.Timeout),
cfg: cfg,
noAuthPaths: mapPathToAssertion(cfg.NoAuthPaths),
},
ErrorHandler: handleError,
}
}

func updateDialContext(t *http.Transport, dialTimeout time.Duration) *http.Transport {
if dialTimeout != time.Duration(0) {
thgm3116 marked this conversation as resolved.
Show resolved Hide resolved
t.DialContext = (&net.Dialer{
Timeout: dialTimeout,
}).DialContext
}
return t
}

func transportFromCfg(cfg config.Transport) *http.Transport {
isZero := func(v interface{}) bool {
switch v.(type) {
Expand Down
57 changes: 57 additions & 0 deletions handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"context"
"encoding/json"
"fmt"
"math"
"net"
"net/http"
"net/http/httptest"
"net/http/httputil"
Expand Down Expand Up @@ -565,6 +567,61 @@ func TestNew(t *testing.T) {
}
}

func Test_updateDialContext(t *testing.T) {
type args struct {
cfg *http.Transport
dialTimeout time.Duration
}
tests := []struct {
name string
args args
want *http.Transport
}{
{
name: "check dialContext.timeout == 0 is not used",
args: args{
cfg: &http.Transport{},
dialTimeout: 0,
},
want: &http.Transport{},
},
{
name: "check dialContext.timeout != 0 is used",
args: args{
cfg: &http.Transport{},
dialTimeout: 10 * time.Second,
},
want: &http.Transport{
DialContext: (&net.Dialer{
Timeout: 10 * time.Second,
}).DialContext,
},
},
{
name: "check if dialContext.timeout is negative, timeout is math.MaxInt64",
args: args{
cfg: &http.Transport{},
dialTimeout: -1,
},
want: &http.Transport{
DialContext: (&net.Dialer{
Timeout: math.MaxInt64,
}).DialContext,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := updateDialContext(tt.args.cfg, tt.args.dialTimeout)
p1 := reflect.ValueOf(got.DialContext).Pointer()
p2 := reflect.ValueOf(tt.want.DialContext).Pointer()
if p1 != p2 {
t.Errorf("updateDialContext() = %+v, want %+v", p1, p2)
}
})
}
}

func Test_transportFromCfg(t *testing.T) {
type args struct {
cfg config.Transport
Expand Down
2 changes: 2 additions & 0 deletions test/data/example_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ proxy:
writeBufferSize: 0
readBufferSize: 0
forceAttemptHTTP2: true
dialContext:
timeout: "1s"
originLog:
statusCode:
enable: true
Expand Down