英文:
Golang how can I stop an Http request from continuing to fire
问题
我今天早些时候进行了一些负载测试,并发现了一些奇怪的问题,有时候一个HTTP请求不会结束,而是一直发送。我该如何在我的Golang代码中进行修正?例如,看下面的图片。我正在进行1,000个HTTP请求的负载测试,但是如果你注意到下面的第1,000个请求花费了392,999毫秒或392秒,而其他请求平均只需要2.2秒。我已经进行了多次测试,有时候会卡住。这是我的代码:
func Home_streams(w http.ResponseWriter, r *http.Request) {
var result string
r.ParseForm()
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
db.QueryRow("select json_build_object('Locations', array_to_json(array_agg(t))) from (SELECT latitudes,county,longitudes," +
"statelong,thirtylatmin,thirtylatmax,thirtylonmin,thirtylonmax,city" +
" FROM zips where city='Orlando' ORDER BY city limit 5) t").Scan(&result)
}()
wg.Wait()
fmt.Fprintf(w,result)
}
我使用以下代码连接到数据库:
func init() {
var err error
db, err = sql.Open("postgres","Postgres Connection String")
if err != nil {
log.Fatal("Invalid DB config:", err)
}
if err = db.Ping(); err != nil {
log.Fatal("DB unreachable:", err)
}
}
我会说大约有10%的时间,在进行负载测试时会出现这个问题,唯一的停止方法是手动停止请求,否则它会无限制地继续进行。我想知道这个问题是否在这里得到了解决:https://medium.com/@nate510/don-t-use-go-s-default-http-client-4804cb19f779。我还在学习如何使用Golang。
英文:
I was doing some load testing earlier today and found something peculiar, sometimes an Http request doesn't not die and keeps on firing . How can I correct that in my Golang code for instance see the image below . I am load testing loading 1,000 HTTP request but if you notice on the 1,000th request below it takes 392,999 milliseconds or 392 seconds while the rest of the request takes 2.2 seconds on average . I have done the test multiple times and sometimes it hangs . This is my code
func Home_streams(w http.ResponseWriter, r *http.Request) {
var result string
r.ParseForm()
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
db.QueryRow("select json_build_object('Locations', array_to_json(array_agg(t))) from (SELECT latitudes,county,longitudes,"+
"statelong,thirtylatmin,thirtylatmax,thirtylonmin,thirtylonmax,city"+
" FROM zips where city='Orlando' ORDER BY city limit 5) t").Scan(&result)
}()
wg.Wait()
fmt.Fprintf(w,result)
}
and I connect to the database with this code
func init() {
var err error
db, err = sql.Open("postgres","Postgres Connection String")
if err != nil {
log.Fatal("Invalid DB config:", err)
}
if err = db.Ping(); err != nil {
log.Fatal("DB unreachable:", err)
}
}
I would say that about 10 % of the time I load test this issue happens and the only way it stops is if I stop the requests manually otherwise it keeps on going indefinitely . I wonder if maybe this issue is addressed here https://medium.com/@nate510/don-t-use-go-s-default-http-client-4804cb19f779#.83uzpsp24 I am still learning my way around Golang .
答案1
得分: 1
> 而且唯一停止的方法是手动停止请求,否则它会无限制地继续进行。
你没有展示完整的代码,但看起来你正在使用http.ListenAndServe
这个便利函数,它没有设置默认的超时时间。所以我猜测的情况是你的数据库服务器被过载了,而你的HTTP服务器没有设置超时,所以它一直在等待数据库的响应。
如果我所猜测的都正确的话,你可以尝试像这样做:
srv := &http.Server{
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
srv.ListenAndServe()
这里有一个很好的参考链接。
英文:
> and the only way it stops is if I stop the requests manually otherwise
> it keeps on going indefinitely
You're not showing your full code, but it seems like you're using the http.ListenAndServe
convenience function, which doesn't set a default timeout. So what I assume is happening is you're overloading your database server and your http server isn't set to timeout so it's just waiting for your database to respond.
Assuming all of this is correct try doing something like this instead:
srv := &http.Server{
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
srv.ListenAndServe()
There's a nice reference here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论