Skip to content

Instantly share code, notes, and snippets.

@lintianzhi
Created January 24, 2014 18:00
Show Gist options
  • Save lintianzhi/8602607 to your computer and use it in GitHub Desktop.
Save lintianzhi/8602607 to your computer and use it in GitHub Desktop.
func SetTimeout(client *Client, t int) (err error) {
timeout := time.Duration(t) * time.Second
dialTimeout := func(network, addr string) (net.Conn, error) {
return net.DialTimeout(network, addr, timeout)
}
if client.Transport == nil {
client.Transport = &http.Transport{
Dial: dialTimeout,
}
return
}
if tp, ok := client.Transport.(*http.Transport); ok {
tp.Dial = dialTimeout
} else {
err = errors.New("transport is not *http.Transport")
}
return
}
func GetTimeoutClient(t int) (Client) {
timeout := time.Duration(t) * time.Second
dialTimeout := func(network, addr string) (net.Conn, error) {
return net.DialTimeout(network, addr, timeout)
}
return Client{
&http.Client{
Transport: &http.Transport{
Dial: dialTimeout,
},
},
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment