This repository has been archived by the owner on Jul 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
publish.go
142 lines (117 loc) · 3.46 KB
/
publish.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package bps
import (
"context"
"fmt"
"net/url"
"sync"
)
var (
pubReg = map[string]PublisherFactory{}
pubRegMu sync.Mutex
)
// Publisher defines the main publisher interface.
type Publisher interface {
// Topic returns a topic handle by name.
Topic(name string) PubTopic
// Close closes the producer connection.
Close() error
}
// NewPublisher inits to a publisher via URL.
//
// pub, err := bps.NewPublisher(context.TODO(), "kafka://10.0.0.1:9092,10.0.0.2:9092,10.0.0.3:9092/namespace")
func NewPublisher(ctx context.Context, urlStr string) (Publisher, error) {
u, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
pubRegMu.Lock()
factory, ok := pubReg[u.Scheme]
pubRegMu.Unlock()
if !ok {
return nil, fmt.Errorf("unknown URL scheme %q", u.Scheme)
}
return factory(ctx, u)
}
// --------------------------------------------------------------------
// PublisherFactory constructs a publisher from a URL.
type PublisherFactory func(context.Context, *url.URL) (Publisher, error)
// RegisterPublisher registers a new protocol with a scheme and a corresponding
// PublisherFactory.
func RegisterPublisher(scheme string, factory PublisherFactory) {
pubRegMu.Lock()
defer pubRegMu.Unlock()
if _, exists := pubReg[scheme]; exists {
panic("protocol " + scheme + " already registered")
}
pubReg[scheme] = factory
}
// --------------------------------------------------------------------
// PubMessage represents a single message for publishing.
type PubMessage struct {
// ID is an optional message identifier.
// It may not be supported by some implementations (then it is ignored).
// Or may be used just to calculate partition the message.
ID string `json:"id,omitempty"`
// Data is the message payload.
Data []byte `json:"data,omitempty"`
// Attributes contains optional key-value labels.
// It may not be supported by some implementations (then it is ignored).
Attributes map[string]string `json:"attributes,omitempty"`
}
// PubTopic is a publisher handle to a topic.
type PubTopic interface {
// Publish publishes a message to the topic.
Publish(context.Context, *PubMessage) error
}
// --------------------------------------------------------------------
// InMemPublisher is an in-memory publisher implementation which can be used for tests.
type InMemPublisher struct {
topics map[string]*InMemPubTopic
mu sync.RWMutex
}
// NewInMemPublisher returns an initialised publisher.
func NewInMemPublisher() *InMemPublisher {
return &InMemPublisher{
topics: make(map[string]*InMemPubTopic),
}
}
// Topic implements Publisher interface. It will auto-provision a topic if it does not exist.
func (p *InMemPublisher) Topic(name string) PubTopic {
p.mu.RLock()
topic, ok := p.topics[name]
p.mu.RUnlock()
if ok {
return topic
}
p.mu.Lock()
defer p.mu.Unlock()
if topic, ok = p.topics[name]; !ok {
topic = new(InMemPubTopic)
p.topics[name] = topic
}
return topic
}
// Close implements Publisher.
func (*InMemPublisher) Close() error {
return nil
}
// InMemPubTopic is an in-memory implementation of a Topic.
// Useful for tests.
type InMemPubTopic struct {
messages []*PubMessage
mu sync.RWMutex
}
// Publish implements Topic.
func (t *InMemPubTopic) Publish(_ context.Context, msg *PubMessage) error {
t.mu.Lock()
t.messages = append(t.messages, msg)
t.mu.Unlock()
return nil
}
// Messages returns published messages.
func (t *InMemPubTopic) Messages() []*PubMessage {
t.mu.RLock()
messages := t.messages
t.mu.RUnlock()
return messages
}