gorequest package : check specifically for timeout

346 views Asked by At

I am using the below package to make a outbound http request https://github.com/parnurzeal/gorequest

For eg I am making a GET request like below res, body, errs = goReq.Get(url).End()

My question is how to figure out if there is timeout in the request.

1

There are 1 answers

0
Peter On BEST ANSWER

Since the Timeout method sets the dealines for dial, read, and write, you can use os.IsTimeout (all error types in the net and net/url packages implement Timeout() bool). Contexts are not supported by gorequest, so context.Canceled doesn't have to be considered:

package main

import (
    "log"
    "net/http"
    "net/http/httptest"
    "time"

    "github.com/parnurzeal/gorequest"
)

func main() {
    s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        time.Sleep(time.Second)
    }))

    request := gorequest.New()
    _, _, errs := request.Get(s.URL).Timeout(500 * time.Millisecond).End()

    for _, err := range errs {
        if os.IsTimeout(err) {
            log.Fatal("timeout")
        }
    }
}