Go pub sub库中PullWait的行为是什么?

huangapple go评论90阅读模式
英文:

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(&quot;Error when trying to pull messages from subscription: %v&quot;, err)
} else {
	for _, msg := range msgs {
		str := string(msg.Data)
		log.Printf(&quot;Message [msg-id=%s]: &#39;%v&#39;&quot;, msg.ID, str)

		if err := pubsub.Ack(ctx, subscriptionName, msg.AckID); err != nil {
			log.Printf(&quot;Unable to acknowledge message [ack-id=%s]: %v&quot;, 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.

huangapple
  • 本文由 发表于 2015年11月4日 20:10:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/33521546.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定