Home >Backend Development >Golang >Google Pub/Sub settings programmatically enable message retention for topics

Google Pub/Sub settings programmatically enable message retention for topics

WBOY
WBOYforward
2024-02-09 08:10:081232browse

Google Pub/Sub 设置以编程方式启用主题的消息保留

php Xiaobian Xigua brings you new skills about Google Pub/Sub. You can now set message retention programmatically to ensure messages in your topic are not lost. This is a very useful feature, especially when working with important data or tasks that require long processing times. By enabling message retention, you can confidently process messages without worrying that they will be lost in the process. Next, we'll detail how to programmatically enable message retention for a topic using Google Pub/Sub settings. Stay tuned!

Question content

In Google pub/sub, if a topic is created, we can set the retention policy

https://console.cloud.google.com/cloudpubsub

By clicking, editing, editing topic, updating

Is it possible to update this value programmatically?

I saw that the subscription level has this configuration https://github.com/googleapis/google-cloud-go/blob/main/pubsub/subscription.go#L564 But not at the topic level?

Workaround

Yes, message retention can be configured programmatically in the topic. In Go, you can set RetentionDuration in TopicConfig and pass it to CreateTopicWithConfig Do this on creation:

tc := TopicConfig{
    RetentionDuration = 168 * time.Hour
}
topic, err := c.CreateTopicWithConfig(context.Background(), "my-topic", tc)

To do this on update, set the property in TopicConfigToUpdate and pass it to Update:

topic := client.Topic("my-topic")
topicConfig, err := topic.Update(ctx, pubsub.TopicConfigToUpdate{
  RetentionDuration = 168 * time.Hour
})

The above is the detailed content of Google Pub/Sub settings programmatically enable message retention for topics. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete