-
Notifications
You must be signed in to change notification settings - Fork 17
/
publisher_example_test.go
87 lines (69 loc) · 2.05 KB
/
publisher_example_test.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package rabbitroutine_test
import (
"context"
"fmt"
"log"
"time"
"github.com/furdarius/rabbitroutine"
amqp "github.com/rabbitmq/amqp091-go"
)
// This example demonstrates publishing messages in RabbitMQ exchange using FireForgetPublisher.
func ExampleFireForgetPublisher() {
ctx := context.Background()
url := "amqp://guest:guest@127.0.0.1:5672/"
conn := rabbitroutine.NewConnector(rabbitroutine.Config{
// Max reconnect attempts
ReconnectAttempts: 20000,
// How long wait between reconnect
Wait: 2 * time.Second,
})
pool := rabbitroutine.NewLightningPool(conn)
pub := rabbitroutine.NewFireForgetPublisher(pool)
go func() {
err := conn.Dial(ctx, url)
if err != nil {
log.Println("failed to establish RabbitMQ connection:", err)
}
}()
for i := 0; i < 5000; i++ {
timeoutCtx, cancel := context.WithTimeout(ctx, 100*time.Millisecond)
err := pub.Publish(timeoutCtx, "myexch", "myqueue", amqp.Publishing{
Body: []byte(fmt.Sprintf("message %d", i)),
})
if err != nil {
log.Println("failed to publish:", err)
}
cancel()
}
}
// This example demonstrates publishing messages in RabbitMQ exchange delivery guarantees by EnsurePublisher
// and publishing retries by RetryPublisher.
func ExampleEnsurePublisher() {
ctx := context.Background()
url := "amqp://guest:guest@127.0.0.1:5672/"
conn := rabbitroutine.NewConnector(rabbitroutine.Config{
// Max reconnect attempts
ReconnectAttempts: 20000,
// How long wait between reconnect
Wait: 2 * time.Second,
})
pool := rabbitroutine.NewPool(conn)
ensurePub := rabbitroutine.NewEnsurePublisher(pool)
pub := rabbitroutine.NewRetryPublisher(ensurePub)
go func() {
err := conn.Dial(ctx, url)
if err != nil {
log.Println("failed to establish RabbitMQ connection:", err)
}
}()
for i := 0; i < 5000; i++ {
timeoutCtx, cancel := context.WithTimeout(ctx, 100*time.Millisecond)
err := pub.Publish(timeoutCtx, "myexch", "myqueue", amqp.Publishing{
Body: []byte(fmt.Sprintf("message %d", i)),
})
if err != nil {
log.Println("failed to publish:", err)
}
cancel()
}
}