我目前正在试验 Google Cloud PubSub 的go 库并同时查阅文档。
我的代码测试了PullWait
函数的行为,根据文档执行以下操作:
PullWait 从订阅中拉取消息。如果订阅队列中没有足够的消息,它将阻塞,直到至少有 n 条消息到达或发生超时,并且 n 不能大于 100。
但是,我的测试表明,无论n
指定的值如何,我总是会立即收到m
m <= n 的消息。我在这里错过了什么吗?
使用的代码摘录:
msgs, err := pubsub.PullWait(subCtx, subscriptionName, 50)
if err != nil {
log.Printf("Error when trying to pull messages from subscription: %v", err)
} else {
for _, msg := range msgs {
str := string(msg.Data)
log.Printf("Message [msg-id=%s]: '%v'", msg.ID, str)
if err := pubsub.Ack(ctx, subscriptionName, msg.AckID); err != nil {
log.Printf("Unable to acknowledge message [ack-id=%s]: %v", msg.AckID, err)
}
}
}
当时队列只包含一条消息,立即返回给我:
2015/11/04 11:45:15 消息 [msg-id=2384294654226]: '你好,我的朋友'