QMSTR / broker-test

RabbitMQ as a message broker on Kubernetes
0 stars 0 forks source link

Consumer polling solution #6

Closed marcomicera closed 3 years ago

marcomicera commented 3 years ago

As I described in #4, NewReceiveQueueMessagesRequest() seems not to be blocking.

In https://github.com/QMSTR/broker-test/issues/4#issuecomment-734369336 I said:

We could use KubeMQ's "long polling" feature with a timeout (e.g., 60 seconds) in a for loop, kinda like it's done in Java with wait() to handle spurious wakeups.

So it'd be something like:

for {
   res, err := client.NewReceiveQueueMessagesRequest().
      SetChannel(channel).
      SetMaxNumberOfMessages(1).
      SetWaitTimeSeconds(60).
      Send(ctx)
   if err != nil {
      log.Fatal(err)
   }

   if res != nil && res.MessagesReceived != 0 {
      break
   }
}

This is in fact what I've done, but:

But the name suggests that it's polling anyway, so not that great.

I cannot find any details regarding SetWaitTimeSeconds()'s implementation in the KubeMQ documentation. I don't know whether the Go routine is actually being suspended or not, hence I don't know whether this is the optimal solution for our problem...

marcomicera commented 3 years ago

Closing: now using callbacks and Go channels https://github.com/QMSTR/broker-test/commit/3cf054d7deb5a5a869b2eadbc5975dff34997f3e.