set timeout for http get request is possible?

1,443 views
Skip to first unread message

Yaşar Arabacı

unread,
Jan 22, 2013, 2:23:49 PM1/22/13
to golan...@googlegroups.com
Is there a way to set a timeout limit for net/http's client.Get()? Currently I have this much;

func disallowRedirect(req *http.Request, via []*http.Request) error {
return errors.New("Redirection is not allowed.")
}
func somefunction(url string, timeout float64, userAgent string)
client := &http.Client(
        CheckRedirect: disallowRedirect,
    )
    req, err := http.NewRequest("GET", url, nil);
    if err != nil {
        return
    }
    req.Header.add("User-Agent", userAgent);
    client.Do(req)
}

--
http://yasararabaci.tumblr.com

Fatih Arslan

unread,
Jun 3, 2013, 9:15:27 AM6/3/13
to Piyush Madan, golan...@googlegroups.com, yasar...@gmail.com
You can use https://github.com/mreiferson/go-httpclient

From Readme:

HttpClient wraps Go's built in HTTP client providing an API to:

- set timeouts
- separate connect timeout
- request based timeout (*not* just read/write deadline)
- easy access to the connection object for a given request


--
Fatih Arslan


On 3 June 2013 Monday at 13:50, Piyush Madan wrote:

> > Yaşar Arabacı if you found the answer to this question please post here, I would be really grateful.
>
>
>
> Otherwise, can someone else please help me out here? How do you set the timeout of a Get request in golang?
>
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com (mailto:golang-nuts...@googlegroups.com).
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



Brad Fitzpatrick

unread,
Jun 3, 2013, 10:21:12 AM6/3/13
to Yaşar Arabacı, golang-nuts
Your "timeout float64" should be a time.Duration, not a float64--- is that float64 seconds, days, weeks?

See http://golang.org/pkg/net/http/#Transport and its ResponseHeaderTimeout.



--
 
 

John Nagle

unread,
Jun 3, 2013, 1:36:26 PM6/3/13
to golan...@googlegroups.com
On 6/3/2013 7:21 AM, Brad Fitzpatrick wrote:
> Your "timeout float64" should be a time.Duration, not a float64--- is that
> float64 seconds, days, weeks?
>
> See http://golang.org/pkg/net/http/#Transport and its ResponseHeaderTimeout.

For real-world HTTP use with arbitrary sites, you have to have
timeouts on everything, not just headers. Some sites quickly respond
with a header, then stall while providing the response body.
(Some content management systems do this when overloaded.)

Also, does ResponseHeaderTimeout handle stalls during SSL
negotiation? (Try "https://soton.ac.uk/". Your browser will
be stuck for minutes.)

John Nagle

Brad Fitzpatrick

unread,
Jun 4, 2013, 1:22:38 PM6/4/13
to John Nagle, golang-nuts
The other timeouts are under your control.




--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages