Commit e9f38b9f by Dieter Plaetinck

no unbound recursion in publish()

unbound recursion approach can blow up call stack,
and - I think - allocate memory unboundedly as well.

We can simply loop until err != nil

I didn't actually test this live, though tests succeed
parent 5896903b
...@@ -109,6 +109,7 @@ func Setup() error { ...@@ -109,6 +109,7 @@ func Setup() error {
} }
func publish(routingKey string, msgString []byte) { func publish(routingKey string, msgString []byte) {
for {
err := channel.Publish( err := channel.Publish(
exchange, //exchange exchange, //exchange
routingKey, // routing key routingKey, // routing key
...@@ -119,15 +120,15 @@ func publish(routingKey string, msgString []byte) { ...@@ -119,15 +120,15 @@ func publish(routingKey string, msgString []byte) {
Body: msgString, Body: msgString,
}, },
) )
if err != nil { if err == nil {
return
}
// failures are most likely because the connection was lost. // failures are most likely because the connection was lost.
// the connection will be re-established, so just keep // the connection will be re-established, so just keep
// retrying every 2seconds until we successfully publish. // retrying every 2seconds until we successfully publish.
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)
fmt.Println("publish failed, retrying.") fmt.Println("publish failed, retrying.")
publish(routingKey, msgString)
} }
return
} }
func eventListener(event interface{}) error { func eventListener(event interface{}) error {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment