我喜欢 Go 在内部处理 I/O 多路复用的方式,即 epoll
和其他机制,并自行安排绿色线程(此处为 go-routine),从而可以自由编写同步代码。
我知道 TCP 套接字是非阻塞
,read
会在没有可用数据时返回 EAGAIN
。鉴于此,conn.Read(buffer)
将检测到这一点并阻止 go 例程在套接字缓冲区中没有可用数据的情况下执行连接读取。有没有办法在不关闭底层连接的情况下停止这样的例程。我正在使用连接池,因此关闭 TCP 连接对我来说没有意义,我想将该连接返回到池中。
下面是模拟这种场景的代码:
func main() {
conn, _ := net.Dial("tcp", "127.0.0.1:9090")
// Spawning a go routine
go func(conn net.Conn) {
var message bytes.Buffer
for {
k := make([]byte, 255) // buffer
m, err := conn.Read(k) // blocks here
if err != nil {
if err != io.EOF {
fmt.Println("Read error : ", err)
} else {
fmt.Println("End of the file")
}
break // terminate loop if error
}
// converting bytes to string for printing
if m > 0 {
for _, b := range k {
message.WriteByte(b)
}
fmt.Println(message.String())
}
}
}(conn)
// prevent main from exiting
select {}
}
如果不可能,我可以采取哪些其他方法:
1) 调用syscall.Read
并手动处理。在这种情况下,我需要一种方法在调用 syscall.Read
之前检查套接字是否可读,否则我最终会浪费不必要的 CPU 周期。对于我的场景,我认为我可以跳过基于事件的轮询并继续调用 syscall.Read
,因为我的用例中始终有数据。
2) 任何建议:)
最佳答案
func receive(conn net.TCPConn, kill <-chan struct{}) error {
// Spawn a goroutine to read from the connection.
data := make(chan []byte)
readErr := make(chan error)
go func() {
for {
b := make([]byte, 255)
_, err := conn.Read(b)
if err != nil {
readErr <- err
break
}
data <- b
}
}()
for {
select {
case b := <-data:
// Do something with `b`.
case err := <-readErr:
// Handle the error.
return err
case <-kill:
// Received kill signal, returning without closing the connection.
return nil
}
}
}
从另一个 goroutine 发送一个空结构到 kill
以停止从连接接收。这是一个在一秒钟后停止接收的程序:
kill := make(chan struct{})
go func() {
if err := receive(conn, kill); err != nil {
log.Fatal(err)
}
}()
time.Sleep(time.Second)
kill <- struct{}{}
这可能不是您要找的东西,因为读取 goroutine 仍然会在 Read
上被阻塞,即使在您发送到 kill
之后也是如此。但是,处理传入读取的 goroutine 将终止。
关于sockets - 在不关闭连接的情况下关闭从 TCP 连接读取的 goroutine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50187876/