-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pubsub: add samples for dead letter topics (#1279)
- Loading branch information
Showing
7 changed files
with
395 additions
and
82 deletions.
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
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_subscription] | ||
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_subscription] |
Oops, something went wrong.