gcppubsub

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package gcppubsub is the Google Cloud Pub/Sub broker of the pubsub package:

ps, err := pubsub.ForApp(app, gcppubsub.Driver())

Settings: PUBSUB_DRIVER=gcp, PUBSUB_GCP_PROJECT (the project's ID), and PUBSUB_GCP_CREATE=true to create missing topics and subscriptions (for development and the emulator; in production, create them with your infrastructure tools). Credentials come from Application Default Credentials; PUBSUB_EMULATOR_HOST selects the emulator.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Driver

func Driver(opts ...option.ClientOption) pubsub.Driver

Driver is the broker's driver (PUBSUB_DRIVER=gcp). opts configure the client (tests pass option.WithGRPCConn for a fake server).

Types

type Broker

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

Broker is a pub/sub broker on Google Cloud Pub/Sub. Topic and subscription IDs are the names with the prefix, with "%" and the characters Pub/Sub doesn't allow in IDs (such as ":") escaped as "%XX".

The client extends ack deadlines while a message is handled, up to the listener's ack timeout. A failed message is nacked: Pub/Sub delivers it again as the subscription's retry policy says (the listener's Backoff doesn't apply). Message.Attempt is set only for subscriptions with a dead-letter policy; without one, the listener's MaxAttempts can't apply.

func NewBroker

func NewBroker(client *gpubsub.Client, prefix string, create bool) *Broker

NewBroker returns a broker using client, with IDs starting with prefix; with create, it creates missing topics and subscriptions. Closing the broker closes the client.

func (*Broker) Close

func (b *Broker) Close() error

Close implements pubsub.Broker: it stops the publishers and closes the client.

func (*Broker) Prepare

func (b *Broker) Prepare(ctx context.Context, s pubsub.SubscriptionSpec) error

Prepare implements pubsub.Broker. With create, it creates the subscription (and its topic) if it doesn't exist, with the ack deadline of the listener's ack timeout (10s to 600s) and a retry policy backing off from 10s to 600s; that needs the pubsub.subscriptions.get and .create and pubsub.topics.create permissions. Without create, it only checks the names: a missing subscription shows when the listener starts.

func (*Broker) Publish

func (b *Broker) Publish(ctx context.Context, topic string, m pubsub.Outgoing) (string, error)

Publish implements pubsub.Broker. With create, a missing topic is created.

func (*Broker) Subscribe

func (b *Broker) Subscribe(ctx context.Context, s pubsub.SubscriptionSpec, handle func(ctx context.Context, m *pubsub.Message) pubsub.Outcome) error

Subscribe implements pubsub.Broker.

type Config

type Config struct {
	// Project is the Google Cloud project's ID. PUBSUB_GCP_PROJECT.
	Project string `env:"PUBSUB_GCP_PROJECT"`
	// Create creates missing topics and subscriptions. PUBSUB_GCP_CREATE,
	// default false.
	Create bool `env:"PUBSUB_GCP_CREATE" default:"false"`
}

Config is the broker's settings.

Jump to

Keyboard shortcuts

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