Go包syscall conn.Read()是非阻塞的,并且会导致高CPU使用率。

huangapple go评论81阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2013年5月13日 15:35:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/16516948.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定