recovery

package
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 9, 2020 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package recovery contains middleware which recovers when a pubsub receive function panics.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRecoveryHandler

func NewRecoveryHandler(next pubsub_middleware.HandlerFunc, opts ...Option) pubsub_middleware.HandlerFunc

NewRecoveryHandler returns a renew recovery handler middleware.

Example
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/bvwells/pubsub-middleware/recovery"

	"cloud.google.com/go/pubsub"
)

func main() {
	ctx := context.Background()
	client, err := pubsub.NewClient(ctx, "project-id")
	if err != nil {
		log.Fatal(err)
	}

	// Log message ID if panic occurs when processing received message.
	recoveryFunc := recovery.WithRecoveryFunc(func(_ context.Context, msg *pubsub.Message, _ interface{}) {
		log.Printf("recovered from panic while handling message '%s'", msg.ID)
	})

	// Use a callback to receive messages via subscription.
	sub := client.Subscription("subscription")
	err = sub.Receive(ctx, recovery.NewRecoveryHandler(func(ctx context.Context, m *pubsub.Message) {
		fmt.Println(m.Data)
		m.Ack() // Acknowledge that we've consumed the message.
	}, recoveryFunc))
	if err != nil {
		log.Println(err)
	}
}
Output:

Types

type Option

type Option func(*options)

Option describes a recovery option.

func WithRecoveryFunc

func WithRecoveryFunc(r RecoveryFunc) Option

WithRecoveryFunc sets the function for recovering from a panic.

type RecoveryFunc

type RecoveryFunc func(context.Context, *pubsub.Message, interface{})

RecoveryFunc is a function that is called when the recovery middleware recovers from a panic. The func takes are arguments the receive context, message and the return value from recover which reports whether the goroutine is panicking. Example usages of HandlerFunc could be to log panics or nack/ack messages which cause panics.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL