Golang服务器超时

时间:2013-12-12 23:15:40

标签: go

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

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基准测试工具:

ab -c 50 -n 50000 -p post.txt http://127.0.0.1:8080/test/bid

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

2 个答案:

答案 0 :(得分:3)

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

例如,

修改/etc/security/limits.conf添加

*    soft    nofile 100000
*    soft    nofile 100000

要打开更多文件。

修改/etc/sysctl.conf

# 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

答案 1 :(得分:2)

我试图在我的linux amd64笔记本电脑上复制你的问题没有成功 - 它甚至可以正常工作

ab -c 200 -n 500000 -p post.txt http://127.0.0.1:8080/test/bid

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

一个更现实世界的测试可能是将最大限度超过400个套接字的Keepalive

ab -k -c 200 -n 500000 -p post.txt http://127.0.0.1:8080/test/bid

结果是

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并查看为大量开放套接字调整系统