@@ -16,18 +16,19 @@ func main() {
16
16
var stateAccepted int32
17
17
var stateReleased int32
18
18
var stateRejected int32
19
+ var isRunning bool
19
20
20
21
var received int32
21
22
var failed int32
22
23
23
24
startTime := time .Now ()
25
+ isRunning = true
24
26
go func () {
25
- for {
27
+ for isRunning {
26
28
time .Sleep (5 * time .Second )
27
29
total := stateAccepted + stateReleased + stateRejected
28
30
messagesPerSecond := float64 (total ) / time .Since (startTime ).Seconds ()
29
31
rmq .Info ("[Stats]" , "sent" , total , "received" , received , "failed" , failed , "messagesPerSecond" , messagesPerSecond )
30
-
31
32
}
32
33
}()
33
34
@@ -41,6 +42,16 @@ func main() {
41
42
switch statusChanged .To .(type ) {
42
43
case * rmq.StateOpen :
43
44
signalBlock .Broadcast ()
45
+ case * rmq.StateReconnecting :
46
+ rmq .Info ("[connection]" , "Reconnecting to the AMQP 1.0 server" )
47
+ case * rmq.StateClosed :
48
+ StateClosed := statusChanged .To .(* rmq.StateClosed )
49
+ if errors .Is (StateClosed .GetError (), rmq .ErrMaxReconnectAttemptsReached ) {
50
+ rmq .Error ("[connection]" , "Max reconnect attempts reached. Closing connection" , StateClosed .GetError ())
51
+ signalBlock .Broadcast ()
52
+ isRunning = false
53
+ }
54
+
44
55
}
45
56
}
46
57
}(stateChanged )
@@ -87,13 +98,13 @@ func main() {
87
98
88
99
// Consume messages from the queue
89
100
go func (ctx context.Context ) {
90
- for {
101
+ for isRunning {
91
102
deliveryContext , err := consumer .Receive (ctx )
92
103
if errors .Is (err , context .Canceled ) {
93
104
// The consumer was closed correctly
94
105
return
95
106
}
96
- if err != nil {
107
+ if err != nil && isRunning {
97
108
// An error occurred receiving the message
98
109
// here the consumer could be disconnected from the server due to a network error
99
110
signalBlock .L .Lock ()
@@ -107,7 +118,7 @@ func main() {
107
118
108
119
atomic .AddInt32 (& received , 1 )
109
120
err = deliveryContext .Accept (context .Background ())
110
- if err != nil {
121
+ if err != nil && isRunning {
111
122
// same here the delivery could not be accepted due to a network error
112
123
// we wait for 2_500 ms and try again
113
124
time .Sleep (2500 * time .Millisecond )
@@ -124,12 +135,13 @@ func main() {
124
135
return
125
136
}
126
137
127
- wg := & sync.WaitGroup {}
128
138
for i := 0 ; i < 1 ; i ++ {
129
- wg .Add (1 )
130
139
go func () {
131
- defer wg .Done ()
132
140
for i := 0 ; i < 500_000 ; i ++ {
141
+ if ! isRunning {
142
+ rmq .Info ("[Publisher]" , "Publisher is stopped simulation not running, queue" , queueName )
143
+ return
144
+ }
133
145
publishResult , err := publisher .Publish (context .Background (), rmq .NewMessage ([]byte ("Hello, World!" + fmt .Sprintf ("%d" , i ))))
134
146
if err != nil {
135
147
// here you need to deal with the error. You can store the message in a local in memory/persistent storage
@@ -160,7 +172,6 @@ func main() {
160
172
}
161
173
}()
162
174
}
163
- wg .Wait ()
164
175
165
176
println ("press any key to close the connection" )
166
177
0 commit comments