Pkzzz
Pkzzz

Reputation: 111

transport.Roundtrip context canceled error

I made a roundtrip in client, and I set a retry for 1 time. Question is sometimes my client triggered "context canceled", and entire processing seems like end in 300ms, but I didn't set a timeout. how is it triggered?

func RoundTrip(req *http.Request)(res *http.Response, err error){
    for i:=0; i<1; i++ {
        transport = http.DefaultTransport
        res, err = transport.RoundTrip(req)
        if err == nil {
             break;
        }
    }
    
    return 
}

Upvotes: 0

Views: 1182

Answers (1)

Liam Kelly
Liam Kelly

Reputation: 3714

You can add the context to the request with the Request.WithContext method or NewRequestWithContext function. When you cancel the context it should propagate to all functions handling the request.

Update

Sorry I thought you wanted to cancel the request, not asking why. Anyhow given your comment on this answer:

but where canceled the context in 300ms

300ms is pretty short. If it was longer I would recommend adjusting the timers in net.DefaultTransport; however, instead I would guess that the underlying TCP connection is being refused/limited and causing the RoundTrip to fail. TCP limit could be something internal (DNS lookup error, etc) or on the server side. You would need to scope the problem more to debug.

Upvotes: 1

Related Questions