/
broker.go
executable file
·42 lines (32 loc) · 1.2 KB
/
broker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package broker
import (
"context"
"cloud.google.com/go/pubsub"
)
// Broker is an interface used for asynchronous messaging.
type Broker interface {
NewPublisher(topic string, opts ...PublishOption) (pub Publisher, err error)
AddSubscriber(subscription string, hdlr Handler, opts ...SubscribeOption) (err error)
Start() error
}
type Publisher interface {
Publish(ctx context.Context, msg *pubsub.Message) error
}
// Handler is used to process messages via a subscription of a topic.
// The handler is passed a publication interface which contains the
// message and optional Ack method to acknowledge receipt of the message.
type Handler func(context.Context, *pubsub.Message)
var DefaultBroker Broker
// NewBroker creates and returns a new Broker based on the packages within.
func NewBroker(ctx context.Context, opts ...Option) Broker {
return newBroker(ctx, opts...)
}
func Start() error {
return DefaultBroker.Start()
}
func NewPublisher(topic string, opts ...PublishOption) (Publisher, error) {
return DefaultBroker.NewPublisher(topic, opts...)
}
func AddSubscriber(subscription string, handler Handler, opts ...SubscribeOption) error {
return DefaultBroker.AddSubscriber(subscription, handler, opts...)
}