首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Golang服务器超时

Golang服务器超时
EN

Stack Overflow用户
提问于 2013-12-12 23:15:40
回答 2查看 1.5K关注 0票数 0

我有一个非常简单的go服务器:

代码语言:javascript
复制
package main

import(
  "net/http"
  "fmt"
  "log"
)

func test(w http.ResponseWriter, r *http.Request){
  fmt.Println("No bid")
  http.Error(w, "NoBid", 204)
}

func main() {
  http.HandleFunc("/test/bid", test)
  http.ListenAndServe(":8080", nil)
  log.Println("Done serving")
}

然后运行apache基准测试工具:

代码语言:javascript
复制
ab -c 50 -n 50000 -p post.txt http://127.0.0.1:8080/test/bid

服务器运行并响应大约15000个请求,然后超时。我想知道为什么会这样,如果有什么我可以做的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-12-13 02:13:31

如果您在Linux中运行,可能打开的文件太多,因此无法创建连接,您需要更改系统配置来支持更多的连接。

例如,

编辑/etc/security/limits.conf添加

代码语言:javascript
复制
*    soft    nofile 100000
*    soft    nofile 100000

打开更多文件。

编辑/etc/sysctl.conf

代码语言:javascript
复制
# use more port
net.ipv4.ip_local_port_range = 1024 65000
# keep alive timeout
net.ipv4.tcp_keepalive_time = 300
# allow reuse
net.ipv4.tcp_tw_reuse = 1
# quick recovery
net.ipv4.tcp_tw_recycle = 1
票数 3
EN

Stack Overflow用户

发布于 2013-12-13 00:29:03

我试图在linux amd64笔记本上复制您的问题,但没有成功--即使在

代码语言:javascript
复制
ab -c 200 -n 500000 -p post.txt http://127.0.0.1:8080/test/bid

虽然有大约28,000个插座打开,但这可能会对您的系统造成限制。

一个更真实的测试可能是打开在400个套接字上最大的内存。

代码语言:javascript
复制
ab -k -c 200 -n 500000 -p post.txt http://127.0.0.1:8080/test/bid

结果是

代码语言:javascript
复制
Server Software:        
Server Hostname:        127.0.0.1
Server Port:            8080

Document Path:          /test/bid
Document Length:        6 bytes

Concurrency Level:      200
Time taken for tests:   33.807 seconds
Complete requests:      500000
Failed requests:        0
Write errors:           0
Keep-Alive requests:    500000
Total transferred:      77000000 bytes
Total body sent:        221500000
HTML transferred:       3000000 bytes
Requests per second:    14790.04 [#/sec] (mean)
Time per request:       13.523 [ms] (mean)
Time per request:       0.068 [ms] (mean, across all concurrent requests)
Transfer rate:          2224.28 [Kbytes/sec] received
                        6398.43 kb/s sent
                        8622.71 kb/s total

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0      11
Processing:     0   14   5.2     13      42
Waiting:        0   14   5.2     13      42
Total:          0   14   5.2     13      42

Percentage of the requests served within a certain time (ms)
  50%     13
  66%     16
  75%     17
  80%     18
  90%     20
  95%     21
  98%     24
  99%     27
 100%     42 (longest request)

我建议您尝试使用ab-k,并查看如何为大量打开的套接字对系统进行调优。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20556224

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档