-
Notifications
You must be signed in to change notification settings - Fork 56
/
pub.go
302 lines (258 loc) · 5.88 KB
/
pub.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Copyright 2018 The go-zeromq Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package zmq4
import (
"context"
"fmt"
"net"
"sync"
"sync/atomic"
)
const (
DefaultSendHwm = 1000
)
// Topics is an interface that wraps the basic Topics method.
type Topics interface {
// Topics returns the sorted list of topics a socket is subscribed to.
Topics() []string
}
// NewPub returns a new PUB ZeroMQ socket.
// The returned socket value is initially unbound.
func NewPub(ctx context.Context, opts ...Option) Socket {
pub := &pubSocket{sck: newSocket(ctx, Pub, opts...)}
pub.sck.w = newPubMWriter(pub.sck.ctx)
pub.sck.r = newPubQReader(pub.sck.ctx)
return pub
}
// pubSocket is a PUB ZeroMQ socket.
type pubSocket struct {
sck *socket
}
// Close closes the open Socket
func (pub *pubSocket) Close() error {
return pub.sck.Close()
}
// Send puts the message on the outbound send queue.
// Send blocks until the message can be queued or the send deadline expires.
func (pub *pubSocket) Send(msg Msg) error {
ctx, cancel := context.WithTimeout(pub.sck.ctx, pub.sck.Timeout())
defer cancel()
return pub.sck.w.write(ctx, msg)
}
// SendMulti puts the message on the outbound send queue.
// SendMulti blocks until the message can be queued or the send deadline expires.
// The message will be sent as a multipart message.
func (pub *pubSocket) SendMulti(msg Msg) error {
msg.multipart = true
ctx, cancel := context.WithTimeout(pub.sck.ctx, pub.sck.Timeout())
defer cancel()
return pub.sck.w.write(ctx, msg)
}
// Recv receives a complete message.
func (*pubSocket) Recv() (Msg, error) {
msg := Msg{err: fmt.Errorf("zmq4: PUB sockets can't recv messages")}
return msg, msg.err
}
// Listen connects a local endpoint to the Socket.
func (pub *pubSocket) Listen(ep string) error {
return pub.sck.Listen(ep)
}
// Dial connects a remote endpoint to the Socket.
func (pub *pubSocket) Dial(ep string) error {
return pub.sck.Dial(ep)
}
// Type returns the type of this Socket (PUB, SUB, ...)
func (pub *pubSocket) Type() SocketType {
return pub.sck.Type()
}
// Addr returns the listener's address.
// Addr returns nil if the socket isn't a listener.
func (pub *pubSocket) Addr() net.Addr {
return pub.sck.Addr()
}
// GetOption is used to retrieve an option for a socket.
func (pub *pubSocket) GetOption(name string) (interface{}, error) {
return pub.sck.GetOption(name)
}
// SetOption is used to set an option for a socket.
func (pub *pubSocket) SetOption(name string, value interface{}) error {
err := pub.sck.SetOption(name, value)
if err != nil {
return err
}
if name != OptionHWM {
return ErrBadProperty
}
hwm, ok := value.(int)
if !ok {
return ErrBadProperty
}
w := pub.sck.w.(*pubMWriter)
w.hwm.Store(int64(hwm))
return nil
}
// Topics returns the sorted list of topics a socket is subscribed to.
func (pub *pubSocket) Topics() []string {
return pub.sck.topics()
}
// pubQReader is a queued-message reader.
type pubQReader struct {
ctx context.Context
mu sync.RWMutex
rs []*Conn
c chan Msg
sem *semaphore // ready when a connection is live.
}
func newPubQReader(ctx context.Context) *pubQReader {
const qrsize = 10
return &pubQReader{
ctx: ctx,
c: make(chan Msg, qrsize),
sem: newSemaphore(),
}
}
func (q *pubQReader) Close() error {
q.mu.RLock()
var err error
for _, r := range q.rs {
e := r.Close()
if e != nil && err == nil {
err = e
}
}
q.rs = nil
q.mu.RUnlock()
return err
}
func (q *pubQReader) addConn(r *Conn) {
q.mu.Lock()
q.sem.enable()
q.rs = append(q.rs, r)
q.mu.Unlock()
go q.listen(q.ctx, r)
}
func (q *pubQReader) rmConn(r *Conn) {
q.mu.Lock()
defer q.mu.Unlock()
cur := -1
for i := range q.rs {
if q.rs[i] == r {
cur = i
break
}
}
if cur >= 0 {
q.rs = append(q.rs[:cur], q.rs[cur+1:]...)
}
}
func (q *pubQReader) read(ctx context.Context, msg *Msg) error {
q.sem.lock(ctx)
select {
case <-ctx.Done():
case *msg = <-q.c:
}
return msg.err
}
func (q *pubQReader) listen(ctx context.Context, r *Conn) {
defer q.rmConn(r)
defer r.Close()
for {
msg := r.read()
select {
case <-ctx.Done():
return
default:
if msg.err != nil {
return
}
switch {
case q.topic(msg):
r.subscribe(msg)
default:
q.c <- msg
}
}
}
}
func (q *pubQReader) topic(msg Msg) bool {
if len(msg.Frames) != 1 {
return false
}
frame := msg.Frames[0]
if len(frame) == 0 {
return false
}
topic := frame[0]
return topic == 0 || topic == 1
}
type pubMWriter struct {
ctx context.Context
mu sync.RWMutex
subscribers map[*Conn]chan Msg
hwm atomic.Int64
}
func newPubMWriter(ctx context.Context) *pubMWriter {
p := &pubMWriter{
ctx: ctx,
subscribers: map[*Conn]chan Msg{},
}
p.hwm.Store(DefaultSendHwm)
return p
}
func (w *pubMWriter) Close() error {
w.mu.Lock()
defer w.mu.Unlock()
for conn, channel := range w.subscribers {
_ = conn.Close()
close(channel)
}
w.subscribers = nil
return nil
}
func (mw *pubMWriter) addConn(w *Conn) {
mw.mu.Lock()
defer mw.mu.Unlock()
c := make(chan Msg, mw.hwm.Load())
mw.subscribers[w] = c
go func() {
for {
msg, ok := <-c
if !ok {
break
}
topic := string(msg.Frames[0])
if w.subscribed(topic) {
_ = w.SendMsg(msg)
}
}
}()
}
func (mw *pubMWriter) rmConn(w *Conn) {
mw.mu.Lock()
defer mw.mu.Unlock()
if channel, ok := mw.subscribers[w]; ok {
_ = w.Close()
delete(mw.subscribers, w)
close(channel)
}
}
func (w *pubMWriter) write(ctx context.Context, msg Msg) error {
w.mu.RLock()
defer w.mu.RUnlock()
for _, channel := range w.subscribers {
select {
case <-ctx.Done():
return ctx.Err()
case channel <- msg: // proceeds to default case if the channel is full (msg will be discarded)
default:
}
}
return nil
}
var (
_ rpool = (*pubQReader)(nil)
_ wpool = (*pubMWriter)(nil)
_ Socket = (*pubSocket)(nil)
_ Topics = (*pubSocket)(nil)
)