英文:
Go package syscall conn.Read() is non-blocking and cause high CPU usage
问题
奇怪的是,在我的情况下,Read()
是非阻塞的,并且导致了高 CPU 使用率。
我的代码:
在 main
函数中:
l, err := net.Listen("tcp", ":13798")
if err != nil {
log.Fatal(err)
}
for {
// 等待连接。
conn, err := l.Accept()
if err != nil {
log.Fatal(err)
}
// 在一个新的 goroutine 中处理连接。
// 然后循环返回到接受连接的状态,以便可以同时处理多个连接。
go reqHandler.TCPHandler(conn)
runtime.Gosched()
}
TCPHandler
函数:
func TCPHandler(conn net.Conn) {
request := make([]byte, 4096)
for {
read_len, err := conn.Read(request)
if err != nil {
if err.Error() == "use of closed network connection" {
LOG("连接关闭,可能发生了错误")
break
}
neterr, ok := err.(net.Error);
if ok && neterr.Timeout() {
fmt.Println(neterr)
LOG("客户端超时!")
break
}
}
if read_len == 0 {
LOG("没有读取到数据")
continue
} else {
// 做一些操作
}
request := make([]byte, 4096)
}
}
问题是,conn.Read()
是非阻塞的,所以每次都会执行 LOG("没有读取到数据")
然后继续,这导致了高 CPU 使用率。如何使 conn.Read()
成为阻塞调用?
我已经研究了 syscall
包,但在 Syscall.Read()
上卡住了。
由于我在我的 OS X 10.8.3 上发现了这个问题,这里是相关的源代码:
http://golang.org/src/pkg/syscall/zsyscall_darwin_amd64.go?h=Read#L898
我不知道 Syscall(SYS_READ, uintptr(fd), uintptr(_p0), uintptr(len(p)))
是什么意思。
英文:
Strangely, in my case Read()
is non-blocking and caused high CPU usage.
My code:
In function main
:
l, err := net.Listen("tcp", ":13798")
if err != nil {
log.Fatal(err)
}
for {
// Wait for a connection.
conn, err := l.Accept()
if err != nil {
log.Fatal(err)
}
// Handle the connection in a new goroutine.
// The loop then returns to accepting, so that
// multiple connections may be served concurrently.
go reqHandler.TCPHandler(conn)
runtime.Gosched()
}
Function TCPHandler
:
func TCPHandler(conn net.Conn) {
request := make([]byte, 4096)
for {
read_len, err := conn.Read(request)
if err != nil {
if err.Error() == "use of closed network connection" {
LOG("Conn closed, error might happened")
break
}
neterr, ok := err.(net.Error);
if ok && neterr.Timeout() {
fmt.Println(neterr)
LOG("Client timeout!")
break
}
}
if read_len == 0 {
LOG("Nothing read")
continue
} else {
// do something
}
request := make([]byte, 4096)
}
}
The problem is, conn.Read()
is non-blocking, so every time it goes to LOG("Nothing read")
then continue, this is causing high CPU usage. How to make conn.Read()
a block call?
I've researched into syscall
package but got stucked at Syscall.Read()
Since I found this issue on my OS X 10.8.3 here is the source code related:
http://golang.org/src/pkg/syscall/zsyscall_darwin_amd64.go?h=Read#L898
I have no idea what Syscall(SYS_READ, uintptr(fd), uintptr(_p0), uintptr(len(p)))
means.
答案1
得分: 8
你没有正确处理TCP。当conn.Read()返回0字节时,这意味着对等方已经正常关闭了TCP连接。在这种情况下,你应该关闭你的TCP连接的一端。
(请注意,这不仅适用于Go语言,read()/recv()在TCP连接上返回0几乎普遍意味着另一端已关闭连接)
英文:
You're not handling TCP correctly. When conn.Read() returns 0 bytes read, that means the peer has closed the TCP connection gracefully. You should probably close your end of the TCP connection in this case.
(Note that this is not special to Go, read()/recv() returning 0 on a TCP connection more or less universally means the other end has closed the connection)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论