Created
January 24, 2014 18:00
-
-
Save lintianzhi/8602607 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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