-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
49 lines (44 loc) · 1.25 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"log"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/heureka/gorabbit/connection"
amqp "github.com/rabbitmq/amqp091-go"
)
func main() {
bo := backoff.NewExponentialBackOff()
bo.MaxElapsedTime = 10 * time.Second
// create new connection with re-dialing capabilities.
conn, err := connection.Dial(
"amqp://localhost:5672",
// set up different backoff strategy.
connection.WithBackoff(bo),
// provide different then default dialing config.
connection.WithConfig(amqp.Config{
Heartbeat: 10 * time.Second, // e.g. set different heartbeat interval
}),
// log dial attempts
connection.WithDialAttemptCallback(func(err error) {
if err != nil {
log.Println("can't dial rabbitmq", err)
return
}
log.Println("successfully dial rabbitmq")
}),
// configure connection as you wish on each successful dial.
connection.WithDialledCallback(func(conn *amqp.Connection) {
// for example, add listener for close notification
closeNotif := conn.NotifyClose(make(chan *amqp.Error))
go func() {
for err := range closeNotif {
log.Println("connection got closed", err)
}
}()
}),
)
if err != nil {
log.Panic(err)
}
conn.Channel() // crate new channel or do whatever you do with connection.
}