mirror of
https://github.com/andwati/telecom-platform.git
synced 2026-12-06 02:18:47 +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
|
// 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()
|
msg.Timestamp = time.Now()
|
||||||
|
|
||||||
body, err := json.Marshal(msg)
|
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)
|
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()
|
defer cancel()
|
||||||
|
|
||||||
err = mq.channel.PublishWithContext(
|
err = mq.channel.PublishWithContext(
|
||||||
ctx,
|
timeoutCtx,
|
||||||
"", // exchange
|
"", // exchange
|
||||||
queue, // routing key
|
queue, // routing key
|
||||||
false, // mandatory
|
false, // mandatory
|
||||||
|
|||||||
Reference in New Issue
Block a user