Golang HTTP server timeout

2019-07-31 01:23发布

问题:

I'm trying to write a web server in golang to handle geocoding requests. Some of these requests take longer than a minute to process. In this case, the server seems to be returning an empty body to the client, though the handler keeps running. I've tried the code below to no avail. Am I missing something? Is it possible this is a problem with pat?

r := pat.New()

r.Get("/geocode", GeocodeHandler)
r.Get("/status", StatusHandler)
r.Get("/", InvalidHandler)

s := &http.Server{
    Addr: ":"+port,
    Handler: r,
    ReadTimeout: 10 * time.Minute,
    WriteTimeout: 10 * time.Minute,
    MaxHeaderBytes: 0,
}

s.ListenAndServe()

The client is in ruby. I don't believe that's the problem though, since I see similar behavior if I use curl.

uri = URI(URI.escape("http://blah.com/geocode?address=#{address}"))
http = Net::HTTP.new(uri.host, uri.port)
http.read_timeout = 10 * 60
response = http.request(Net::HTTP::Get.new(uri.request_uri))

回答1:

Found the issue. I didn't mention that my server was behind an Amazon ELB. Its default timeout is 60 seconds.