-
Notifications
You must be signed in to change notification settings - Fork 0
/
broker.go
35 lines (29 loc) · 1.31 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
package mq
import "errors"
// Broker is an interface that represents an underlying cloud message broker.
type Broker interface {
// CreateTopic creates a new topic.
// This is an idempotent call and returns no error if the topic already exists.
CreateTopic(topicID string) error
// CreateSubscription creates a new subscription to the topic specified in options.
// This is an idempotent call and returns no error if a subscription with the same id already exists,
// provided that the topic and other parameters are the same.
CreateSubscription(subscriptionID string, options *SubscriptionOptions) error
// Publish publishes a message to a topic.
Publish(topicID string, message *Message) error
// Consume consumes messages from a subscription
// and passes them on to the handler function.
// This is a blocking function and doesn't return until it encounters a network error.
Consume(subscriptionID string, handler func(*Message) error, options *ConsumerOptions) error
}
// NewBroker returns a new broker configured for an underlying cloud provider.
func NewBroker(config *Config) (Broker, error) {
switch config.Provider {
case ProviderAWS:
return newAwsBroker(config.AWSRegion)
case ProviderGCP:
return newGcloudBroker(config.GCloudProject)
default:
return nil, errors.New("unrecognized provider")
}
}