Go - Queue.Dequeue()
Dequeue messages.
import ("context""fmt""github.com/nitrictech/go-sdk/nitric""github.com/nitrictech/go-sdk/nitric/queues")func main() {queue := nitric.NewQueue("queue-name").Allow(queues.QueueDequeue)ctx := context.TODO()// dequeue up to 10 messagesmessages, err := queue.Dequeue(ctx, 10)if err != nil {return}for _, message := range messages {fmt.Println("Message: ", message.Message())message.Complete(ctx)}nitric.Run()}
Parameters
- Name
ctx
- Required
- Required
- Type
- context
- Description
The context of the call, used for tracing.
- Name
depth
- Optional
- Optional
- Type
- int
- Description
The maximum number of messages to dequeue. Defaults to 1.
Notes
Completing dequeued items
Since the process is asynchronous the queue doesn't know when a message has been processed. This ensures failed services/containers don't result in lost message, since messages are not removed from a queue when they're dequeued.
Instead, messages are hidden and requesters are granted a temporary lease for each message they request.
When processing is complete, the requester must tell the queue the task was completed successfully, which will remove it from the queue and stop it being reprocessed.
Failing to complete a dequeued message before the lease expires will result in it being re-queued.
To notify the queue that a message was completed call the Complete()
method on the dequeued message reference.
Dequeue depth
When calling Dequeue()
a depth parameter can be provided, e.g. Dequeue(ctx, 5)
. This will attempt to dequeue up to 5 messages from the queue.
However, if the queue is empty or less than 5 messages are available on the queue, then the maximum that are currently available will be returned.
This means calls to dequeue will return between 0 and depth tasks.
Examples
Dequeue and complete messages
import ("context""fmt""github.com/nitrictech/go-sdk/nitric""github.com/nitrictech/go-sdk/nitric/queues")func main() {queue := nitric.NewQueue("queue-name").Allow(queues.QueueDequeue)ctx := context.TODO()// dequeue up to 10 messagesmessages, err := queue.Dequeue(ctx, 10)if err != nil {return}for _, message := range messages {fmt.Println("Message: ", message.Message())message.Complete(ctx)}nitric.Run()}