forked from go-zeromq/zmq4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rep.go
334 lines (287 loc) · 6.47 KB
/
rep.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// 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"
)
// NewRep returns a new REP ZeroMQ socket.
// The returned socket value is initially unbound.
func NewRep(ctx context.Context, opts ...Option) Socket {
rep := &repSocket{newSocket(ctx, Rep, opts...)}
sharedState := newRepState()
rep.sck.w = newRepWriter(rep.sck.ctx, sharedState)
rep.sck.r = newRepReader(rep.sck.ctx, sharedState)
return rep
}
// repSocket is a REP ZeroMQ socket.
type repSocket struct {
sck *socket
}
// Close closes the open Socket
func (rep *repSocket) Close() error {
return rep.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 (rep *repSocket) Send(msg Msg) error {
ctx, cancel := context.WithTimeout(rep.sck.ctx, rep.sck.timeout())
defer cancel()
return rep.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 (rep *repSocket) SendMulti(msg Msg) error {
msg.multipart = true
ctx, cancel := context.WithTimeout(rep.sck.ctx, rep.sck.timeout())
defer cancel()
return rep.sck.w.write(ctx, msg)
}
// Recv receives a complete message.
func (rep *repSocket) Recv() (Msg, error) {
ctx, cancel := context.WithCancel(rep.sck.ctx)
defer cancel()
var msg Msg
err := rep.sck.r.read(ctx, &msg)
return msg, err
}
// Listen connects a local endpoint to the Socket.
func (rep *repSocket) Listen(ep string) error {
return rep.sck.Listen(ep)
}
// Dial connects a remote endpoint to the Socket.
func (rep *repSocket) Dial(ep string) error {
return rep.sck.Dial(ep)
}
// Type returns the type of this Socket (PUB, SUB, ...)
func (rep *repSocket) Type() SocketType {
return rep.sck.Type()
}
// Addr returns the listener's address.
// Addr returns nil if the socket isn't a listener.
func (rep *repSocket) Addr() net.Addr {
return rep.sck.Addr()
}
// GetOption is used to retrieve an option for a socket.
func (rep *repSocket) GetOption(name string) (interface{}, error) {
return rep.sck.GetOption(name)
}
// SetOption is used to set an option for a socket.
func (rep *repSocket) SetOption(name string, value interface{}) error {
return rep.sck.SetOption(name, value)
}
type repMsg struct {
conn *Conn
msg Msg
}
type repReader struct {
ctx context.Context
state *repState
mu sync.Mutex
conns []*Conn
msgCh chan repMsg
}
func newRepReader(ctx context.Context, state *repState) *repReader {
const qsize = 10
return &repReader{
ctx: ctx,
msgCh: make(chan repMsg, qsize),
state: state,
}
}
func (r *repReader) addConn(c *Conn) {
r.mu.Lock()
r.conns = append(r.conns, c)
r.mu.Unlock()
go r.listen(r.ctx, c)
}
func (r *repReader) rmConn(conn *Conn) {
r.mu.Lock()
defer r.mu.Unlock()
cur := -1
for i := range r.conns {
if r.conns[i] == conn {
cur = i
break
}
}
if cur >= 0 {
r.conns = append(r.conns[:cur], r.conns[cur+1:]...)
}
}
func (r *repReader) read(ctx context.Context, msg *Msg) error {
select {
case <-ctx.Done():
return ctx.Err()
case repMsg := <-r.msgCh:
if repMsg.msg.err != nil {
return repMsg.msg.err
}
pre, innerMsg := splitReq(repMsg.msg)
if pre == nil {
return fmt.Errorf("zmq4: invalid REP message")
}
*msg = innerMsg
r.state.Set(repMsg.conn, pre)
}
return nil
}
func (r *repReader) listen(ctx context.Context, conn *Conn) {
defer r.rmConn(conn)
defer conn.Close()
for {
msg := conn.read()
select {
case <-ctx.Done():
return
default:
if msg.err != nil {
return
}
r.msgCh <- repMsg{conn, msg}
}
}
}
func (r *repReader) Close() error {
r.mu.Lock()
defer r.mu.Unlock()
var err error
for _, conn := range r.conns {
e := conn.Close()
if e != nil && err == nil {
err = e
}
}
r.conns = nil
return err
}
func splitReq(envelope Msg) (preamble [][]byte, msg Msg) {
for i, frame := range envelope.Frames {
if len(frame) != 0 {
continue
}
preamble = envelope.Frames[:i+1]
if i+1 < len(envelope.Frames) {
msg = NewMsgFrom(envelope.Frames[i+1:]...)
}
}
return
}
type repSendPayload struct {
conn *Conn
preamble [][]byte
msg Msg
}
type repWriter struct {
ctx context.Context
state *repState
mu sync.Mutex
conns []*Conn
sendCh chan repSendPayload
}
func (r repSendPayload) buildReplyMsg() Msg {
var frames = make([][]byte, 0, len(r.preamble)+len(r.msg.Frames))
frames = append(frames, r.preamble...)
frames = append(frames, r.msg.Frames...)
return NewMsgFrom(frames...)
}
func newRepWriter(ctx context.Context, state *repState) *repWriter {
r := &repWriter{
ctx: ctx,
state: state,
sendCh: make(chan repSendPayload),
}
go r.run()
return r
}
func (r *repWriter) addConn(w *Conn) {
r.mu.Lock()
r.conns = append(r.conns, w)
r.mu.Unlock()
}
func (r *repWriter) rmConn(conn *Conn) {
r.mu.Lock()
defer r.mu.Unlock()
cur := -1
for i := range r.conns {
if r.conns[i] == conn {
cur = i
break
}
}
if cur >= 0 {
r.conns = append(r.conns[:cur], r.conns[cur+1:]...)
}
}
func (r *repWriter) write(ctx context.Context, msg Msg) error {
conn, preamble := r.state.Get()
r.sendCh <- repSendPayload{conn, preamble, msg}
return nil
}
func (r *repWriter) run() {
for {
select {
case <-r.ctx.Done():
return
case payload, ok := <-r.sendCh:
if !ok {
return
}
r.sendPayload(payload)
}
}
}
func (r *repWriter) sendPayload(payload repSendPayload) {
r.mu.Lock()
defer r.mu.Unlock()
for _, conn := range r.conns {
if conn == payload.conn {
reply := payload.buildReplyMsg()
// not much we can do at this point. Perhaps log the error?
_ = conn.SendMsg(reply)
return
}
}
}
func (r *repWriter) Close() error {
close(r.sendCh)
r.mu.Lock()
defer r.mu.Unlock()
var err error
for _, conn := range r.conns {
e := conn.Close()
if e != nil && err == nil {
err = e
}
}
r.conns = nil
return err
}
type repState struct {
mu sync.Mutex
conn *Conn
preamble [][]byte // includes delimiter
}
func newRepState() *repState {
return &repState{}
}
func (r *repState) Get() (conn *Conn, preamble [][]byte) {
r.mu.Lock()
conn = r.conn
preamble = r.preamble
r.mu.Unlock()
return
}
func (r *repState) Set(conn *Conn, pre [][]byte) {
r.mu.Lock()
r.conn = conn
r.preamble = pre
r.mu.Unlock()
}
var (
_ Socket = (*repSocket)(nil)
)