Home >Backend Development >Golang >Google Pub/Sub settings programmatically enable message retention for topics
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!
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?
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!