登录
首页 >  Golang >  Go问答

golang http Rest 服务在高负载情况下出现阻塞

来源:stackoverflow

时间:2024-02-29 21:03:26 186浏览 收藏

哈喽!今天心血来潮给大家带来了《golang http Rest 服务在高负载情况下出现阻塞》,想必大家应该对Golang都不陌生吧,那么阅读本文就都不会很困难,以下内容主要涉及到,若是你正在学习Golang,千万别错过这篇文章~希望能帮助到你!

问题内容

我正在尝试测试 golang 如何处理大负载,以将其与我们当前使用 java 制作的应用程序进行比较。

我所做的是一个简单的回显休息服务(我只添加了代码的重要部分):

// return default message for root routing
func index(w http.responsewriter, r *http.request) {
    fmt.fprintf(w, "hello, %q", html.escapestring(r.url.path))
}

// main function
func main() {

    router := mux.newrouter() //.strictslash(true)
    router.handlefunc("/", index).methods("get")
    router.handlefunc("/echo/{message}", echohandler(calledserviceurl)).methods("get")

    log.println("running server....")

    log.fatal(http.listenandserve(port, router))
}

我使用 ab 工具进行了测试,它与 -c 500 -n 500 配合使用效果很好,但是当我尝试使用这样的大负载进行测试时

ab -c 500 -n 50000 http://localhost:9596/echo/javier

该过程运行良好几秒钟,但随后似乎关闭了 tcp 连接,因为我收到以下错误:

Benchmarking localhost (be patient)
apr_socket_recv: Connection reset by peer (54)
Total of 501 requests completed

是由于我的测试达到了操作系统限制还是我的 golang 应用程序可以处理的限制?

有没有更好的方法来处理请求,然后避免程序关闭连接? (队列请求或类似的东西)。

提前致谢 j


解决方案


你碰巧使用 osx 吗?我知道 ab 在 osx 上被破坏了。

您可以尝试的另一件事是使用 -k 来使用保持活动标志,但这可能不是您想要的。

50000 也接近接口上的最大套接字数,因此可能您的套接字已耗尽。套接字不能直接重用,因为它们将处于 time_wait 状态一两分钟。确切的值可能因操作系统和配置而异。

但是,代码看起来也很好。

以下代码对我来说效果很好:

package main

import (
    "fmt"
    "github.com/gorilla/mux"
    "log"
    "net/http"
)


func echo(w http.responsewriter, r *http.request) {
    v := mux.vars(r)

    fmt.fprintf(w, "echo %v", v["message"])
}


func main() {

    router := mux.newrouter() //.strictslash(true)
    router.handlefunc("/echo/{message}", echo).methods("get")

    log.println("running server....")

    log.fatal(http.listenandserve("localhost:8080", router))
}

并给出以下结果:

ab -c 500 -n 50000 localhost:8080/echo/foobar
This is ApacheBench, Version 2.3 <$Revision: 1807734 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 5000 requests
Completed 10000 requests
Completed 15000 requests
Completed 20000 requests
Completed 25000 requests
Completed 30000 requests
Completed 35000 requests
Completed 40000 requests
Completed 45000 requests
Completed 50000 requests
Finished 50000 requests


Server Software:        
Server Hostname:        localhost
Server Port:            8080

Document Path:          /echo/foobar
Document Length:        12 bytes

Concurrency Level:      500
Time taken for tests:   2.471 seconds
Complete requests:      50000
Failed requests:        0
Total transferred:      6450000 bytes
HTML transferred:       600000 bytes
Requests per second:    20233.39 [#/sec] (mean)
Time per request:       24.712 [ms] (mean)
Time per request:       0.049 [ms] (mean, across all concurrent requests)
Transfer rate:          2548.93 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   18 122.8      3    1034
Processing:     0    5  14.9      4     225
Waiting:        0    4  14.7      3     222
Total:          1   24 132.7      6    1245

Percentage of the requests served within a certain time (ms)
  50%      6
  66%      7
  75%      7
  80%      7
  90%     12
  95%     20
  98%     30
  99%   1040
 100%   1245 (longest request)

这是在 ubuntu 18.04 上执行的。

到这里,我们也就讲完了《golang http Rest 服务在高负载情况下出现阻塞》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>