如何停止从 UDP 读取的 goroutine?

How can I stop a goroutine that is reading from UDP?

我有一个使用goroutine 读取UDP 数据包的go 程序。 我想使用 select 子句和 "stopping" 通道来关闭 goroutine,以便在不再需要时立即关闭。

这里是 goroutine 的一个简单代码示例:

func Run(c chan string, q chan bool, conn *net.UDPConn) {

    defer close(c)

    buf := make([]byte, 1024)

    for {
        select {
            case <- q:
                return
            default:
                n, _, err := conn.ReadFromUDP(buf)
                c <- string(buf[0:n])
                fmt.Println("Received ", string(buf[0:n]))

                if err != nil {
                    fmt.Println("Error: ", err)
                }
        }
    }
}

连接创建为:

    conn, err := net.ListenUDP("udp",addr.Addr)

goroutine 应该使用以下方式终止:

    close(q)

关闭 "stopping" 通道 ("q") 后,goroutine 不会立即停止。我需要通过 UDP 连接再发送一个字符串。这样做时,goroutine 停止。 我根本不理解这种行为,如果有人能启发我,我将不胜感激。

提前致谢!

当您关闭频道时,您的程序可能会在这一行停止:

n, _, err := conn.ReadFromUDP(buf)

因为执行在 ReadFrom 方法处被阻止,select 语句未被评估,因此不会立即检测到通道 q 上的关闭。当您在 UDP 连接上执行另一个发送时,ReadFrom 解除阻塞并且(一旦该循环迭代完成)控制移动到 select 语句:此时检测到 q 上的关闭。

你可以close the connection to unblock ReadFrom, as was suggested in a comment. See the PacketConn documentation in the net package,尤其是"Any blocked ReadFrom or WriteTo operations will be unblocked and return errors":

// Close closes the connection.
// Any blocked ReadFrom or WriteTo operations will be unblocked and return errors.
Close() error

根据您的需要,超时也可能是一种选择,请再次查看PacketConn documentation in the net package:

 // ReadFrom reads a packet from the connection,
 // copying the payload into b. It returns the number of
 // bytes copied into b and the return address that
 // was on the packet.
 // ReadFrom can be made to time out and return
 // an Error with Timeout() == true after a fixed time limit;
 // see SetDeadline and SetReadDeadline.
 ReadFrom(b []byte) (n int, addr Addr, err error)