-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublisher.go
More file actions
87 lines (72 loc) · 1.18 KB
/
publisher.go
File metadata and controls
87 lines (72 loc) · 1.18 KB
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 main
import (
"context"
"encoding/json"
"time"
amqp "github.com/rabbitmq/amqp091-go"
)
type MsgBroker struct {
conn *amqp.Connection
queue string
}
func newMsgBroker(conn *amqp.Connection, queue string) (*MsgBroker, error) {
broker := &MsgBroker{
conn: conn,
queue: queue,
}
err := broker.setup()
if err != nil {
return nil, err
}
return broker, nil
}
func (a *MsgBroker) setup() error {
channel, err := a.conn.Channel()
if err != nil {
return err
}
defer channel.Close()
_, err = channel.QueueDeclare(
a.queue,
true,
false,
false,
false,
nil,
)
if err != nil {
return err
}
err = channel.Qos(
1,
0,
false,
)
return err
}
func (a *MsgBroker) PublishLog(log *log) error {
wireData, err := json.Marshal(log)
if err != nil {
return err
}
channel, err := a.conn.Channel()
if err != nil {
return err
}
defer channel.Close()
msg := amqp.Publishing{
DeliveryMode: amqp.Persistent,
Body: wireData,
ContentType: "application/json",
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
return channel.PublishWithContext(
ctx,
"",
a.queue,
false,
false,
msg,
)
}