Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add max priority level to RabbitMQ queue #33

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions shared/queue/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
log "github.com/sirupsen/logrus"
)

const maxPriority uint8 = 5

type Queue struct {
channel *amqp.Channel
queue amqp.Queue
Expand Down Expand Up @@ -38,13 +40,17 @@ func Init(queueName, url string) (*Queue, error) {
return nil, err
}

args := amqp.Table{
"x-max-priority": maxPriority, // Set the max priority level
}

q.queue, err = q.channel.QueueDeclare(
queueName, // name
false, // durable
false, // delete when unused
false, // exclusive
false, // no-wait
nil, // arguments
args, // arguments
)
if err != nil {
log.WithError(err).Error("error declaring RabbitMQ queue")
Expand All @@ -57,6 +63,11 @@ func Init(queueName, url string) (*Queue, error) {
func (q *Queue) Enqueue(ctx context.Context, msg payload.QueuePayload, prio uint8) error {
log.Debugf("Enqueue function called with ctx %+v message: %v", ctx, msg)

if prio > maxPriority {
log.Warnf("Priority %d is higher than max priority %d. Setting to max priority", prio, maxPriority)
prio = maxPriority
}

body, err := json.Marshal(msg)
if err != nil {
log.WithError(err).Error("error marshalling message")
Expand All @@ -69,7 +80,7 @@ func (q *Queue) Enqueue(ctx context.Context, msg payload.QueuePayload, prio uint
false, // mandatory
false, // immediate
amqp.Publishing{
ContentType: "text/plain",
ContentType: "application/json",
Body: []byte(body),
Priority: prio,
})
Expand Down