如果我正确理解你的问题,你希望能够退出例程处理队列?
超时设置在此处不起作用。这是针对服务器/客户端心跳的,并且只有在任何时间没有收到心跳的情况下才会开始使用,但是对于没有在队列中接收到帧而不做任何事情。
我的建议是一样的东西
import "sync"
import "os/signal"
var (
wg sync.WaitGroup
sigs = make(chan os.Signal, 1)
stop = make(chan bool)
)
func main() {
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
//your other setup stuff
wg.Add(2)
go myQueueProcessor()
go mySignalProcessor()
//Now your main will wait for your goroutines to finish here.
wg.Wait()
//your program cleanup stuff
}
func myQueueProcessor() {
defer wg.Done()
//keep running over the select indefinitely
for {
select {
case <-stop: //if the stop channel is closed exit out of go routine
return
case msg := <-sub.C: //Whatever queue processes you want
//do message stuff
}
}
}
func mySignalProcessor() {
defer wg.Done()
select {
case sig := <-sigs:
close(stop)
return
}
}
现在,如果你把你的程序的终止信号,它会抓住它,关闭停止频道和队列处理的goroutine将退出出来的东西应该关闭干净。