Skip to content

Commit

Permalink
[nightly] Add dial timeout option
Browse files Browse the repository at this point in the history
fix

Signed-off-by: taniwa <taniwa@yahoo-corp.jp>

add updateDialContext()
  • Loading branch information
taniwa committed May 16, 2023
1 parent 8c9d05b commit be55bca
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
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) {
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
46 changes: 45 additions & 1 deletion handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
"net"
"net/http"
"net/http/httptest"
"net/http/httputil"
Expand Down Expand Up @@ -565,6 +566,49 @@ 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,
},
},
}
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 Expand Up @@ -620,7 +664,7 @@ func Test_transportFromCfg(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := transportFromCfg(tt.args.cfg); !reflect.DeepEqual(got, tt.want) {
t.Errorf("transportFromCfg() = %v, want %v", got, tt.want)
t.Errorf("transportFromCfg() = %+v, want %+v", got, tt.want)
}
})
}
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

0 comments on commit be55bca

Please sign in to comment.