您的代码很好。您甚至可以进一步替换:
s.acceptChannel <-&Connection{tcpConn: rw, .... }与:
go handleConnection(&Connection{tcpConn: rw, .... })如注释中所述,例程不是系统线程,而是由Go运行时管理的轻量级线程。当为每个连接创建例程时,可以轻松地使用阻塞操作,这些操作更易于实现。然后,运行时程序将为您
选择 例程,因此,您正在寻找的行为只是存在于该语言中的其他地方。您看不到它,但是无处不在。
现在,如果您需要更复杂的功能,并且根据我们的对话,实现与超时类似的选择,您将完全按照您的建议进行操作:将所有新连接推送到一个通道,并使用计时器对其进行多路复用。这似乎是Go中要走的路。
请注意,如果 其中一个 接收器发生故障,您将无法关闭接收通道,因为另一个接收器在写入时会出现恐慌。
我的(更完整的)示例:
newConns := make(chan net.Conn)// For every listener spawn the following routinego func(l net.Listener) { for { c, err := l.Accept() if err != nil { // handle error (and then for example indicate acceptor is down) newConns <- nil return } newConns <- c }}(listener)for { select { case c := <-newConns: // new connection or nil if acceptor is down, in which case we should // do something (respawn, stop when everyone is down or just explode) case <-time.After(time.Minute): // timeout branch, no connection for a minute }}


