context: testing if an HTTP request was canceled

524 views
Skip to first unread message

Manlio Perillo

unread,
Aug 21, 2016, 12:53:20 PM8/21/16
to golang-nuts
I writing a simple HTTP client using the new context support in Go 1.7.
I need to test it in case a timeout is specified and expires, but I'm having problems.

The context package defines the Cancel error variable, that is returned by Context.Err when the context is canceled.
However the Transport.Roundtrip method returns errRequestCanceled or errRequestCanceled, making it impossible to check if error was due to request cancelation.

What about adding an `IsCanceled(err error) bool` function to the context package, and redefine the errRequestCanceled and errRequestCanceled so that context.IsCanceled returns true?
Right now I'm checking if the error message contains the string "canceled".


Thanks
Manlio

Ian Lance Taylor

unread,
Aug 22, 2016, 2:08:26 PM8/22/16
to Manlio Perillo, golang-nuts
I think this problem is addressed in a different way by
https://golang.org/cl/24978. Could you see if that change fixes your
problem?

Ian

ymmt2005

unread,
Aug 22, 2016, 8:49:15 PM8/22/16
to golang-nuts, manlio....@gmail.com
I am experiencing the same problem, and the fix from Ian will resolve it.

ymmt2005

2016年8月23日火曜日 3時08分26秒 UTC+9 Ian Lance Taylor:

Manlio Perillo

unread,
Aug 23, 2016, 10:34:54 AM8/23/16
to golang-nuts, manlio....@gmail.com
Il giorno lunedì 22 agosto 2016 20:08:26 UTC+2, Ian Lance Taylor ha scritto:
On Sun, Aug 21, 2016 at 9:53 AM, Manlio Perillo
<manlio....@gmail.com> wrote:
> [...]

> The context package defines the Cancel error variable, that is returned by
> Context.Err when the context is canceled.
> However the Transport.Roundtrip method returns errRequestCanceled or
> errRequestCanceled, making it impossible to check if error was due to
> request cancelation.
>
> [...] 

I think this problem is addressed in a different way by
https://golang.org/cl/24978.  Could you see if that change fixes your
problem?


Thanks.
It fixed my problem, since I can explicitly check if error is equal to context.DeadlineExceeded.

It is probably better to try to call the Timeout method, if it is implemented.


Manlio 
Reply all
Reply to author
Forward
0 new messages