Golang HTTP服务器超时

huangapple go评论73阅读模式
英文:

Golang HTTP server timeout

问题

我正在尝试使用golang编写一个Web服务器来处理地理编码请求。其中一些请求需要超过一分钟的时间来处理。在这种情况下,服务器似乎会向客户端返回一个空的响应体,尽管处理程序仍在运行。我尝试了下面的代码,但没有成功。我是否漏掉了什么?这可能是与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()

客户端是用Ruby编写的。我不认为这是问题,因为我使用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))
英文:

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

得分: 3

发现问题了。我没有提到我的服务器在亚马逊ELB后面。它的默认超时时间是60秒。

英文:

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

huangapple
  • 本文由 发表于 2013年7月17日 11:03:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/17690380.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定