[go-nuts] Re: context cancellation flake

2017-09-17 Thread Steven Lee
Thank you Silviu, that seems to work.

Do you know exactly why it flakes? what is racing? just for me to have an 
understanding of why this happens

On Sunday, 17 September 2017 01:19:35 UTC+1, Silviu Capota Mera wrote:
>
> Hi Steven,
>
> In case it's still unclear, you need to wrap both the "do request" + "read 
> body" inside the same cancellation context. The "defer cancel" should 
> encompass both of them, sort of atomically, so the idea is to take it out 
> of your fetch, one level up.
>
> https://play.golang.org/p/trMP7Q-maT
>
> Cheers,
> silviu
>
> On Saturday, 16 September 2017 16:16:05 UTC-4, Steven Lee wrote:
>>
>> I guess thats plausible
>>
>> Ive tried to find the code in the library that sets the body if a context 
>> is cancelled but cant, I understand how to fix the code just struggling to 
>> understand the mechanics and especially why it flakes :(
>>
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: context cancellation flake

2017-09-16 Thread Steven Lee
I guess thats plausible

Ive tried to find the code in the library that sets the body if a context 
is cancelled but cant, I understand how to fix the code just struggling to 
understand the mechanics and especially why it flakes :(

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: context cancellation flake

2017-09-16 Thread Steven Lee
Does calling the cancel func on a context that is attached to a 
http.Request change the contents of the body to context cancellation?

On Friday, 15 September 2017 18:15:25 UTC+1, Steven Lee wrote:
>
> Hello,
>
> I was wondering if anyone could help me with understanding the behaviour 
> of cancelling context on a request before its read.
>
> I am using golang 1.9 on macOS X sierra (latest)
>
> Sometimes this code returns context cancelled, sometimes it works.
>
> package main
>
> import (
> "context"
> "io/ioutil"
> "log"
> "net/http"
> "time"
>
> "golang.org/x/net/context/ctxhttp"
> )
>
> func main() {
> req, err := http.NewRequest("GET", "https://swapi.co/api/people/1;, 
> nil)
> if err != nil {
> log.Fatal(err)
> }
> resp, err := fetch(req)
> if err != nil {
> log.Fatal(err)
> }
> log.Print(readBody(resp))
> }
>
> func fetch(req *http.Request) (*http.Response, error) {
> ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second
> )
> defer cancel()
> return ctxhttp.Do(ctx, http.DefaultClient, req)
> }
>
> func readBody(resp *http.Response) (string, error) {
> b, err := ioutil.ReadAll(resp.Body)
> if err != nil {
> return "", err
> }
> return string(b), err
> }
>
>
>
>
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.