-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pubsub: add samples for dead letter topics #1279
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
76a31fe
pubsub: add samples for dead letter topics
hongalex f8e363f
pubsub: fix license headers, add test for DeliveryAttempt
hongalex 95ea997
pubsub: refactor based on code review comments
hongalex 5dd2e3d
pubsub: merge master into pubsub-dlq-samples
hongalex ee58d47
pubsub: update region tags with pubsub_dead_letter prefix
hongalex 912157d
Merge branch 'master' into pubsub-dlq-samples
tbpg 13e57b5
pubsub: revert go version to 1.11
hongalex 459773d
pubsub: fix region tag
hongalex 6bba417
Merge branch 'master' into pubsub-dlq-samples
tbpg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package subscriptions | ||
|
||
// [START pubsub_dead_letter_create_subscription] | ||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"time" | ||
|
||
"cloud.google.com/go/pubsub" | ||
) | ||
|
||
// createSubWithDeadLetter creates a subscription with a dead letter policy. | ||
func createSubWithDeadLetter(w io.Writer, projectID, subID string, topicID string, fullyQualifiedDeadLetterTopic string) error { | ||
// projectID := "my-project-id" | ||
// subID := "my-sub" | ||
// topicID := "my-topic" | ||
// fullyQualifiedDeadLetterTopic := "projects/my-project/topics/my-dead-letter-topic" | ||
ctx := context.Background() | ||
client, err := pubsub.NewClient(ctx, projectID) | ||
if err != nil { | ||
return fmt.Errorf("pubsub.NewClient: %v", err) | ||
} | ||
|
||
topic := client.Topic(topicID) | ||
|
||
subConfig := pubsub.SubscriptionConfig{ | ||
Topic: topic, | ||
AckDeadline: 20 * time.Second, | ||
DeadLetterPolicy: &pubsub.DeadLetterPolicy{ | ||
DeadLetterTopic: fullyQualifiedDeadLetterTopic, | ||
MaxDeliveryAttempts: 10, | ||
}, | ||
} | ||
|
||
sub, err := client.CreateSubscription(ctx, subID, subConfig) | ||
if err != nil { | ||
return fmt.Errorf("CreateSubscription: %v", err) | ||
} | ||
fmt.Fprintf(w, "Created subscription (%s) with dead letter topic (%s)\n", sub.String(), fullyQualifiedDeadLetterTopic) | ||
fmt.Fprintln(w, "To process dead letter messages, remember to add a subscription to your dead letter topic.") | ||
return nil | ||
} | ||
|
||
// [END pubsub_dead_letter_create_subscription] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package subscriptions | ||
|
||
// [START pubsub_dead_letter_delivery_attempt] | ||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"time" | ||
|
||
"cloud.google.com/go/pubsub" | ||
) | ||
|
||
func pullMsgsDeadLetterDeliveryAttempt(w io.Writer, projectID, subID string) error { | ||
// projectID := "my-project-id" | ||
// subID := "my-sub" | ||
ctx := context.Background() | ||
client, err := pubsub.NewClient(ctx, projectID) | ||
if err != nil { | ||
return fmt.Errorf("pubsub.NewClient: %v", err) | ||
} | ||
|
||
// Receive messages for 10 seconds. | ||
ctx, cancel := context.WithTimeout(ctx, 10*time.Second) | ||
defer cancel() | ||
|
||
sub := client.Subscription(subID) | ||
err = sub.Receive(ctx, func(ctx context.Context, msg *pubsub.Message) { | ||
// When dead lettering is enabled, the delivery attempt field is a pointer to the | ||
// the number of times the service has attempted to delivery a message. | ||
// Otherwise, the field is nil. | ||
if msg.DeliveryAttempt != nil { | ||
fmt.Fprintf(w, "message: %s, delivery attempts: %d", msg.Data, *msg.DeliveryAttempt) | ||
} | ||
msg.Ack() | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("got error in Receive: %v", err) | ||
} | ||
return nil | ||
} | ||
|
||
// [END pubsub_dead_letter_delivery_attempt] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package subscriptions | ||
|
||
// [START pubsub_dead_letter_remove] | ||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
|
||
"cloud.google.com/go/pubsub" | ||
) | ||
|
||
// removeDeadLetterTopic removes the dead letter policy from a subscription. | ||
func removeDeadLetterTopic(w io.Writer, projectID, subID string) error { | ||
// projectID := "my-project-id" | ||
// subID := "my-sub" | ||
ctx := context.Background() | ||
client, err := pubsub.NewClient(ctx, projectID) | ||
if err != nil { | ||
return fmt.Errorf("pubsub.NewClient: %v", err) | ||
} | ||
|
||
subConfig, err := client.Subscription(subID).Update(ctx, pubsub.SubscriptionConfigToUpdate{ | ||
DeadLetterPolicy: &pubsub.DeadLetterPolicy{}, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("Update: %v", err) | ||
} | ||
fmt.Fprintf(w, "Updated subscription config: %+v\n", subConfig) | ||
return nil | ||
} | ||
|
||
// [END pubsub_dead_letter_remove] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package subscriptions | ||
|
||
// [START pubsub_dead_letter_update] | ||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
|
||
"cloud.google.com/go/pubsub" | ||
) | ||
|
||
// updateDeadLetter updates an existing subscription with a dead letter policy. | ||
func updateDeadLetter(w io.Writer, projectID, subID string, fullyQualifiedDeadLetterTopic string) error { | ||
// projectID := "my-project-id" | ||
// subID := "my-sub" | ||
// fullyQualifiedDeadLetterTopic := "projects/my-project/topics/my-dead-letter-topic" | ||
ctx := context.Background() | ||
client, err := pubsub.NewClient(ctx, projectID) | ||
if err != nil { | ||
return fmt.Errorf("pubsub.NewClient: %v", err) | ||
} | ||
|
||
updateConfig := pubsub.SubscriptionConfigToUpdate{ | ||
DeadLetterPolicy: &pubsub.DeadLetterPolicy{ | ||
DeadLetterTopic: fullyQualifiedDeadLetterTopic, | ||
MaxDeliveryAttempts: 20, | ||
}, | ||
} | ||
|
||
subConfig, err := client.Subscription(subID).Update(ctx, updateConfig) | ||
if err != nil { | ||
return fmt.Errorf("Update: %v", err) | ||
} | ||
fmt.Fprintf(w, "Updated subscription config: %+v\n", subConfig) | ||
return nil | ||
} | ||
|
||
// [END pubsub_dead_letter_update] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep as 1.11. This signifies the Go version compatibility, which we need to keep at 1.11, the oldest supported GAE/GCF version.