import "go.chromium.org/luci/common/gcloud/pubsub"
publisher.go quota.go resource.go scopes.go subscription.go topic.go
const ( // MaxPublishRequestBytes is the maximum size of a single publish request in // bytes, as determined by the PubSub service. // // See: https://cloud.google.com/pubsub/publisher MaxPublishRequestBytes = 1e7 // MaxPublishRequestCount is the maximum PubSub batch size. MaxPublishRequestCount = pubsub.MaxPublishRequestCount // MaxProjectMessagesPerSecond is the maximum number of requests per second, // across the entire project. MaxProjectMessagesPerSecond = 10000 // MaxACKDeadline is the maximum acknowledgement deadline that can be applied // to a leased subscription Message. MaxACKDeadline = 600 * time.Second )
Cloud PubSub quota is documented here: https://cloud.google.com/pubsub/quotas
var ( // PublisherScopes is the set of OAuth2 scopes needed for a publisher to // publish messages. PublisherScopes = []string{ pubsub.ScopePubSub, } // SubscriberScopes is the set of OAuth2 scopes needed for a subscriber to // pull and acknowledge messages. SubscriberScopes = []string{ pubsub.ScopePubSub, } )
type ClientFactory interface { // Client returns the Pub/Sub publisher client to use. // Client will be closed when this UnbufferedPublisher is closed. Client(context.Context) (*vkit.PublisherClient, error) // RecreateClient is called if any publish calls fail. // This is used to tell the underlying service to maybe generate a new client. RecreateClient() }
ClientFactory is passed into an UnbufferedPublisher to create or reset a client.
type Publisher interface { Publish(c context.Context, msgs ...*pubsub.Message) ([]string, error) Close() error }
Publisher is a generic interface to something that can publish Pub/Sub messages.
A Publisher should be Closed when finished with it.
Subscription is a Pub/Sub subscription name.
func NewSubscription(project, name string) Subscription
NewSubscription generates a new Subscription for a given project and subscription name.
func (s *Subscription) Set(value string) error
Set implements flag.Value.
func (s Subscription) Split() (p, n string)
Split returns the Subscription's project component. If no project is defined (malformed), an empty string will be returned.
func (s Subscription) SplitErr() (p, n string, err error)
SplitErr returns the Subscription's project and name components.
func (s *Subscription) String() string
func (s Subscription) Validate() error
Validate returns an error if the subscription name is invalid.
Topic is a fully-qualified Pub/Sub project/topic name.
NewTopic generates a new Topic for a given project and topic name.
Set implements flag.Value.
Split returns the Topic's project component. If no project is defined (malformed), an empty string will be returned.
SplitErr returns the Topic's project and name components.
Validate returns an error if the topic name is invalid.
type UnbufferedPublisher struct { // AECtx is the AppEngine context used to create a pubsub client. AECtx context.Context // Topic is the name of the Topic to publish to. Topic Topic // ClientFactory produces a client for the publisher. This is called on each // and every publish request. If a publish request fails, then RecreateClient is called. ClientFactory ClientFactory // CallOpts are arbitrary call options that will be passed to the Publish // call. CallOpts []gax.CallOption }
UnbufferedPublisher directly instantiates a Pub/Sub client and publishes a message to it.
The standard Pub/Sub library has several issues, especially when used from AppEngine:
- It uses an empty Context, discarding AppEngine context. - It uses a buffer, which expects a lifecycle beyond that of a simple AppEngine Request.
func (up *UnbufferedPublisher) Close() error
Close closes the UnbufferedPublisher, notably its Client.
func (up *UnbufferedPublisher) Publish(c context.Context, msgs ...*pubsub.Message) ([]string, error)
Publish publishes a message immediately, blocking until it completes.
"c" must be an AppEngine context.
Package pubsub imports 12 packages (graph) and is imported by 11 packages. Updated 2021-01-25. Refresh now. Tools for package owners.