package main
import (
"fmt"
"time"
"github.com/streadway/amqp"
)
func connectRabbitMQ() (*amqp.Connection, error) {
// Replace with your RabbitMQ server details
conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
if err != nil {
return nil, fmt.Errorf("failed to connect to RabbitMQ: %w", err)
}
return conn, nil
}
func main() {
var conn *amqp.Connection
var err error
// Set initial backoff duration
backoffDuration := 1 * time.Second
for {
// Attempt to connect to RabbitMQ
conn, err = connectRabbitMQ()
if err != nil {
fmt.Println(err)
time.Sleep(backoffDuration)
// Increase backoff duration exponentially
backoffDuration *= 2
if backoffDuration > 30*time.Second {
backoffDuration = 30 * time.Second
}
continue
}
fmt.Println("Successfully connected to RabbitMQ!")
backoffDuration = 1 * time.Second // Reset backoff after successful connection
defer conn.Close()
// You can implement your message publishing/subscribing logic here
break // Exit loop if connection successful
}
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?