Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 如何超时rabbitmq使用者?_Go_Rabbitmq - Fatal编程技术网

Go 如何超时rabbitmq使用者?

Go 如何超时rabbitmq使用者?,go,rabbitmq,Go,Rabbitmq,我让rabbitmq使用队列,但一旦订阅了客户端,它将永远保持使用队列。是否存在声明和退出超时,即队列为空后 msgs, err := ch.Consume( q.Name, // queue "", // consumer true, // auto-ack false, // exclusive false, //

我让rabbitmq使用队列,但一旦订阅了客户端,它将永远保持使用队列。是否存在声明和退出超时,即队列为空后

 msgs, err := ch.Consume(
                q.Name, // queue
                "",     // consumer
                true,   // auto-ack
                false,  // exclusive
                false,  // no-local
                false,  // no-wait
                nil,    // args
        )
for msg := range msgs { 
                log.Printf("Received message with message: %s", msg.Body)
}
你可以用

下面是一个工作示例

const duration = 3 * time.Second
timer := time.NewTimer(duration)
for {
    select {
    case d := <-msgs:
        timer.Reset(duration)
        fmt.Printf("Received a message: %s\n", d.Body)
    case <- timer.C:
        fmt.Println("Timeout !")
        os.Exit(1)
    }
}
const duration=3*时间秒
计时器:=时间。新计时器(持续时间)
为了{
挑选{
案例d:=您可以使用

下面是一个工作示例

const duration = 3 * time.Second
timer := time.NewTimer(duration)
for {
    select {
    case d := <-msgs:
        timer.Reset(duration)
        fmt.Printf("Received a message: %s\n", d.Body)
    case <- timer.C:
        fmt.Println("Timeout !")
        os.Exit(1)
    }
}
const duration=3*时间秒
计时器:=时间。新计时器(持续时间)
为了{
挑选{

案例d:=是否使用break将其插入到上面的for循环中?是的,并且每次从通道读取时都重置计时器。我在案例中使用了go timeout模式,这非常有趣。我尝试过,对于'msg:=range msgs{},它看起来没有超时'循环。如果在rabbitmq loopA for…range循环外部设置为只在一个通道上进行for…select,则会执行此操作;在本例中,为rabbitmq通道。要以这种方式超时,您需要在两个通道上进行select,即rabbitmq通道和超时通道。是否使用break?Yes将其插入到上面的for循环中,并重置t每次从通道读取时都会出现imer。我在case中使用了go超时模式,这非常有趣。我尝试过,看起来对于'msg:=range msgs{}'循环。如果在rabbitmq loopA外部进行设置,则在一个通道上的for…range循环类似于只在一个通道上进行for…select;在本例中,为rabbitmq通道。要以这种方式超时,您需要在两个通道上进行select,即rabbitmq通道和超时通道。