How to handle HTTP timeout errors and accessing status codes in golang
Asked Answered
I

3

6

I have some code (see below) written in Go which is supposed to "fan-out" HTTP requests, and collate/aggregate the details back.

I'm new to golang and so expect me to be a nOOb and my knowledge to be limited

The output of the program is currently something like:

{
    "Status":"success",
    "Components":[
        {"Id":"foo","Status":200,"Body":"..."},
        {"Id":"bar","Status":200,"Body":"..."}, 
        {"Id":"baz","Status":404,"Body":"..."}, 
        ...
    ]
}

There is a local server running that is purposely slow (sleeps for 5 seconds and then returns a response). But I have other sites listed (see code below) that sometime trigger an error as well (if they error, then that's fine).

The problem I have at the moment is how best to handle these errors, and specifically the "timeout" related errors; in that I'm not sure how to recognise if a failure is a timeout or some other error?

At the moment I get a blanket error back all the time:

Get http://localhost:8080/pugs: read tcp 127.0.0.1:8080: use of closed network connection

Where http://localhost:8080/pugs will generally be the url that failed (hopefully by timeout!). But as you can see from the code (below), I'm not sure how to determine the error code is related to a timeout nor how to access the status code of the response (I'm currently just blanket setting it to 404 but obviously that's not right - if the server was to error I'd expect something like a 500 status code and obviously I'd like to reflect that in the aggregated response I send back).

The full code can be seen below. Any help appreciated.

    package main

    import (
            "encoding/json"
            "fmt"
            "io/ioutil"
            "net/http"
            "sync"
            "time"
    )

    type Component struct {
            Id  string `json:"id"`
            Url string `json:"url"`
    }

    type ComponentsList struct {
            Components []Component `json:"components"`
    }

    type ComponentResponse struct {
            Id     string
            Status int
            Body   string
    }

    type Result struct {
            Status     string
            Components []ComponentResponse
    }

    var overallStatus string = "success"

    func main() {
            var cr []ComponentResponse
            var c ComponentsList

            b := []byte(`{"components":[{"id":"local","url":"http://localhost:8080/pugs"},{"id":"google","url":"http://google.com/"},{"id":"integralist","url":"http://integralist.co.uk/"},{"id":"sloooow","url":"http://stevesouders.com/cuzillion/?c0=hj1hfff30_5_f&t=1439194716962"}]}`)

            json.Unmarshal(b, &c)

            var wg sync.WaitGroup

            timeout := time.Duration(1 * time.Second)
            client := http.Client{
                    Timeout: timeout,
            }

            for i, v := range c.Components {
                    wg.Add(1)

                    go func(i int, v Component) {
                            defer wg.Done()

                            resp, err := client.Get(v.Url)

                            if err != nil {
                                fmt.Printf("Problem getting the response: %s\n", err)

                                cr = append(cr, ComponentResponse{
                                    v.Id,
                                    404,
                                    err.Error(),
                                })
                            } else {
                                    defer resp.Body.Close()
                                    contents, err := ioutil.ReadAll(resp.Body)
                                    if err != nil {
                                            fmt.Printf("Problem reading the body: %s\n", err)
                                    }

                                    cr = append(cr, ComponentResponse{
                                            v.Id,
                                            resp.StatusCode,
                                            string(contents),
                                    })
                            }
                    }(i, v)
            }
            wg.Wait()

            j, err := json.Marshal(Result{overallStatus, cr})
            if err != nil {
                    fmt.Printf("Problem converting to JSON: %s\n", err)
                    return
            }

            fmt.Println(string(j))
    }
Incognizant answered 16/8, 2015 at 18:22 Comment(5)
Most likely unrelated to your problem, but you have a data race appending to cr. You cannot write the same variable from multiple goroutines without synchronization. You may want to build/run with the race detector.Haplite
Thanks for the comment. I will look to utilise channels instead + I'll investigate that race detector :-)Incognizant
If the client call returns an error, there is no status code, because there was no complete http request. There's not much you can do with an error at that point, but in go1.5 a client.Timeout will at least return a better message in an net.Error.Antung
Will this help?Drumhead
Seems 1.5 is overdue its release date (as of August 17th 2015). I'll have to hold out to see if that indeed resolves the issue.Incognizant
W
3

I am adding this for completes, as the correct answer was provided by Dave C in the comments of the accepted answer.

We can check if the error is or contains a net.Error and check if it is a timeout.

resp, err := client.Get(url)
if err != nil {
    // if there is an error check if its a timeout error
    var netErr net.Error
    if errors.As(err, &netErr) && netErr.Timeout() {
        // handle timeout
           return
    } 
    // otherwise handle other types of error
}
Westbrooke answered 1/2, 2022 at 8:31 Comment(2)
Thanks. Agreed. I've removed my original chosen answer, but SO isn't letting me select this as the new answer?Incognizant
@Integralist, do you get any warning notifcation or something?Westbrooke
V
1

If you want to fan out then aggregate results and you want specific timeout behavior the net/http package isn't giving you, then you may want to use goroutines and channels.

I just watched this video today and it will walk you through exactly those scenarios using the concurrency features of Go. Plus, the speaker Rob Pike is quite the authority -- he explains it much better than I could.

https://www.youtube.com/watch?v=f6kdp27TYZs

Vintage answered 18/8, 2015 at 0:13 Comment(1)
the downvote is for posting an hour long video. where is the answer?Looney
I
-4

The Go 1.5 release solved this issue by being more specific about the type of error it has handled.

So if you see this example https://github.com/Integralist/Go-Requester/blob/master/requester.go#L38 you'll see that I'm able to apply a regex pattern to the error message to decipher if the error was indeed a timeout or not

status := checkError(err.Error())

func checkError(msg string) int {
    timeout, _ := regexp.MatchString("Timeout", msg)

    if timeout {
        return 408
    }

  return 500
}
Incognizant answered 24/8, 2015 at 10:58 Comment(5)
Don't grok around in error strings at best it's incredible fragile; the error string is not part of any API. The net.Error interface defines the Timeout method for this.Haplite
@Dave C thanks for the comment but I'm not sure how what you've suggested works. The error I get back from client.Get is a string. It has no methods. So nothing to queryIncognizant
It's not just a string, it's an error. You through away everything but the formatted result string with by calling your check function with err.Error(). Instead you should attempt a type assertion and other checks. Only if all that doesn't work should you a) submit a bug report about a non-testable error and b) as an ugly work-around (with a big warning comment) maybe grep around in the string (or change the code you're calling to return a testable error and submit that as a change request upstream).Haplite
By the way I finally got around to trying out type assertion checking (err.(net.Error)) and it seems that the the type of the error is *url.Error and not net.Error and so I couldn't use that ; panic: interface conversion: *url.Error is not net.Error: missing method TemporaryIncognizant
A url.Error contains an Err error field with an underlying error (if any). It's also used almost exclusively for URL parsing errors in which case it would never be a timeout. (e.g. something like if e, ok := err.(net.Error); ok && e.Timeout() {/*…network timeout…*/} would correctly say it's not a network timeout related error.)Haplite

© 2022 - 2024 — McMap. All rights reserved.