ctx, cancel = context.WithTimeout(context.Background(), client.Timeout)



On Tuesday, October 31, 2017 at 5:52:29 AM UTC-7, 慕希颜 wrote:
>
> Is there a way to add a timeout setting for per request if i declare a 
> global var? Because in my case i need different timeout setting for per 
> request.Currently i am doing it with creating a new client object for per 
> request:
>
> func getTiemoutClient(timeout int) *http.Client {
>      client := &http.Client{
>                 Timeout: time.Duration(timeout) * time.Millisecond,
>         }
>         return client
> }
>
>
> is this a better way ?
>
> func Do(ctx context.Context, client *http.Client, req *http.Request) 
> (*http.Response, error) {
>     if client == nil {
>         client = http.DefaultClient
>     }
>     resp, err := client.Do(req.WithContext(ctx))
>     // If we got an error, and the context has been canceled,
>     // the context's error is probably more useful.
>     if err != nil {
>         select {
>         case <-ctx.Done():
>             err = ctx.Err()
>         default:
>         }
>     }
>     return resp, 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.

Reply via email to