Google Pub Sub 订阅。接收重试策略

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

Google Pub Sub Subscription. Receive retry policy

问题

我是你的中文翻译助手,以下是翻译好的内容:

我是一个使用Google Pub/Sub的新手,请原谅我的无知。我正在使用Go语言。
我们是一个使用Pub/Sub的公司的客户,他们发布通知,意味着我们只是消费者,不拥有Pub/Sub订阅。该公司每小时会推送100条消息,我们需要不断地查找消息。
我有以下用于获取消息的示例代码,可以在后台运行:

func PullMyMessages() {
   ctx := context.Background()
   client, _ := pubsub.NewClient(ctx, "myAPI", option.WithCredentialsFile("abc.json"))
   sub := client.Subscription("Mysub")
   msgSlice := make(chan *pubsub.Message, 1)
   cctx, cancel := context.WithCancel(context.TODO())
   go sub.Receive(cctx, func(ctx context.Context, m *pubsub.Message) {
      msgSlice <- m
   })
   for {
      select {
      case res := <-msgSlice:
         fmt.Printf("Got message: %q\n", string(res.Data))
         res.Ack()

      case <-time.After(5 * time.Minute):
         cancel()
      }
   }
}

通过上述代码,我能够获取订阅的消息,当没有订阅的消息时,我会退出应用程序。
问题:

  1. 参考链接:https://cloud.google.com/pubsub/docs/handling-failures#pubsub_dead_letter_delivery_attempt-go,由于我只是订阅的消费者,是否能够为处理失败的消息设置重试策略?有关此问题的示例代码或指南将非常有帮助。

  2. 目前,我期望该应用程序在后台运行,当它在5分钟内没有收到任何消息时,它会退出。我的理解正确吗?

在实际运行中,我会移除时间因素,以实现持续运行,但希望能够实现重试机制。

英文:

I am newbie in using google pubsub, please pardon my ignorance. I am using go language
We are a client of a company who uses pubsub who are publishing notification meaning, we do not own the pubsub subscription just consumers, the company who owns the pubsub will be pushing 100 messages an hour and we need to continuously looking for messages.
I have the below sample code for getting message which is run in the background

func PullMyMessages() {
   ctx := context.Background()
   client, _ := pubsub.NewClient(ctx, &quot;myAPI&quot;, option.WithCredentialsFile(&quot;abc.json&quot;))
   sub := client.Subscription(&quot;Mysub&quot;)
   msgSlice := make(chan *pubsub.Message, 1)
   cctx, cancel := context.WithCancel(context.TODO())
   go sub.Receive(cctx, func(ctx context.Context, m *pubsub.Message) {
      msgSlice &lt;- m
   })
   for {
      select {
      case res := &lt;-msgSlice:
         fmt.Printf(&quot;Got message: %q\n&quot;, string(res.Data))
         res.Ack()

      case &lt;-time.After(5 * time.minute):
         cancel()
      }
   }
}

With the above code I was able to get messages for the subscription and when there is no subscription for minutes i would exit out of the application.
Question

  1. ref: https://cloud.google.com/pubsub/docs/handling-failures#pubsub_dead_letter_delivery_attempt-go , since I am only the consumer/subscriber of the subscription will be able to set a retry policy set for the message that fail to process. any sample code or pointers would be helpful on this

  2. Currently I am expecting that this application is running in background and when it does not get any message for 5 minute, it would exit out. is the understanding correct?

I would in real time remove the time factor to continuously run but would want the retry mechanism in place

答案1

得分: 0

在PubSub上有几件事情需要知道:

  • 如果你不是订阅的所有者(或者至少被授权更新),你无法更改重试策略。
  • 在订阅中,消息会被保留一段时间(默认为7天,但可以在10秒到30天之间)。这意味着如果你一段时间内没有监听订阅,消息不会丢失,而是堆积在订阅中,当你的订阅者重新连接时,你会收到这些消息。
  • 你可以运行已经启动的订阅者(去除定时器的情况),但你也可以考虑批量处理消息(例如每10分钟监听订阅5分钟)。这样你可以利用无服务器服务,节省资源、金钱和二氧化碳排放!这取决于你的使用情况和实时要求。
英文:

There are several things to know on PubSub.

  • You can't change the retry policies if you are not the owner (or at list to be authorized to update) of the subscription
  • On a subscription, the messages are kept for a while (by default it's 7 days, but it could be between 10s and 30 days). That means if you don't listen the subscription for a while, the messages are not lost, but stacked in the subscription and you will received them when your subscriber is up.
  • You can run already up subscriber (remove the case with the timer), but you can also imagine to process the messages by bulk (listen 5 minutes the subscription every 10 minutes for example). Like that you can leverage serverless services and save resource, money and CO2! It's depends on your use case and realtime requirement.

huangapple
  • 本文由 发表于 2023年2月10日 11:41:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75406713.html
匿名

发表评论

匿名网友

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

确定