-
Notifications
You must be signed in to change notification settings - Fork 56
/
router.go
251 lines (217 loc) · 5.06 KB
/
router.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
// 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 (
"bytes"
"context"
"net"
"sync"
"golang.org/x/sync/errgroup"
)
// NewRouter returns a new ROUTER ZeroMQ socket.
// The returned socket value is initially unbound.
func NewRouter(ctx context.Context, opts ...Option) Socket {
router := &routerSocket{newSocket(ctx, Router, opts...)}
router.sck.r = newRouterQReader(router.sck.ctx)
router.sck.w = newRouterMWriter(router.sck.ctx)
return router
}
// routerSocket is a ROUTER ZeroMQ socket.
type routerSocket struct {
sck *socket
}
// Close closes the open Socket
func (router *routerSocket) Close() error {
return router.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 (router *routerSocket) Send(msg Msg) error {
ctx, cancel := context.WithTimeout(router.sck.ctx, router.sck.Timeout())
defer cancel()
return router.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 (router *routerSocket) SendMulti(msg Msg) error {
msg.multipart = true
return router.Send(msg)
}
// Recv receives a complete message.
func (router *routerSocket) Recv() (Msg, error) {
return router.sck.Recv()
}
// Listen connects a local endpoint to the Socket.
func (router *routerSocket) Listen(ep string) error {
return router.sck.Listen(ep)
}
// Dial connects a remote endpoint to the Socket.
func (router *routerSocket) Dial(ep string) error {
return router.sck.Dial(ep)
}
// Type returns the type of this Socket (PUB, SUB, ...)
func (router *routerSocket) Type() SocketType {
return router.sck.Type()
}
// Addr returns the listener's address.
// Addr returns nil if the socket isn't a listener.
func (router *routerSocket) Addr() net.Addr {
return router.sck.Addr()
}
// GetOption is used to retrieve an option for a socket.
func (router *routerSocket) GetOption(name string) (interface{}, error) {
return router.sck.GetOption(name)
}
// SetOption is used to set an option for a socket.
func (router *routerSocket) SetOption(name string, value interface{}) error {
return router.sck.SetOption(name, value)
}
// routerQReader is a queued-message reader.
type routerQReader struct {
ctx context.Context
mu sync.RWMutex
rs []*Conn
c chan Msg
sem *semaphore // ready when a connection is live.
}
func newRouterQReader(ctx context.Context) *routerQReader {
const qrsize = 10
return &routerQReader{
ctx: ctx,
c: make(chan Msg, qrsize),
sem: newSemaphore(),
}
}
func (q *routerQReader) 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 *routerQReader) 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 *routerQReader) 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 *routerQReader) read(ctx context.Context, msg *Msg) error {
q.sem.lock(ctx)
select {
case <-ctx.Done():
return ctx.Err()
case *msg = <-q.c:
}
return msg.err
}
func (q *routerQReader) listen(ctx context.Context, r *Conn) {
defer q.rmConn(r)
defer r.Close()
id := []byte(r.Peer.Meta[sysSockID])
for {
msg := r.read()
select {
case <-ctx.Done():
return
default:
if msg.err != nil {
return
}
msg.Frames = append([][]byte{id}, msg.Frames...)
q.c <- msg
}
}
}
type routerMWriter struct {
ctx context.Context
mu sync.Mutex
ws []*Conn
sem *semaphore
}
func newRouterMWriter(ctx context.Context) *routerMWriter {
return &routerMWriter{
ctx: ctx,
sem: newSemaphore(),
}
}
func (w *routerMWriter) Close() error {
w.mu.Lock()
var err error
for _, ww := range w.ws {
e := ww.Close()
if e != nil && err == nil {
err = e
}
}
w.ws = nil
w.mu.Unlock()
return err
}
func (mw *routerMWriter) addConn(w *Conn) {
mw.mu.Lock()
mw.sem.enable()
mw.ws = append(mw.ws, w)
mw.mu.Unlock()
}
func (mw *routerMWriter) rmConn(w *Conn) {
mw.mu.Lock()
defer mw.mu.Unlock()
cur := -1
for i := range mw.ws {
if mw.ws[i] == w {
cur = i
break
}
}
if cur >= 0 {
mw.ws = append(mw.ws[:cur], mw.ws[cur+1:]...)
}
}
func (w *routerMWriter) write(ctx context.Context, msg Msg) error {
w.sem.lock(ctx)
grp, _ := errgroup.WithContext(ctx)
w.mu.Lock()
id := msg.Frames[0]
dmsg := NewMsgFrom(msg.Frames[1:]...)
for i := range w.ws {
ww := w.ws[i]
pid := []byte(ww.Peer.Meta[sysSockID])
if !bytes.Equal(pid, id) {
continue
}
grp.Go(func() error {
return ww.SendMsg(dmsg)
})
}
err := grp.Wait()
w.mu.Unlock()
return err
}
var (
_ rpool = (*routerQReader)(nil)
_ wpool = (*routerMWriter)(nil)
_ Socket = (*routerSocket)(nil)
)