-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
357 lines (310 loc) · 9.3 KB
/
main.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package main
import (
"bufio"
"context"
"errors"
"flag"
"fmt"
"github.com/Azure/azure-sdk-for-go/sdk/messaging/azservicebus"
"log"
"os"
"strings"
)
var buildVersion = "unknown"
var buildDate = "unknown"
const NullStr = "\xff"
var logDebug = false
var prefixMsgWithSessionId = false
var prefixMsgWithMessageId = false
var correlationId = ""
func send(ctx context.Context, client *azservicebus.Client, sessionId *string, queueName *string) {
if *sessionId == "" {
sessionId = nil
}
debug("Opening sender")
sender, err := client.NewSender(*queueName, nil)
if err != nil {
fatal("Cannot create sender:", err)
}
defer func() {
debug("Closing sender")
sender.Close(ctx)
}()
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
msg := createMsgFromString(scanner.Text())
if sessionId != nil && *sessionId != NullStr {
msg.SessionID = sessionId
}
debug("Sending message")
err := sender.SendMessage(ctx, msg, nil)
if err != nil {
fatal("Cannot send message:", err)
}
}
if err := scanner.Err(); err != nil {
fatal("Cannot read standard input:", err)
}
}
func receive(ctx context.Context, client *azservicebus.Client, sessionId *string, count int, queueName *string) {
if sessionId != nil && *sessionId != NullStr && *sessionId != "" {
receiveMoreSession(ctx, client, sessionId, queueName, count)
} else {
for i := 0; (count < 0 || i < count) && receiveOne(ctx, client, sessionId, queueName); i++ {
}
}
}
func receiveOne(ctx context.Context, client *azservicebus.Client, sessionId *string, queueName *string) bool {
if *sessionId == NullStr {
return receiveOneNoSession(ctx, client, queueName)
} else {
if *sessionId == "" {
sessionId = nil
}
return receiveMoreSession(ctx, client, sessionId, queueName, 1)
}
}
func receiveOneNoSession(ctx context.Context, client *azservicebus.Client, queueName *string) bool {
debug("Opening receiver")
receiver, err := client.NewReceiverForQueue(
*queueName,
nil,
)
defer func() {
debug("Closing receiver")
receiver.Close(ctx)
}()
if err != nil {
fatal("Cannot create receiver:", err)
}
debug("Calling receive", 1)
messages, err := receiver.ReceiveMessages(ctx, 1, nil)
if err != nil {
fatal("Cannot receive message:", err)
}
debug("Received messages", len(messages))
for _, msg := range messages {
if msg.SessionID != nil && prefixMsgWithSessionId {
fmt.Print(*msg.SessionID + ":")
}
if msg.SessionID != nil && prefixMsgWithMessageId {
fmt.Print(msg.MessageID + ":")
}
fmt.Println(string(msg.Body))
err = receiver.CompleteMessage(ctx, msg, nil)
if err != nil {
var sbErr *azservicebus.Error
if errors.As(err, &sbErr) && sbErr.Code == azservicebus.CodeLockLost {
// The message lock has expired. This isn't fatal for the client, but it does mean
// that this message can be received by another Receiver (or potentially this one!).
debug("Message lock expired\n")
continue
}
fatal("Cannot receive message", err)
}
return true
}
return false
}
func receiveMoreSession(ctx context.Context, client *azservicebus.Client, sessionId *string, queueName *string, count int) bool {
var receiver *azservicebus.SessionReceiver
var err error
for true {
if sessionId != nil && *sessionId != "" {
debug("Opening queue session", strPtroToString(sessionId))
receiver, err = client.AcceptSessionForQueue(ctx, *queueName, *sessionId, nil)
} else {
debug("Opening next available session")
receiver, err = client.AcceptNextSessionForQueue(ctx, *queueName, nil)
}
if err != nil {
// After version 1.1.0 this should be possible (see https://github.com/Azure/azure-sdk-for-go/issues/19039)
//var sbErr *azservicebus.Error
//if errors.As(err, &sbErr) && sbErr.Code == azservicebus.CodeTimeout {
// // there are no sessions available. This isn't fatal - we can use the client and try
// // to AcceptNextSessionForQueue() again.
// debug("No session available")
// continue
//}
if strings.Contains(err.Error(), "com.microsoft:timeout") {
// there are no sessions available. This isn't fatal - we can use the client and try
// to AcceptNextSessionForQueue() again.
debug("No session available, trying again")
continue
}
fatal("Cannot open session:", err)
} else {
break
}
}
defer func() {
debug("Closing queue session", receiver.SessionID())
receiver.Close(ctx)
}()
for i := 0; count < 0 || i < count; {
remains := 100
if count > 0 {
remains = count - i
}
debug("Calling receive", remains)
messages, err := receiver.ReceiveMessages(ctx, remains, nil)
if err != nil {
fatal("Cannot receive messages:", err)
}
for _, msg := range messages {
if msg.SessionID != nil && prefixMsgWithSessionId {
fmt.Print(*msg.SessionID + ":")
}
if msg.SessionID != nil && prefixMsgWithMessageId {
fmt.Print(msg.MessageID + ":")
}
fmt.Println(string(msg.Body))
err = receiver.CompleteMessage(ctx, msg, nil)
if err != nil {
var sbErr *azservicebus.Error
if errors.As(err, &sbErr) && sbErr.Code == azservicebus.CodeLockLost {
// The message lock has expired. This isn't fatal for the client, but it does mean
// that this message can be received by another Receiver (or potentially this one!).
debug("Message lock expired\n")
continue
}
fatal("Cannot receive message", err)
}
i++
}
}
return true
}
func peek(ctx context.Context, client *azservicebus.Client, queueName *string, count int) {
receiver, err := client.NewReceiverForQueue(
*queueName,
nil,
)
if err != nil {
fatal("Cannot create receiver:", err)
}
defer receiver.Close(ctx)
for i := 0; count < 0 || i < count; {
messages, err := receiver.PeekMessages(ctx, 1, nil)
if err != nil {
fatal("Cannot peek message:", err)
}
for _, msg := range messages {
if msg.SessionID != nil && prefixMsgWithSessionId {
fmt.Print(*msg.SessionID + ":")
}
if msg.SessionID != nil && prefixMsgWithMessageId {
fmt.Print(msg.MessageID + ":")
}
fmt.Println(string(msg.Body))
i++
}
}
}
func usage() {
fmt.Println(`Usage: ` + os.Args[0] + ` <command> <options>
Commands:
send - Sends messages to a queue. Reads standard input, sending each line as message, all in same session.
receive - Receives messages, outputting them to standard output, message per line.
peek - Peeks one or more messages, outpus them to standard output.
-v - Prints version info.
Common options:
-c Connection string
-d (flag) Log debug info
-h (flag) Show this help
-n number of received or peeked messages. Defaults to one.
-q Queue name
-s Session ID.
If the queue is not session-enabled, do not set this option.
If the queue is session-enabled, must be specified.
If set to empty string for receive, will receive message from any session.
Receive options:
-ps Prefix every message with session id, separated with ':'. Useful if receiving all sessions messages.
-pm Prefix every message with message id, separated with ':'.
Send option:
-i Correlation ID for sent messages
`)
}
func printVersion() {
fmt.Println(os.Args[0] + " " + buildVersion + " (built " + buildDate + ")")
}
func main() {
commonFlags := flag.NewFlagSet("common flags", flag.ExitOnError)
connStrPtr := commonFlags.String("c", "", "Connection string")
queueNamePtr := commonFlags.String("q", "", "Queue name")
sessionIdPtr := commonFlags.String("s", NullStr, "Session ID")
msgCountPtr := commonFlags.Int("n", 1, "Number of received/peeked messages")
helpPtr := commonFlags.Bool("h", false, "Show help")
commonFlags.BoolVar(&logDebug, "d", false, "Log debug info")
if len(os.Args) < 2 {
usage()
os.Exit(2)
}
switch os.Args[1] {
case "-h":
usage()
return
case "-v":
printVersion()
return
case "receive":
commonFlags.BoolVar(&prefixMsgWithSessionId, "ps", false, "Prefix received messages with session id, separated with ':'")
commonFlags.BoolVar(&prefixMsgWithMessageId, "pm", false, "Prefix received messages with message id, separated with ':'")
case "send":
commonFlags.StringVar(&correlationId, "i", "", "Correlation ID")
}
err := commonFlags.Parse(os.Args[2:])
if err != nil {
usage()
os.Exit(2)
}
if *helpPtr {
usage()
os.Exit(2)
}
if *connStrPtr == "" {
fatal("connection string is required")
}
if *queueNamePtr == "" {
fatal("queue name is required")
}
//ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
//defer cancel()
ctx := context.Background()
client, err := azservicebus.NewClientFromConnectionString(*connStrPtr, nil)
if err != nil {
fatal("Cannot connect to servicebus:", err)
}
switch os.Args[1] {
case "send":
send(ctx, client, sessionIdPtr, queueNamePtr)
case "receive":
receive(ctx, client, sessionIdPtr, *msgCountPtr, queueNamePtr)
case "peek":
peek(ctx, client, queueNamePtr, *msgCountPtr)
}
}
func strPtroToString(s *string) string {
if s == nil {
return "<nil>"
} else {
return *s
}
}
func debug(msg string, args ...interface{}) {
if logDebug {
log.Println(append([]interface{}{"DEBUG: " + msg}, args...)...)
}
}
func fatal(msg string, args ...interface{}) {
log.Fatal(append([]interface{}{"ERROR: " + msg}, args...)...)
}
func createMsgFromString(s string) *azservicebus.Message {
msg := &azservicebus.Message{
Body: []byte(s),
}
if correlationId != "" {
msg.CorrelationID = &correlationId
}
return msg
}