pubsub

package
v10.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2026 License: AGPL-3.0 Imports: 19 Imported by: 0

Documentation

Overview

Package pubsub is a messagequeue publisher and consumer over Google Cloud Pub/Sub.

Both providers take an already-built *pubsub.Client rather than credentials, so authentication, the emulator, and any client-level tuning are the caller's to arrange and this package inherits whatever they set up.

Delivery

At-least-once. A handler that succeeds acks; one that returns an error nacks, and what happens next belongs to the subscription rather than to this code — its retry policy, its backoff, its dead-letter topic. Handlers must be idempotent; see the idempotency package. The delivery attempt count, when Pub/Sub supplies one, goes on the span.

Publish is synchronous despite the client batching underneath: it blocks until the server has assigned a message ID, and returns that failure to the caller rather than deferring it to a background flush. Nothing here retries beyond what the client does internally.

Ordering

messagequeue.WithOrderingKey becomes the message's OrderingKey, and every publisher this package builds is created with the client's EnableMessageOrdering set — without it the client rejects a keyed message locally, before it reaches the wire. Turning it on costs unkeyed publishes nothing: those bundle under the empty key, which the client schedules exactly as it does with ordering off.

The publishing half is all this package can set. Ordered *delivery* also requires the subscription to have been created with message ordering enabled, which belongs to whatever provisions the subscription — the same place the subscription itself comes from. A key published to a subscription without it is carried and stored, and delivered in whatever order the subscription likes.

A publish that fails on an ordering key pauses that key in the client: every later message for it is refused until ResumePublish. This package resumes the key itself on a failed publish. That guard exists for the client's asynchronous API, where messages are already queued behind the failing one; here Publish blocks and hands the error back, so nothing is queued and the caller is the one deciding what happens next — and leaving the key paused would turn one transient failure into a key that rejects everything with no explanation.

messagequeue.WithDeduplicationKey is accepted and ignored. Pub/Sub does not deduplicate on a caller-supplied key.

Names, and what must already exist

A short topic name is qualified to projects/{project}/topics/{name}; a name that already starts with "projects/" is used as given. The publisher reaches the topic directly and never calls GetTopic, which keeps the required IAM down to pubsub.topics.publish — a service that only publishes does not need pubsub.topics.get.

The consumer derives its subscription from the topic by substitution: projects/{project}/subscriptions/{name}. The subscription must already exist and must be named after its topic. Nothing here creates one, and a mismatch surfaces at Consume time as a GetSubscription failure on the error channel rather than at construction. Topic and subscription are provisioned by whatever manages the project's infrastructure.

Lifecycle

Ping is a no-op — Pub/Sub is a managed service and there is no endpoint worth probing. Close on either provider closes the shared client, which is the one resource either holds.

The consumer provider caches by topic and returns the cached consumer for a repeat NewConsumer, rather than the ErrConsumerAlreadyRegistered its kafka, redis, and sqs siblings return.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	ProjectID string `env:"PROJECT_ID" json:"projectID,omitempty" yaml:"projectID,omitempty"`
}

Config configures a PubSub-backed pubSubConsumer.

func (*Config) ValidateWithContext

func (cfg *Config) ValidateWithContext(ctx context.Context) error

ValidateWithContext validates a Config struct.

The project ID is required because the Pub/Sub client cannot be built without one — it was already the difference between a working consumer and a construction error, and saying so here is what moves the report to startup.

type ConsumerProvider

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

ConsumerProvider is the Google Cloud Pub/Sub messagequeue.ConsumerProvider implementation. It is exported, and returned by NewPubSubConsumerProvider, so a caller who has chosen Google Cloud Pub/Sub can depend on that choice rather than on the interface every broker shares.

func NewPubSubConsumerProvider

func NewPubSubConsumerProvider(client *pubsub.Client, opts ...Option) *ConsumerProvider

NewPubSubConsumerProvider returns a ConsumerProvider for a given address.

func (*ConsumerProvider) Close

func (p *ConsumerProvider) Close()

Close closes the connection topic.

func (*ConsumerProvider) NewConsumer

func (p *ConsumerProvider) NewConsumer(_ context.Context, topic string, handlerFunc messagequeue.ConsumerFunc) (messagequeue.Consumer, error)

NewConsumer returns a pubSubConsumer for a given topic.

type Option

type Option func(*options)

Option configures the providers this package constructs. The zero configuration works: absent observability deps are normalized downstream.

func WithLogger

func WithLogger(logger logging.Logger) Option

WithLogger attaches a logger.

func WithMetricsProvider

func WithMetricsProvider(metricsProvider metrics.Provider) Option

WithMetricsProvider attaches a metrics provider.

func WithTracerProvider

func WithTracerProvider(tracerProvider tracing.Provider) Option

WithTracerProvider attaches a tracer provider.

type PublisherProvider

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

PublisherProvider is the Google Cloud Pub/Sub messagequeue.PublisherProvider implementation. It is exported, and returned by NewPubSubPublisherProvider, so a caller who has chosen Google Cloud Pub/Sub can depend on that choice rather than on the interface every broker shares.

func NewPubSubPublisherProvider

func NewPubSubPublisherProvider(client *pubsub.Client, projectID string, opts ...Option) *PublisherProvider

NewPubSubPublisherProvider returns a PublisherProvider for a given address.

func (*PublisherProvider) Close

func (p *PublisherProvider) Close()

Close closes the connection topic.

func (*PublisherProvider) NewPublisher

func (p *PublisherProvider) NewPublisher(ctx context.Context, topicName string) (messagequeue.Publisher, error)

NewPublisher returns a pubSubPublisher for a given topic.

func (*PublisherProvider) Ping

Ping is a no-op for GCP Pub/Sub (managed service).

Jump to

Keyboard shortcuts

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