Package pubsub
Overview ▹
Index ▹
Constants
const ( // ScopePubSub grants permissions to view and manage Pub/Sub // topics and subscriptions. ScopePubSub = "https://www.googleapis.com/auth/pubsub" // ScopeCloudPlatform grants permissions to view and manage your data // across Google Cloud Platform services. ScopeCloudPlatform = "https://www.googleapis.com/auth/cloud-platform" )
const ( // The maximum number of messages that can be in a single publish request, as // determined by the PubSub service. MaxPublishRequestCount = 1000 // The maximum size of a single publish request in bytes, as determined by the PubSub service. MaxPublishRequestBytes = 1e7 )
Variables
DefaultPublishSettings holds the default values for topics' PublishSettings.
var DefaultPublishSettings = PublishSettings{ DelayThreshold: 1 * time.Millisecond, CountThreshold: 100, ByteThreshold: 1e6, Timeout: 60 * time.Second, }
DefaultReceiveSettings holds the default values for ReceiveSettings.
var DefaultReceiveSettings = ReceiveSettings{ MaxExtension: 10 * time.Minute, MaxOutstandingMessages: 1000, MaxOutstandingBytes: 1e9, }
ErrOversizedMessage indicates that a message's size exceeds MaxPublishRequestBytes.
var ErrOversizedMessage = bundler.ErrOversizedItem
type Client ¶
Client is a Google Pub/Sub client scoped to a single project.
Clients should be reused rather than being created as needed. A Client may be shared by multiple goroutines.
type Client struct {
// contains filtered or unexported fields
}
func NewClient ¶
func NewClient(ctx context.Context, projectID string, opts ...option.ClientOption) (*Client, error)
NewClient creates a new PubSub client.
▹ Example
func (*Client) Close ¶
func (c *Client) Close() error
Close closes any resources held by the client.
Close need not be called at program exit.
func (*Client) CreateSubscription ¶
func (c *Client) CreateSubscription(ctx context.Context, id string, cfg SubscriptionConfig) (*Subscription, error)
CreateSubscription creates a new subscription on a topic.
id is the name of the subscription to create. It must start with a letter, and contain only letters ([A-Za-z]), numbers ([0-9]), dashes (-), underscores (_), periods (.), tildes (~), plus (+) or percent signs (%). It must be between 3 and 255 characters in length, and must not start with "goog".
cfg.Topic is the topic from which the subscription should receive messages. It need not belong to the same project as the subscription. This field is required.
cfg.AckDeadline is the maximum time after a subscriber receives a message before the subscriber should acknowledge the message. It must be between 10 and 600 seconds (inclusive), and is rounded down to the nearest second. If the provided ackDeadline is 0, then the default value of 10 seconds is used. Note: messages which are obtained via Subscription.Receive need not be acknowledged within this deadline, as the deadline will be automatically extended.
cfg.PushConfig may be set to configure this subscription for push delivery.
If the subscription already exists an error will be returned.
▹ Example
func (*Client) CreateTopic ¶
func (c *Client) CreateTopic(ctx context.Context, id string) (*Topic, error)
CreateTopic creates a new topic. The specified topic ID must start with a letter, and contain only letters ([A-Za-z]), numbers ([0-9]), dashes (-), underscores (_), periods (.), tildes (~), plus (+) or percent signs (%). It must be between 3 and 255 characters in length, and must not start with "goog". If the topic already exists an error will be returned.
▹ Example
func (*Client) Subscription ¶
func (c *Client) Subscription(id string) *Subscription
Subscription creates a reference to a subscription.
func (*Client) Subscriptions ¶
func (c *Client) Subscriptions(ctx context.Context) *SubscriptionIterator
Subscriptions returns an iterator which returns all of the subscriptions for the client's project.
▹ Example
func (*Client) Topic ¶
func (c *Client) Topic(id string) *Topic
Topic creates a reference to a topic.
If a Topic's Publish method is called, it has background goroutines associated with it. Clean them up by calling Topic.Stop.
Avoid creating many Topic instances if you use them to publish.
func (*Client) Topics ¶
func (c *Client) Topics(ctx context.Context) *TopicIterator
Topics returns an iterator which returns all of the topics for the client's project.
▹ Example
type Message ¶
Message represents a Pub/Sub message.
type Message struct { // ID identifies this message. // This ID is assigned by the server and is populated for Messages obtained from a subscription. // This field is read-only. ID string // Data is the actual data in the message. Data []byte // Attributes represents the key-value pairs the current message // is labelled with. Attributes map[string]string // The time at which the message was published. // This is populated by the server for Messages obtained from a subscription. // This field is read-only. PublishTime time.Time // contains filtered or unexported fields }
func (*Message) Ack ¶
func (m *Message) Ack()
Ack indicates successful processing of a Message passed to the Subscriber.Receive callback. It should not be called on any other Message value. If message acknowledgement fails, the Message will be redelivered. Client code must call Ack or Nack when finished for each received Message. Calls to Ack or Nack have no effect after the first call.
func (*Message) Nack ¶
func (m *Message) Nack()
Nack indicates that the client will not or cannot process a Message passed to the Subscriber.Receive callback. It should not be called on any other Message value. Nack will result in the Message being redelivered more quickly than if it were allowed to expire. Client code must call Ack or Nack when finished for each received Message. Calls to Ack or Nack have no effect after the first call.
type PublishResult ¶
A PublishResult holds the result from a call to Publish.
type PublishResult struct {
// contains filtered or unexported fields
}
func (*PublishResult) Get ¶
func (r *PublishResult) Get(ctx context.Context) (serverID string, err error)
Get returns the server-generated message ID and/or error result of a Publish call. Get blocks until the Publish call completes or the context is done.
func (*PublishResult) Ready ¶
func (r *PublishResult) Ready() <-chan struct{}
Ready returns a channel that is closed when the result is ready. When the Ready channel is closed, Get is guaranteed not to block.
type PublishSettings ¶
PublishSettings control the bundling of published messages.
type PublishSettings struct { // Publish a non-empty batch after this delay has passed. DelayThreshold time.Duration // Publish a batch when it has this many messages. The maximum is // MaxPublishRequestCount. CountThreshold int // Publish a batch when its size in bytes reaches this value. ByteThreshold int // The number of goroutines that invoke the Publish RPC concurrently. // Defaults to a multiple of GOMAXPROCS. NumGoroutines int // The maximum time that the client will attempt to publish a bundle of messages. Timeout time.Duration }
type PushConfig ¶
PushConfig contains configuration for subscriptions that operate in push mode.
type PushConfig struct { // A URL locating the endpoint to which messages should be pushed. Endpoint string // Endpoint configuration attributes. See https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions#pushconfig for more details. Attributes map[string]string }
type ReceiveSettings ¶
ReceiveSettings configure the Receive method. A zero ReceiveSettings will result in values equivalent to DefaultReceiveSettings.
type ReceiveSettings struct { // MaxExtension is the maximum period for which the Subscription should // automatically extend the ack deadline for each message. // // The Subscription will automatically extend the ack deadline of all // fetched Messages for the duration specified. Automatic deadline // extension may be disabled by specifying a duration less than 1. MaxExtension time.Duration // MaxOutstandingMessages is the maximum number of unprocessed messages // (unacknowledged but not yet expired). If MaxOutstandingMessages is 0, it // will be treated as if it were DefaultReceiveSettings.MaxOutstandingMessages. // If the value is negative, then there will be no limit on the number of // unprocessed messages. MaxOutstandingMessages int // MaxOutstandingBytes is the maximum size of unprocessed messages // (unacknowledged but not yet expired). If MaxOutstandingBytes is 0, it will // be treated as if it were DefaultReceiveSettings.MaxOutstandingBytes. If // the value is negative, then there will be no limit on the number of bytes // for unprocessed messages. MaxOutstandingBytes int }
type Subscription ¶
Subscription is a reference to a PubSub subscription.
type Subscription struct { // Settings for pulling messages. Configure these before calling Receive. ReceiveSettings ReceiveSettings // contains filtered or unexported fields }
func (*Subscription) Config ¶
func (s *Subscription) Config(ctx context.Context) (SubscriptionConfig, error)
Config fetches the current configuration for the subscription.
▹ Example
func (*Subscription) Delete ¶
func (s *Subscription) Delete(ctx context.Context) error
Delete deletes the subscription.
▹ Example
func (*Subscription) Exists ¶
func (s *Subscription) Exists(ctx context.Context) (bool, error)
Exists reports whether the subscription exists on the server.
▹ Example
func (*Subscription) IAM ¶
func (s *Subscription) IAM() *iam.Handle
func (*Subscription) ID ¶
func (s *Subscription) ID() string
ID returns the unique identifier of the subscription within its project.
func (*Subscription) ModifyPushConfig ¶
func (s *Subscription) ModifyPushConfig(ctx context.Context, conf PushConfig) error
ModifyPushConfig updates the endpoint URL and other attributes of a push subscription.
▹ Example
func (*Subscription) Receive ¶
func (s *Subscription) Receive(ctx context.Context, f func(context.Context, *Message)) error
Receive calls f with the outstanding messages from the subscription. It blocks until ctx is done, or the service returns a non-retryable error.
The standard way to terminate a Receive is to cancel its context:
cctx, cancel := context.WithCancel(ctx) err := sub.Receive(cctx, callback) // Call cancel from callback, or another goroutine.
If the service returns a non-retryable error, Receive returns that error after all of the outstanding calls to f have returned. If ctx is done, Receive returns either nil after all of the outstanding calls to f have returned and all messages have been acknowledged or have expired.
Receive calls f concurrently from multiple goroutines. It is encouraged to process messages synchronously in f, even if that processing is relatively time-consuming; Receive will spawn new goroutines for incoming messages, limited by MaxOutstandingMessages and MaxOutstandingBytes in ReceiveSettings.
The context passed to f will be canceled when ctx is Done or there is a fatal service error.
Receive will automatically extend the ack deadline of all fetched Messages for the period specified by s.ReceiveSettings.MaxExtension.
Each Subscription may have only one invocation of Receive active at a time.
▹ Example
▹ Example (MaxExtension)
▹ Example (MaxOutstanding)
func (*Subscription) String ¶
func (s *Subscription) String() string
String returns the globally unique printable name of the subscription.
type SubscriptionConfig ¶
Subscription config contains the configuration of a subscription.
type SubscriptionConfig struct { Topic *Topic PushConfig PushConfig // The default maximum time after a subscriber receives a message before // the subscriber should acknowledge the message. Note: messages which are // obtained via Subscription.Receive need not be acknowledged within this // deadline, as the deadline will be automatically extended. AckDeadline time.Duration // contains filtered or unexported fields }
type SubscriptionIterator ¶
SubscriptionIterator is an iterator that returns a series of subscriptions.
type SubscriptionIterator struct {
// contains filtered or unexported fields
}
func (*SubscriptionIterator) Next ¶
func (subs *SubscriptionIterator) Next() (*Subscription, error)
Next returns the next subscription. If there are no more subscriptions, iterator.Done will be returned.
▹ Example
type Topic ¶
Topic is a reference to a PubSub topic.
The methods of Topic are safe for use by multiple goroutines.
type Topic struct { // Settings for publishing messages. All changes must be made before the // first call to Publish. The default is DefaultPublishSettings. PublishSettings PublishSettings // contains filtered or unexported fields }
func (*Topic) Delete ¶
func (t *Topic) Delete(ctx context.Context) error
Delete deletes the topic.
▹ Example
func (*Topic) Exists ¶
func (t *Topic) Exists(ctx context.Context) (bool, error)
Exists reports whether the topic exists on the server.
▹ Example
func (*Topic) IAM ¶
func (t *Topic) IAM() *iam.Handle
func (*Topic) ID ¶
func (t *Topic) ID() string
ID returns the unique idenfier of the topic within its project.
func (*Topic) Publish ¶
func (t *Topic) Publish(ctx context.Context, msg *Message) *PublishResult
Publish publishes msg to the topic asynchronously. Messages are batched and sent according to the topic's PublishSettings. Publish never blocks.
Publish returns a non-nil PublishResult which will be ready when the message has been sent (or has failed to be sent) to the server.
Publish creates goroutines for batching and sending messages. These goroutines need to be stopped by calling t.Stop(). Once stopped, future calls to Publish will immediately return a PublishResult with an error.
▹ Example
func (*Topic) Stop ¶
func (t *Topic) Stop()
Send all remaining published messages and stop goroutines created for handling publishing. Returns once all outstanding messages have been sent or have failed to be sent.
func (*Topic) String ¶
func (t *Topic) String() string
String returns the printable globally unique name for the topic.
func (*Topic) Subscriptions ¶
func (t *Topic) Subscriptions(ctx context.Context) *SubscriptionIterator
Subscriptions returns an iterator which returns the subscriptions for this topic.
▹ Example
type TopicIterator ¶
TopicIterator is an iterator that returns a series of topics.
type TopicIterator struct {
// contains filtered or unexported fields
}
func (*TopicIterator) Next ¶
func (tps *TopicIterator) Next() (*Topic, error)
Next returns the next topic. If there are no more topics, iterator.Done will be returned.
▹ Example
Subdirectories
Name | Synopsis |
---|---|
.. | |
apiv1 | Package pubsub is an experimental, auto-generated package for the pubsub API. |
loadtest | Package loadtest implements load testing for pubsub, following the interface defined in https://github.com/GoogleCloudPlatform/pubsub/tree/master/load-test-framework/ . |
cmd | |
pb | Package google_pubsub_loadtest is a generated protocol buffer package. |