我已经在Go上写了一个http服务器,每天有超过一千个访问者。我现在有一个积累的Goroutine问题。在一天的过程中,我似乎从http服务器上获得了一千多个新的Goroutines。
我不知道我怎么会弄乱处理程序。
http.Handle("/",http.FileServer(http.Dir(config.htdocs_path)))
下面是堆栈中的goroutine之一
goroutine 1582 [chan receive]: net.(*pollServer).WaitRead(0xf84007f680,0xf84066dea0,0xf84007aa80,0xb,0x1,...) /home/ec2-user/go/src/pkg/net/fd.go:268 +0x73 net.(*netFD).Read(0xf84066dea0,0xf840ec1000,0x100000001000,0x7f7effffffff,0xf84007c0f0,...) /home/ec2-user/go/src/pkg/net/fd.go:428 +0x1ec net.(*TCPConn).Read(0xf84068aff8,0xf800000002,0x0,...) /home/ec2-user/go/src/pkg/net/tcpsock_posix.go:87 +0xce io.(*LimitedReader).Read(0xf840d1bc20,0xdcb00000000,...) /home/ec2-user/go/src/pkg/io/io.go:394 +0xc1 bufio.(*Reader).fill(0xf8405b0900,0xdcb00000000) /home/ec2-user/go/src/pkg/bufio/bufio.go:77 +0xf0 bufio.(*Reader).ReadSlice(0xf8405b0900,0xf840d1bc0a,...) /home/ec2-user/go/src/pkg/bufio/bufio.go:257 +0x1b6 bufio.(*Reader).ReadLine(0xf8405b0900,...) /home/ec2-user/go/src/pkg/bufio/bufio.go:283 +0x5b net/textproto.(*Reader).readLineSlice(0xf840730660,0xc0,0x100000000,0x7f7e00000001) /home/ec2-user/go/src/pkg/net/textproto/reader.go:55 +0x4f net/textproto.(*Reader).ReadLine(0xf840730660,0xf84061f300,0x48411c) /home/ec2-user/go/src/pkg/net/textproto/reader.go:36 +0x25 net/http.ReadRequest(0xf8405b0900,0x100000400ccf60,...) /home/ec2-user/go/src/pkg/net/http/request.go:457 +0xb1 net/http.(*conn).readRequest(0xf8402b2b40,0xf8400e3fc0,0xf8405b0a80,...) /home/ec2-user/go/src/pkg/net/http/server.go:240 +0xa8 net/http.(*conn).serve(0xf8402b2b40,0x0) /home/ec2-user/go/src/pkg/net/http/server.go:594 +0x145 created by net/http.(*Server).Serve /home/ec2-user/go/src/pkg/net/http/server.go:1040 +0x430
看起来连接正在读取状态。像http服务器不是时间他们出来。默认服务器是否没有读取超时?
go版本go1
所有这些goroutine正在阅读的原因是保持活着。当浏览器发送保持活动的标题时,服务器保持连接打开以接受更多的请求。当客户端请求许多小文件,并且TCP连接是重要的开销时,这是一件好事。读取超时将确保在请求之间不超过一定时间内的连接保持活动。这将关闭保持活着的连接,但也可以防止某人上传超过超时。不幸的是,还没有保留特定的超时选项。
默认情况下,没有超时。您可以在Server struct http://golang.org/pkg/net/http/#Server中设置超时
srv := &http.Server{ Handler: http.FileServer(http.Dir(config.htdocs_path)),ReadTimeout: 30*time.Second,} srv.ListenAndServe()