gpt4 book ai didi

go - Go pub 子库中 PullWait 的行为?

转载 作者:IT王子 更新时间:2023-10-29 00:46:18 25 4
gpt4 key购买 nike

我目前正在试验 Google Cloud PubSub 的 go library并同时查阅文档。

我的代码测试了 PullWait 函数的行为,根据 documentation执行以下操作:

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.

但是,我的测试表明,无论 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'

最佳答案

事实证明文档不正确。 PullWait 调用底层 pull method returnImmediately 设置为 false,这意味着它等待接收至少一条消息(但不超过 n 条消息)。我已提交更正请求。

关于go - Go pub 子库中 PullWait 的行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33521546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com