英文:
Behaviour of PullWait in Go pub sub library?
问题
我目前正在尝试使用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 Message [msg-id=2384294654226]: 'hello world my friend'
英文:
I am currently experimenting with Google Cloud PubSub's go library and consulting the documentation at the same time.
My code tests the behaviour of the PullWait
function which, according to the documentation does the following:
> PullWait pulls messages from the subscription. If there are not enough messages left in the subscription queue, it will block until at least n number of messages arrive or timeout occurs, and n could not be larger than 100.
However, my test shows that regardless of the value n
specified, I always immediately receive m
messages where m <= n. Am I missing something here?
Excerpt of code used:
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)
}
}
}
And at the time queue contained only one message, which was returned to me rightaway:
> 2015/11/04 11:45:15 Message [msg-id=2384294654226]: 'hello world my friend'
答案1
得分: 2
原文翻译如下:
原来文档是错误的。PullWait 调用底层的 pull 方法,将 returnImmediately 设置为 false,这意味着它等待接收至少一个消息(但不超过 n 条消息)。我已经提交了一个请求来进行更正。
英文:
It turns out the documentation is incorrect. PullWait makes a call to the underlying pull method with returnImmediately set to false, which means it waits for at least one message (but not more than n messages) to be received. I have submitted a request to make the correction.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论