DEV Community

Cover image for Azure Storage Queue SDK for Go Samples for Enqueue/Dequeue and Count
Tsuyoshi Ushio
Tsuyoshi Ushio

Posted on

Azure Storage Queue SDK for Go Samples for Enqueue/Dequeue and Count

I searched a working sample of the Azure Storage Queue with Azure Storage Queue SDK for Go to save my time. However, I couldn't. Only we can find on the GitHub repo is documentation. So I create a sample for sending/receiving queue. You can refer the sample repository below.

Get the count of the messages

The behavior is probably that you expected. However, I noticed that one thing. If you want to get a count of the queue, we can use props.ApproximateMessagesCount() function on the QueueGetPropertiesResponse. As it is represent the name, the count is Approximate number. If you run the example, you will see this output on the receive.go. The log that shows Approximate... is the one that I use the method. For the Visible count of message, it is fetched by a function that I'll explain later. This receive sample Dequeue a message. Before the dequeue, the number of the message is 6. However, after the dequeue, Approximate one is still 6. I should be 5. When you delete the message, the value is the same. It looks the ApproximateMessagesCount() looks ignore the visibility of the message.

receive

1: Approximate number of messages in the queue=7
2: Visible count of messages in the queue=7
3: Dequeue [25009-77-70 254:00:00 +0000 UTC] : Message: Hello!
4: Approximate number of messages in the queue=7
5: Visible count of messages in the queue=6
6: Delete message bc3ae6cc-3222-40c5-b922-f0a2b4266532
7: Approximate number of messages in the queue=6
8: Visible count of messages in the queue=6

According to the official REST API documentation:

The approximate number of messages in the queue. This number is not lower than the actual number of messages in the queue, but could be higher.
Response Headers.

How to get the visible queue count?

Just Peek the queue then, count the number. This method is accurate, however, one downside is, maximum maxCount is 32. numofmessages

func getVisibleCount(messagesURL *azqueue.MessagesURL, maxCount int32) (int32, error) {
    ctx := context.Background()
    queue, err := messagesURL.Peek(ctx, maxCount)
    if err != nil {
        return 0, err
    }
    num := queue.NumMessages()
    return num, nil
}

I'm writing a scaler logic by these values. I'll combine two method to scale my app.

FYI, for the send message, I sent 100 messages. In this case, Visible count of message is 1.

1: Approximate number of messages in the queue=100
2: Visible count of messages in the queue=1
3: Enqueue Hello 99: ec979756-bbc2-42d0-a791-0a29bfb00d2b 
4: Approximate number of messages in the queue=101
5: Visible count of messages in the queue=1

For the Enqueue method, it has two parameters. The function signature is func (m MessagesURL) Enqueue(ctx context.Context, messageText string, visibilityTimeout time.Duration, timeToLive time.Duration) (*EnqueueMessageResponse, error) {
vt := int32(visibilityTimeout.Seconds())
I set the visibilityTimeout as 30*times.Second That is why it takes 30 sec for able to see the message. timeToLive 0 means 7 days.

response, err := messageURL.Enqueue(ctx, "Hello!", 30*time.Second, 0)

If you want to visible immediately, let's make it 0.

response, err := messageURL.Enqueue(ctx, "Hello!", 0, 0)

Then it works as you expected.

1: Approximate number of messages in the queue=30
2: Visible count of messages in the queue=30
3: Enqueue Hello 30: 75909a2a-9d12-41c7-bb23-675c32dd8c88 
4: Approximate number of messages in the queue=31
5: Visible count of messages in the queue=31
1: Approximate number of messages in the queue=31
2: Visible count of messages in the queue=31
3: Enqueue Hello 31: 8898a793-d8d0-47d4-afaf-266fcacc3778 
4: Approximate number of messages in the queue=32
5: Visible count of messages in the queue=32
1: Approximate number of messages in the queue=32
2: Visible count of messages in the queue=32
3: Enqueue Hello 32: 7989faaf-3905-402d-9d3c-0caeebbdb705 
4: Approximate number of messages in the queue=33
5: Visible count of messages in the queue=32
1: Approximate number of messages in the queue=33
2: Visible count of messages in the queue=32

Top comments (0)