-
Notifications
You must be signed in to change notification settings - Fork 55
/
req.go
224 lines (190 loc) · 4.69 KB
/
req.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
// 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"
)
// NewReq returns a new REQ ZeroMQ socket.
// The returned socket value is initially unbound.
func NewReq(ctx context.Context, opts ...Option) Socket {
state := &reqState{}
req := &reqSocket{newSocket(ctx, Req, opts...), state}
req.sck.r = newReqReader(req.sck.ctx, state)
req.sck.w = newReqWriter(req.sck.ctx, state)
return req
}
// reqSocket is a REQ ZeroMQ socket.
type reqSocket struct {
sck *socket
state *reqState
}
// Close closes the open Socket
func (req *reqSocket) Close() error {
return req.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 (req *reqSocket) Send(msg Msg) error {
ctx, cancel := context.WithTimeout(req.sck.ctx, req.sck.Timeout())
defer cancel()
return req.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 (req *reqSocket) SendMulti(msg Msg) error {
msg.multipart = true
ctx, cancel := context.WithTimeout(req.sck.ctx, req.sck.Timeout())
defer cancel()
return req.sck.w.write(ctx, msg)
}
// Recv receives a complete message.
func (req *reqSocket) Recv() (Msg, error) {
ctx, cancel := context.WithCancel(req.sck.ctx)
defer cancel()
var msg Msg
err := req.sck.r.read(ctx, &msg)
return msg, err
}
// Listen connects a local endpoint to the Socket.
func (req *reqSocket) Listen(ep string) error {
return req.sck.Listen(ep)
}
// Dial connects a remote endpoint to the Socket.
func (req *reqSocket) Dial(ep string) error {
return req.sck.Dial(ep)
}
// Type returns the type of this Socket (PUB, SUB, ...)
func (req *reqSocket) Type() SocketType {
return req.sck.Type()
}
// Addr returns the listener's address.
// Addr returns nil if the socket isn't a listener.
func (req *reqSocket) Addr() net.Addr {
return req.sck.Addr()
}
// GetOption is used to retrieve an option for a socket.
func (req *reqSocket) GetOption(name string) (interface{}, error) {
return req.sck.GetOption(name)
}
// SetOption is used to set an option for a socket.
func (req *reqSocket) SetOption(name string, value interface{}) error {
return req.sck.SetOption(name, value)
}
type reqWriter struct {
mu sync.Mutex
conns []*Conn
nextConn int
state *reqState
}
func newReqWriter(ctx context.Context, state *reqState) *reqWriter {
return &reqWriter{
state: state,
}
}
func (r *reqWriter) write(ctx context.Context, msg Msg) error {
msg.Frames = append([][]byte{nil}, msg.Frames...)
r.mu.Lock()
defer r.mu.Unlock()
var err error
for i := 0; i < len(r.conns); i++ {
cur := i + r.nextConn%len(r.conns)
conn := r.conns[cur]
err = conn.SendMsg(msg)
if err == nil {
r.nextConn = cur + 1%len(r.conns)
r.state.Set(conn)
return nil
}
}
return fmt.Errorf("zmq4: no connections available: %w", err)
}
func (r *reqWriter) addConn(c *Conn) {
r.mu.Lock()
r.conns = append(r.conns, c)
r.mu.Unlock()
}
func (r *reqWriter) 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:]...)
}
r.state.Reset(conn)
}
func (r *reqWriter) 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
}
type reqReader struct {
state *reqState
}
func newReqReader(ctx context.Context, state *reqState) *reqReader {
return &reqReader{
state: state,
}
}
func (r *reqReader) addConn(c *Conn) {}
func (r *reqReader) rmConn(c *Conn) {}
func (r *reqReader) Close() error {
return nil
}
func (r *reqReader) read(ctx context.Context, msg *Msg) error {
curConn := r.state.Get()
if curConn == nil {
return fmt.Errorf("zmq4: no connections available")
}
*msg = curConn.read()
if msg.err != nil {
return msg.err
}
if len(msg.Frames) > 1 {
msg.Frames = msg.Frames[1:]
}
return nil
}
type reqState struct {
mu sync.Mutex
lastConn *Conn
}
func (r *reqState) Set(conn *Conn) {
r.mu.Lock()
defer r.mu.Unlock()
r.lastConn = conn
}
// Reset resets the state iff c matches the resident connection
func (r *reqState) Reset(c *Conn) {
r.mu.Lock()
defer r.mu.Unlock()
if r.lastConn == c {
r.lastConn = nil
}
}
func (r *reqState) Get() *Conn {
r.mu.Lock()
defer r.mu.Unlock()
return r.lastConn
}
var (
_ Socket = (*reqSocket)(nil)
)