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

use PublishWithContext instead of Publish #115

Merged
merged 1 commit into from Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion _examples/pubsub/pubsub.go
Expand Up @@ -15,6 +15,7 @@ import (
"io"
"log"
"os"
"time"

amqp "github.com/rabbitmq/amqp091-go"
)
Expand Down Expand Up @@ -87,6 +88,9 @@ func redial(ctx context.Context, url string) chan chan session {
// publish publishes messages to a reconnecting session to a fanout exchange.
// It receives from the application specific source of messages.
func publish(sessions chan chan session, messages <-chan message) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

for session := range sessions {
var (
running bool
Expand Down Expand Up @@ -122,7 +126,7 @@ func publish(sessions chan chan session, messages <-chan message) {

case body = <-pending:
routingKey := "ignored for fanout exchanges, application dependent for other exchanges"
err := pub.Publish(exchange, routingKey, false, false, amqp.Publishing{
err := pub.PublishWithContext(ctx, exchange, routingKey, false, false, amqp.Publishing{
Body: body,
})
// Retry failed delivery on the next session
Expand Down
5 changes: 4 additions & 1 deletion _examples/simple-producer/producer.go
Expand Up @@ -4,6 +4,7 @@
package main

import (
"context"
"flag"
"fmt"
"log"
Expand Down Expand Up @@ -101,12 +102,14 @@ func publish(done chan bool, amqpURI, exchange, exchangeType, routingKey, body s
}

Log.Println("declared Exchange, publishing messages")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

for {
seqNo := channel.GetNextPublishSeqNo()
Log.Printf("publishing %dB body (%q)", len(body), body)

if err := channel.Publish(
if err := channel.PublishWithContext(ctx,
exchange, // publish to an exchange
routingKey, // routing to 0 or more queues
false, // mandatory
Expand Down