mirror of
https://github.com/andwati/telecom-platform.git
synced 2026-10-30 16:42:15 +00:00
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
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user