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

Added timeout option to http client #102

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
8 changes: 5 additions & 3 deletions components/phttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ type ClientConfig struct {
Redirect bool // When true, follow HTTP redirects.
Dialer DialerConfig `config:"dial"`
Transport TransportConfig `config:",squash"`
Timeout time.Duration `config:"timeout"`
}

func DefaultClientConfig() ClientConfig {
return ClientConfig{
Transport: DefaultTransportConfig(),
Dialer: DefaultDialerConfig(),
Redirect: false,
Timeout: time.Second * 10,
}
}

Expand All @@ -45,7 +47,7 @@ func DefaultClientConfig() ClientConfig {
type DialerConfig struct {
DNSCache bool `config:"dns-cache" map:"-"`

Timeout time.Duration `config:"timeout"`
Timeout time.Duration `config:"connect-timeout"`
DualStack bool `config:"dual-stack"`

// IPv4/IPv6 settings should not matter really,
Expand Down Expand Up @@ -116,9 +118,9 @@ func NewHTTP2Transport(conf TransportConfig, dial netutil.DialerFunc) *http.Tran
return tr
}

func newClient(tr *http.Transport, redirect bool) Client {
func newClient(tr *http.Transport, redirect bool, timeout time.Duration) Client {
if redirect {
return redirectClient{&http.Client{Transport: tr}}
return redirectClient{&http.Client{Transport: tr, Timeout: timeout}}
}
return noRedirectClient{tr}
}
Expand Down
2 changes: 1 addition & 1 deletion components/phttp/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func newConnectClient(conf ConnectGunConfig) Client {
conf.ConnectSSL,
NewDialer(conf.Client.Dialer),
))
return newClient(transport, conf.Client.Redirect)
return newClient(transport, conf.Client.Redirect, conf.Client.Timeout)
}

func newConnectDialFunc(target string, connectSSL bool, dialer netutil.Dialer) netutil.DialerFunc {
Expand Down
4 changes: 2 additions & 2 deletions components/phttp/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type HTTP2GunConfig struct {

func NewHTTPGun(conf HTTPGunConfig) *HTTPGun {
transport := NewTransport(conf.Client.Transport, NewDialer(conf.Client.Dialer).DialContext)
client := newClient(transport, conf.Client.Redirect)
client := newClient(transport, conf.Client.Redirect, conf.Client.Timeout)
return NewClientGun(client, conf.Gun)
}

Expand All @@ -40,7 +40,7 @@ func NewHTTP2Gun(conf HTTP2GunConfig) (*HTTPGun, error) {
return nil, errors.New("HTTP/2.0 over TCP is not supported. Please leave SSL option true by default.")
}
transport := NewHTTP2Transport(conf.Client.Transport, NewDialer(conf.Client.Dialer).DialContext)
client := newClient(transport, conf.Client.Redirect)
client := newClient(transport, conf.Client.Redirect, conf.Client.Timeout)
// Will panic and cancel shooting whet target doesn't support HTTP/2.
client = &panicOnHTTP1Client{client}
return NewClientGun(client, conf.Gun), nil
Expand Down