gocloud.devgocloud.dev/pubsub/gcppubsub Index | Examples | Files

package gcppubsub

import "gocloud.dev/pubsub/gcppubsub"

Package gcppubsub provides a pubsub implementation that uses GCP PubSub. Use OpenTopic to construct a *pubsub.Topic, and/or OpenSubscription to construct a *pubsub.Subscription.

URLs

For pubsub.OpenTopic and pubsub.OpenSubscription, gcppubsub registers for the scheme "gcppubsub". The default URL opener will creating a connection using use default credentials from the environment, as described in https://cloud.google.com/docs/authentication/production. To customize the URL opener, or for more details on the URL format, see URLOpener. See https://godoc.org/gocloud.dev#hdr-URLs for background information.

As

gcppubsub exposes the following types for As:

Example (OpenfromURL)

Code:play 

package main

import (
	"context"

	"gocloud.dev/pubsub"
)

func main() {
	ctx := context.Background()

	// OpenTopic creates a *pubsub.Topic from a URL.
	// This URL will open the topic "mytopic" in the project "myproject"
	// using default credentials.
	t, err := pubsub.OpenTopic(ctx, "gcppubsub://myproject/mytopic")

	// Similarly, OpenSubscription creates a *pubsub.Subscription from a URL.
	// This URL will open the subscription "mysubscription" in the project
	// "myproject" using default credentials.
	s, err := pubsub.OpenSubscription(ctx, "gcppubsub://myproject/mysubscription")
	_, _, _ = t, s, err
}

Index

Examples

Constants

const Scheme = "gcppubsub"

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

Functions

func Dial

func Dial(ctx context.Context, ts gcp.TokenSource) (*grpc.ClientConn, func(), error)

Dial opens a gRPC connection to the GCP Pub Sub API.

The second return value is a function that can be called to clean up the connection opened by Dial.

func OpenSubscription

func OpenSubscription(client *raw.SubscriberClient, proj gcp.ProjectID, subscriptionName string, opts *SubscriptionOptions) *pubsub.Subscription

OpenSubscription returns a *pubsub.Subscription backed by an existing GCP PubSub subscription subscriptionName in the given projectID. See the package documentation for an example.

Example

Code:play 

package main

import (
	"context"
	"log"

	"gocloud.dev/gcp"
	"gocloud.dev/pubsub/gcppubsub"
)

func main() {
	// Your GCP credentials.
	// See https://cloud.google.com/docs/authentication/production
	// for more info on alternatives.
	ctx := context.Background()
	creds, err := gcp.DefaultCredentials(ctx)
	if err != nil {
		log.Fatal(err)
	}
	// Get the ProjectID from the credentials (it's required by OpenTopic).
	projID, err := gcp.DefaultProjectID(creds)
	if err != nil {
		log.Fatal(err)
	}

	// Open a gRPC connection to the GCP Pub Sub API.
	conn, cleanup, err := gcppubsub.Dial(ctx, creds.TokenSource)
	if err != nil {
		log.Fatal(err)
	}
	defer cleanup()

	// Construct a SubscriberClient using the connection.
	subClient, err := gcppubsub.SubscriberClient(ctx, conn)
	if err != nil {
		log.Fatal(err)
	}
	defer subClient.Close()

	// Construct a *pubsub.Subscription.
	s := gcppubsub.OpenSubscription(subClient, projID, "example-subscription", nil)
	defer s.Shutdown(ctx)

	// Now we can use s to receive messages.
	msg, err := s.Receive(ctx)
	if err != nil {
		// Handle error....
	}
	// Handle message....
	msg.Ack()
}

func OpenTopic

func OpenTopic(client *raw.PublisherClient, proj gcp.ProjectID, topicName string, opts *TopicOptions) *pubsub.Topic

OpenTopic returns a *pubsub.Topic backed by an existing GCP PubSub topic topicName in the given projectID. See the package documentation for an example.

Example

Code:play 

package main

import (
	"context"
	"log"

	"gocloud.dev/gcp"
	"gocloud.dev/pubsub"
	"gocloud.dev/pubsub/gcppubsub"
)

func main() {
	// Your GCP credentials.
	// See https://cloud.google.com/docs/authentication/production
	// for more info on alternatives.
	ctx := context.Background()
	creds, err := gcp.DefaultCredentials(ctx)
	if err != nil {
		log.Fatal(err)
	}
	// Get the ProjectID from the credentials (it's required by OpenTopic).
	projID, err := gcp.DefaultProjectID(creds)
	if err != nil {
		log.Fatal(err)
	}

	// Open a gRPC connection to the GCP Pub Sub API.
	conn, cleanup, err := gcppubsub.Dial(ctx, creds.TokenSource)
	if err != nil {
		log.Fatal(err)
	}
	defer cleanup()

	// Construct a PublisherClient using the connection.
	pubClient, err := gcppubsub.PublisherClient(ctx, conn)
	if err != nil {
		log.Fatal(err)
	}
	defer pubClient.Close()

	// Construct a *pubsub.Topic.
	t := gcppubsub.OpenTopic(pubClient, projID, "example-topic", nil)
	defer t.Shutdown(ctx)

	// Now we can use t to send messages.
	err = t.Send(ctx, &pubsub.Message{Body: []byte("example message")})
}

func PublisherClient

func PublisherClient(ctx context.Context, conn *grpc.ClientConn) (*raw.PublisherClient, error)

PublisherClient returns a *raw.PublisherClient that can be used in OpenTopic.

func SubscriberClient

func SubscriberClient(ctx context.Context, conn *grpc.ClientConn) (*raw.SubscriberClient, error)

SubscriberClient returns a *raw.SubscriberClient that can be used in OpenSubscription.

Types

type SubscriptionOptions

type SubscriptionOptions struct{}

SubscriptionOptions will contain configuration for subscriptions.

type TopicOptions

type TopicOptions struct{}

TopicOptions will contain configuration for topics.

type URLOpener

type URLOpener struct {
	// Conn must be set to a non-nil ClientConn authenticated with
	// Cloud Pub/Sub scope or equivalent.
	Conn *grpc.ClientConn

	// TopicOptions specifies the options to pass to OpenTopic.
	TopicOptions TopicOptions
	// SubscriptionOptions specifies the options to pass to OpenSubscription.
	SubscriptionOptions SubscriptionOptions
}

URLOpener opens GCP Pub/Sub URLs like "gcppubsub://myproject/mytopic" for topics or "gcppubsub://myproject/mysub" for subscriptions.

The URL's host is used as the projectID, and the URL's path (with the leading "/" trimmed) is used as the topic or subscription name.

No URL parameters are supported.

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.

Source Files

gcppubsub.go

Version
v0.12.0
Published
Mar 20, 2019
Platform
js/wasm
Imports
18 packages
Last checked
21 hours ago

Tools for package owners.