-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublisher.go
59 lines (48 loc) · 1.51 KB
/
publisher.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package fxgcppubsub
import (
"context"
"fmt"
"cloud.google.com/go/pubsub"
"github.com/ankorstore/yokai-contrib/fxgcppubsub/topic"
)
var _ Publisher = (*DefaultPublisher)(nil)
// Publisher is the interface for high level publishers.
type Publisher interface {
Publish(ctx context.Context, topicID string, data any, options ...topic.PublishOption) (*pubsub.PublishResult, error)
Stop()
}
// DefaultPublisher is the default Publisher implementation.
type DefaultPublisher struct {
factory topic.TopicFactory
registry topic.TopicRegistry
}
// NewDefaultPublisher returns a new DefaultPublisher instance.
func NewDefaultPublisher(factory topic.TopicFactory, registry topic.TopicRegistry) *DefaultPublisher {
return &DefaultPublisher{
factory: factory,
registry: registry,
}
}
// Publish publishes data, with options, on a given topicID.
func (p *DefaultPublisher) Publish(ctx context.Context, topicID string, data any, options ...topic.PublishOption) (*pubsub.PublishResult, error) {
// retrieve topic
if !p.registry.Has(topicID) {
top, err := p.factory.Create(ctx, topicID)
if err != nil {
return nil, fmt.Errorf("cannot create topic: %w", err)
}
p.registry.Add(top)
}
top, err := p.registry.Get(topicID)
if err != nil {
return nil, fmt.Errorf("cannot get topic: %w", err)
}
// publish
return top.WithOptions(options...).Publish(ctx, data)
}
// Stop stops gracefully all internal publishers.
func (p *DefaultPublisher) Stop() {
for _, top := range p.registry.All() {
top.BaseTopic().Stop()
}
}