英文:
how to set timeout on bufio.ReadBytes()
问题
我正在编写一段代码,用于从网络中读取数据并设置超时。最初我写了这段代码(为简化起见,省略了错误检查):
func read_msg(conn net.Conn, ch chan []byte) {
b := bufio.NewReader(conn)
msg,_ := b.ReadBytes(byte('\n'))
ch <- msg
close(ch)
}
func main() {
ln,_ := net.Listen("tcp", ":12345")
conn,_ := ln.Accept()
for {
ch := make(chan []byte)
go read_msg(conn,ch)
select {
case msg := <-ch:
fmt.Println("message received: ", msg)
case <-time.After(time.Duration(1)*time.Second):
fmt.Println("timeout")
}
}
}
当超时发生时,goroutine 会保持活动状态,等待 bufio.ReadBytes()
。有没有办法在 bufio
上设置超时呢?
英文:
I am writing a code, which reads data from network with timeouts. Initially I wrote this (much simplified skipped error checks too)
func read_msg(conn net.Conn, ch chan []byte) {
b := bufio.NewReader(conn)
msg,_ := b.ReadBytes(byte('\n'))
ch <- msg
close(ch)
}
func main() {
ln,_ := net.Listen("tcp", ":12345")
conn,_ := ln.Accept()
for {
ch := make(chan []byte)
go read_msg(conn,ch)
select {
case msg := <-ch:
fmt.Println("message received: ", msg)
case <-time.After(time.Duration(1)*time.Second):
fmt.Println("timeout")
}
}
}
When the timeout happens, the goroutine stays active, waiting on the bufio.ReadBytes(). Is there anyway to set a timeout on the bufio itself?
答案1
得分: 3
不,bufio
包没有这个功能(你可以在这里阅读文档)。
更合适的解决方案是使用net.Conn
值的SetReadDeadline
方法。
// 截止时间是一个绝对时间,在此之后,I/O操作将失败而不是阻塞。截止时间适用于所有未来和挂起的I/O操作,而不仅仅是紧接着的Read或Write调用。
...
// SetReadDeadline设置未来Read调用和任何当前阻塞的Read调用的截止时间。
// t的零值表示Read不会超时。
例如:
conn.SetReadDeadline(time.Now().Add(time.Second))
英文:
No, bufio
does not have this feature (you can read the docs here).
The more appropriate solution is to use the SetReadDeadline
method of the net.Conn
value.
// A deadline is an absolute time after which I/O operations
// fail instead of blocking. The deadline applies to all future
// and pending I/O, not just the immediately following call to
// Read or Write.
...
// SetReadDeadline sets the deadline for future Read calls
// and any currently-blocked Read call.
// A zero value for t means Read will not time out.
For example:
conn.SetReadDeadline(time.Now().Add(time.Second))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论