mempubsub

package
v0.18.1-0...-1aa001a Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2020 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package mempubsub provides an in-memory pubsub implementation. Use NewTopic to construct a *pubsub.Topic, and/or NewSubscription to construct a *pubsub.Subscription.

mempubsub should not be used for production: it is intended for local development and testing.

URLs

For pubsub.OpenTopic and pubsub.OpenSubscription, mempubsub registers for the scheme "mem". To customize the URL opener, or for more details on the URL format, see URLOpener. See https://github.com/kainoaseto/go-cloud/concepts/urls/ for background information.

Message Delivery Semantics

mempubsub supports at-least-once semantics; applications must call Message.Ack after processing a message, or it will be redelivered. See https://godoc.org/github.com/kainoaseto/go-cloud/pubsub#hdr-At_most_once_and_At_least_once_Delivery for more background.

As

mempubsub does not support any types for As.

Example (OpenSubscriptionFromURL)
package main

import (
	"context"
	"log"

	"github.com/kainoaseto/go-cloud/pubsub"
)

func main() {
	// PRAGMA: This example is used on github.com/kainoaseto/go-cloud; PRAGMA comments adjust how it is shown and can be ignored.
	// PRAGMA: On github.com/kainoaseto/go-cloud, add a blank import: _ "github.com/kainoaseto/go-cloud/pubsub/mempubsub"
	// PRAGMA: On github.com/kainoaseto/go-cloud, hide lines until the next blank line.
	ctx := context.Background()

	// Create a topic.
	topic, err := pubsub.OpenTopic(ctx, "mem://topicA")
	if err != nil {
		log.Fatal(err)
	}
	defer topic.Shutdown(ctx)

	// Create a subscription connected to that topic.
	subscription, err := pubsub.OpenSubscription(ctx, "mem://topicA")
	if err != nil {
		log.Fatal(err)
	}
	defer subscription.Shutdown(ctx)
}
Output:

Example (OpenTopicFromURL)
package main

import (
	"context"
	"log"

	"github.com/kainoaseto/go-cloud/pubsub"
)

func main() {
	// PRAGMA: This example is used on github.com/kainoaseto/go-cloud; PRAGMA comments adjust how it is shown and can be ignored.
	// PRAGMA: On github.com/kainoaseto/go-cloud, add a blank import: _ "github.com/kainoaseto/go-cloud/pubsub/mempubsub"
	// PRAGMA: On github.com/kainoaseto/go-cloud, hide lines until the next blank line.
	ctx := context.Background()

	topic, err := pubsub.OpenTopic(ctx, "mem://topicA")
	if err != nil {
		log.Fatal(err)
	}
	defer topic.Shutdown(ctx)
}
Output:

Index

Examples

Constants

View Source
const Scheme = "mem"

Scheme is the URL scheme mempubsub registers its URLOpeners under on pubsub.DefaultMux.

Variables

This section is empty.

Functions

func NewSubscription

func NewSubscription(pstopic *pubsub.Topic, ackDeadline time.Duration) *pubsub.Subscription

NewSubscription creates a new subscription for the given topic. It panics if the given topic did not come from mempubsub. If a message is not acked within in the given ack deadline from when it is received, then it will be redelivered.

Example
package main

import (
	"context"
	"time"

	"github.com/kainoaseto/go-cloud/pubsub/mempubsub"
)

func main() {
	// PRAGMA: This example is used on github.com/kainoaseto/go-cloud; PRAGMA comments adjust how it is shown and can be ignored.
	// PRAGMA: On github.com/kainoaseto/go-cloud, hide lines until the next blank line.
	ctx := context.Background()

	// Construct a *pubsub.Topic.
	topic := mempubsub.NewTopic()
	defer topic.Shutdown(ctx)

	// Construct a *pubsub.Subscription for the topic.
	subscription := mempubsub.NewSubscription(topic, 1*time.Minute /* ack deadline */)
	defer subscription.Shutdown(ctx)
}
Output:

func NewTopic

func NewTopic() *pubsub.Topic

NewTopic creates a new in-memory topic.

Example
package main

import (
	"context"

	"github.com/kainoaseto/go-cloud/pubsub/mempubsub"
)

func main() {
	// PRAGMA: This example is used on github.com/kainoaseto/go-cloud; PRAGMA comments adjust how it is shown and can be ignored.
	// PRAGMA: On github.com/kainoaseto/go-cloud, hide lines until the next blank line.
	ctx := context.Background()

	topic := mempubsub.NewTopic()
	defer topic.Shutdown(ctx)
}
Output:

Types

type URLOpener

type URLOpener struct {
	// contains filtered or unexported fields
}

URLOpener opens mempubsub URLs like "mem://topic".

The URL's host+path is used as the topic to create or subscribe to.

Query parameters:

  • ackdeadline: The ack deadline for OpenSubscription, in time.ParseDuration formats. Defaults to 1m.

func (*URLOpener) OpenSubscriptionURL

func (o *URLOpener) OpenSubscriptionURL(ctx context.Context, u *url.URL) (*pubsub.Subscription, error)

OpenSubscriptionURL opens a pubsub.Subscription based on u.

func (*URLOpener) OpenTopicURL

func (o *URLOpener) OpenTopicURL(ctx context.Context, u *url.URL) (*pubsub.Topic, error)

OpenTopicURL opens a pubsub.Topic based on u.

Jump to

Keyboard shortcuts

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