From e9f38b9fc0d27c5f1ad4945fed19fd9e5b426f19 Mon Sep 17 00:00:00 2001 From: Dieter Plaetinck Date: Mon, 18 May 2015 10:01:58 -0400 Subject: [PATCH] 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 --- pkg/services/eventpublisher/eventpublisher.go | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/pkg/services/eventpublisher/eventpublisher.go b/pkg/services/eventpublisher/eventpublisher.go index 14e527b2cc7..2854b63a9a5 100644 --- a/pkg/services/eventpublisher/eventpublisher.go +++ b/pkg/services/eventpublisher/eventpublisher.go @@ -109,25 +109,26 @@ func Setup() error { } func publish(routingKey string, msgString []byte) { - err := channel.Publish( - exchange, //exchange - routingKey, // routing key - false, // mandatory - false, // immediate - amqp.Publishing{ - ContentType: "application/json", - Body: msgString, - }, - ) - if err != nil { + for { + err := channel.Publish( + exchange, //exchange + routingKey, // routing key + false, // mandatory + false, // immediate + amqp.Publishing{ + ContentType: "application/json", + Body: msgString, + }, + ) + if err == nil { + return + } // failures are most likely because the connection was lost. // the connection will be re-established, so just keep // retrying every 2seconds until we successfully publish. time.Sleep(2 * time.Second) fmt.Println("publish failed, retrying.") - publish(routingKey, msgString) } - return } func eventListener(event interface{}) error {