-2

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.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
user27111987
  • 995
  • 2
  • 10
  • 26

1 Answers1

1

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")
        }
    }
}
Peter
  • 29,454
  • 5
  • 48
  • 60