From 1cbe66a0c09021198083dc76b1b30b7e32992a53 Mon Sep 17 00:00:00 2001 From: nutcas3 Date: Fri, 1 May 2026 09:06:43 +0300 Subject: [PATCH] refactor: Add context parameter to MessageQueue Publish method with timeout wrapping - Add context.Context parameter to Publish method signature - Replace context.Background() with provided context wrapped in timeout - Rename timeout context variable from ctx to timeoutCtx for clarity --- apps/api-server/internal/mq/mq.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/apps/api-server/internal/mq/mq.go b/apps/api-server/internal/mq/mq.go index fb55c67..7dd587d 100644 --- a/apps/api-server/internal/mq/mq.go +++ b/apps/api-server/internal/mq/mq.go @@ -82,7 +82,7 @@ func (mq *MessageQueue) DeclareQueue(name string) error { } // Publish publishes a message to a queue -func (mq *MessageQueue) Publish(queue string, msg Message) error { +func (mq *MessageQueue) Publish(ctx context.Context, queue string, msg Message) error { msg.Timestamp = time.Now() body, err := json.Marshal(msg) @@ -90,11 +90,12 @@ func (mq *MessageQueue) Publish(queue string, msg Message) error { return fmt.Errorf("failed to marshal message: %w", err) } - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + // Use provided context with timeout + timeoutCtx, cancel := context.WithTimeout(ctx, 5*time.Second) defer cancel() err = mq.channel.PublishWithContext( - ctx, + timeoutCtx, "", // exchange queue, // routing key false, // mandatory