-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.go
More file actions
353 lines (287 loc) · 7.04 KB
/
backend.go
File metadata and controls
353 lines (287 loc) · 7.04 KB
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
//go:build !stub
// +build !stub
package qzmq
import (
"context"
"fmt"
"sync"
zmq "github.com/luxfi/zmq/v4"
)
// zmqSocket implements Socket using luxfi/zmq
// This automatically uses CZMQ when CGO=1 and czmq tag is set,
// otherwise falls back to pure Go implementation
type zmqSocket struct {
socket zmq.Socket
socketType SocketType
opts Options
metrics *SocketMetrics
mu sync.RWMutex
closed bool
ctx context.Context
cancel context.CancelFunc
}
// Initialize the backend
func initGoBackend() error {
// luxfi/zmq handles backend selection automatically
logInfo("Initializing ZMQ backend", "backend", "luxfi/zmq")
return nil
}
// Create a new socket using luxfi/zmq
func newGoSocket(socketType SocketType, opts Options) (Socket, error) {
logDebug("Creating socket", "type", socketType.String(), "suite", opts.Suite)
ctx, cancel := context.WithCancel(context.Background())
// Create socket with appropriate type
var socket zmq.Socket
switch socketType {
case REQ:
socket = zmq.NewReq(ctx)
case REP:
socket = zmq.NewRep(ctx)
case PUB:
socket = zmq.NewPub(ctx)
case SUB:
socket = zmq.NewSub(ctx)
case XPUB:
socket = zmq.NewXPub(ctx)
case XSUB:
socket = zmq.NewXSub(ctx)
case PUSH:
socket = zmq.NewPush(ctx)
case PULL:
socket = zmq.NewPull(ctx)
case PAIR:
socket = zmq.NewPair(ctx)
case DEALER:
socket = zmq.NewDealer(ctx)
case ROUTER:
socket = zmq.NewRouter(ctx)
case STREAM:
socket = zmq.NewStream(ctx)
default:
cancel()
return nil, fmt.Errorf("unsupported socket type: %v", socketType)
}
if socket == nil {
cancel()
return nil, fmt.Errorf("failed to create socket")
}
return &zmqSocket{
socket: socket,
socketType: socketType,
opts: opts,
metrics: NewSocketMetrics(),
ctx: ctx,
cancel: cancel,
}, nil
}
func (s *zmqSocket) Bind(endpoint string) error {
s.mu.Lock()
defer s.mu.Unlock()
if s.closed {
return ErrNotConnected
}
err := s.socket.Listen(endpoint)
if err != nil {
logError("Failed to bind socket", "endpoint", endpoint, "error", err)
return err
}
logInfo("Socket bound", "type", s.socketType.String(), "endpoint", endpoint)
return nil
}
func (s *zmqSocket) Connect(endpoint string) error {
s.mu.Lock()
defer s.mu.Unlock()
if s.closed {
return ErrNotConnected
}
err := s.socket.Dial(endpoint)
if err != nil {
logError("Failed to connect socket", "endpoint", endpoint, "error", err)
return err
}
logInfo("Socket connected", "type", s.socketType.String(), "endpoint", endpoint)
return nil
}
func (s *zmqSocket) Send(data []byte) error {
s.mu.Lock()
defer s.mu.Unlock()
if s.closed {
return ErrNotConnected
}
// Create a message
msg := zmq.NewMsg(data)
// Send the message
err := s.socket.Send(msg)
if err != nil {
return err
}
// Update metrics
s.metrics.MessagesSent++
s.metrics.BytesSent += uint64(len(data))
logDebug("Message sent", "type", s.socketType.String(), "bytes", len(data))
return nil
}
func (s *zmqSocket) SendMultipart(parts [][]byte) error {
s.mu.Lock()
defer s.mu.Unlock()
if s.closed {
return ErrNotConnected
}
// Create multi-part message
msg := zmq.NewMsgFrom(parts...)
// Send the message
err := s.socket.Send(msg)
if err != nil {
return err
}
// Update metrics
s.metrics.MessagesSent++
for _, part := range parts {
s.metrics.BytesSent += uint64(len(part))
}
return nil
}
func (s *zmqSocket) Recv() ([]byte, error) {
s.mu.RLock()
defer s.mu.RUnlock()
if s.closed {
return nil, ErrNotConnected
}
// Receive message
msg, err := s.socket.Recv()
if err != nil {
return nil, err
}
// Get the first frame
if len(msg.Frames) == 0 {
return []byte{}, nil
}
data := msg.Frames[0]
// Update metrics
s.metrics.MessagesReceived++
s.metrics.BytesReceived += uint64(len(data))
return data, nil
}
func (s *zmqSocket) RecvMultipart() ([][]byte, error) {
s.mu.RLock()
defer s.mu.RUnlock()
if s.closed {
return nil, ErrNotConnected
}
// Receive message
msg, err := s.socket.Recv()
if err != nil {
return nil, err
}
// Get all frames
frames := msg.Frames
// Update metrics
s.metrics.MessagesReceived++
for _, frame := range frames {
s.metrics.BytesReceived += uint64(len(frame))
}
return frames, nil
}
func (s *zmqSocket) Subscribe(filter string) error {
s.mu.Lock()
defer s.mu.Unlock()
if s.closed {
return ErrNotConnected
}
if s.socketType != SUB && s.socketType != XSUB {
return fmt.Errorf("subscribe only valid for SUB/XSUB sockets")
}
// For XSUB, send subscription as a message
if s.socketType == XSUB {
// XSUB subscription format: first byte 1 means subscribe, followed by topic
subMsg := append([]byte{1}, []byte(filter)...)
msg := zmq.NewMsg(subMsg)
return s.socket.Send(msg)
}
// For SUB, use SetOption
return s.socket.SetOption(zmq.OptionSubscribe, filter)
}
func (s *zmqSocket) Unsubscribe(filter string) error {
s.mu.Lock()
defer s.mu.Unlock()
if s.closed {
return ErrNotConnected
}
if s.socketType != SUB && s.socketType != XSUB {
return fmt.Errorf("unsubscribe only valid for SUB/XSUB sockets")
}
// For XSUB, send unsubscription as a message
if s.socketType == XSUB {
// XSUB unsubscription format: first byte 0 means unsubscribe, followed by topic
unsubMsg := append([]byte{0}, []byte(filter)...)
msg := zmq.NewMsg(unsubMsg)
return s.socket.Send(msg)
}
// For SUB, use SetOption
return s.socket.SetOption(zmq.OptionUnsubscribe, filter)
}
func (s *zmqSocket) SetOption(name string, value interface{}) error {
s.mu.Lock()
defer s.mu.Unlock()
if s.closed {
return ErrNotConnected
}
// Map common options to ZMQ options
switch name {
case "sndhwm", "rcvhwm":
if v, ok := value.(int); ok {
return s.socket.SetOption(zmq.OptionHWM, v)
}
case "linger":
// luxfi/zmq doesn't have linger option, ignore for now
return nil
case "identity":
// luxfi/zmq handles identity differently
return nil
case "rcvtimeo", "sndtimeo":
// Timeout options - ignore for now as luxfi/zmq handles timeouts differently
return nil
}
return fmt.Errorf("unsupported option: %s", name)
}
func (s *zmqSocket) GetOption(name string) (interface{}, error) {
s.mu.RLock()
defer s.mu.RUnlock()
if s.closed {
return nil, ErrNotConnected
}
switch name {
case "type":
return s.socketType, nil
case "suite", "qzmq.suite":
return s.opts.Suite, nil
case "qzmq.encrypted":
return s.opts.Suite.KEM != 0 || s.opts.Suite.Sign != 0, nil
case "sndhwm", "rcvhwm":
// luxfi/zmq doesn't expose these options directly
return 1000, nil // default value
case "identity":
// luxfi/zmq handles identity differently
return "", nil
default:
return nil, fmt.Errorf("unsupported option: %s", name)
}
}
func (s *zmqSocket) Close() error {
// Cancel context FIRST to unblock pending Recv operations before acquiring lock
s.cancel()
s.mu.Lock()
defer s.mu.Unlock()
if s.closed {
return nil
}
s.closed = true
return s.socket.Close()
}
func (s *zmqSocket) GetMetrics() *SocketMetrics {
s.mu.RLock()
defer s.mu.RUnlock()
// Return a copy to avoid race conditions
metrics := *s.metrics
return &metrics
}