@@ -16,18 +16,19 @@ func main() {
1616 var stateAccepted int32
1717 var stateReleased int32
1818 var stateRejected int32
19+ var isRunning bool
1920
2021 var received int32
2122 var failed int32
2223
2324 startTime := time .Now ()
25+ isRunning = true
2426 go func () {
25- for {
27+ for isRunning {
2628 time .Sleep (5 * time .Second )
2729 total := stateAccepted + stateReleased + stateRejected
2830 messagesPerSecond := float64 (total ) / time .Since (startTime ).Seconds ()
2931 rmq .Info ("[Stats]" , "sent" , total , "received" , received , "failed" , failed , "messagesPerSecond" , messagesPerSecond )
30-
3132 }
3233 }()
3334
@@ -41,6 +42,16 @@ func main() {
4142 switch statusChanged .To .(type ) {
4243 case * rmq.StateOpen :
4344 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+
4455 }
4556 }
4657 }(stateChanged )
@@ -87,13 +98,13 @@ func main() {
8798
8899 // Consume messages from the queue
89100 go func (ctx context.Context ) {
90- for {
101+ for isRunning {
91102 deliveryContext , err := consumer .Receive (ctx )
92103 if errors .Is (err , context .Canceled ) {
93104 // The consumer was closed correctly
94105 return
95106 }
96- if err != nil {
107+ if err != nil && isRunning {
97108 // An error occurred receiving the message
98109 // here the consumer could be disconnected from the server due to a network error
99110 signalBlock .L .Lock ()
@@ -107,7 +118,7 @@ func main() {
107118
108119 atomic .AddInt32 (& received , 1 )
109120 err = deliveryContext .Accept (context .Background ())
110- if err != nil {
121+ if err != nil && isRunning {
111122 // same here the delivery could not be accepted due to a network error
112123 // we wait for 2_500 ms and try again
113124 time .Sleep (2500 * time .Millisecond )
@@ -124,12 +135,13 @@ func main() {
124135 return
125136 }
126137
127- wg := & sync.WaitGroup {}
128138 for i := 0 ; i < 1 ; i ++ {
129- wg .Add (1 )
130139 go func () {
131- defer wg .Done ()
132140 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+ }
133145 publishResult , err := publisher .Publish (context .Background (), rmq .NewMessage ([]byte ("Hello, World!" + fmt .Sprintf ("%d" , i ))))
134146 if err != nil {
135147 // 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() {
160172 }
161173 }()
162174 }
163- wg .Wait ()
164175
165176 println ("press any key to close the connection" )
166177
0 commit comments