-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoc.go
100 lines (79 loc) · 2.78 KB
/
doc.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
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
Package events provides event bus implemented on top of RabbitMQ message broker.
Guaranties at least once semantics on event delivery. Notice that
event handling should be idempotent, otherwise additional event
deduplication is required to ensure that the same event is not handled
twice.
Features
- Connection to a RabbitMQ cluster with automatic reconnection
- Configurable concurrency for consuming and publishing
- Broker topology setup
- Batching of acknowledgements sent to broker
- Pluggable logging
Example
This example shows basics of using event bus
package main
import (
"fmt"
"log"
"time"
events "github.com/w1ck3dg0ph3r/rabbit-events"
)
// This example shows basics of using event bus
func Example() {
// Create an event bus
bus := &events.Bus{
Connection: &events.Connection{
Protocol: "amqp",
Hostnames: []string{"127.0.0.1:5672"},
Vhost: "",
Username: "user",
Password: "pass",
DialTimeout: 1 * time.Second,
ConnectionTimeout: 10 * time.Second,
ConnectionBackoff: 1 * time.Second,
Logger: nil,
},
AppName: "testapp",
IngressExchange: "ingress",
EgressExchange: "ingress",
EventTTL: 60 * time.Second,
DeadEventExchange: "amq.fanout",
ConcurrentConsumers: 4,
ConcurrentPublishers: 4,
MaxEventsInFlight: 100,
}
if err := bus.SetHandler("event.example", func(e *events.Event, publish events.PublishFunc) {
fmt.Printf("id:%s, cid:%s\n", e.ID, e.CorrelationID)
err := publish(&events.Event{Name: "event.second", ID: "id2", CorrelationID: e.ID})
if err != nil {
fmt.Println(err)
e.Nack(false)
return
}
e.Ack()
}); err != nil {
log.Fatal(err)
}
if err := bus.SetHandler("event.second", func(e *events.Event, publish events.PublishFunc) {
fmt.Printf("id:%s, cid:%s\n", e.ID, e.CorrelationID)
e.Ack()
}); err != nil {
log.Fatal(err)
}
// Run event bus
if err := bus.Start(); err != nil {
log.Fatal(err)
}
// Publish an event
err := bus.Publish(&events.Event{Name: "event.example", ID: "id1"})
if err != nil {
panic(err)
}
time.Sleep(1 * time.Second)
// Stop event bus
bus.Shutdown()
}
See tests and runnable examples for additional info.
*/
package events