This repository has been archived by the owner on Mar 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 231
/
mq_client.go
182 lines (168 loc) · 3.93 KB
/
mq_client.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package mqclient
import (
"context"
"errors"
log "github.com/sirupsen/logrus"
"github.com/streadway/amqp"
"github.com/trustwallet/blockatlas/pubsub"
"go.uber.org/atomic"
"time"
)
var (
reconnectDelay = 5 * time.Second
resendDelay = 5 * time.Second
)
type Client struct {
uri string
conn *amqp.Connection
channel *amqp.Channel
prefetchCount int
streams []*pubsub.Stream
ctx context.Context
notifyClose chan *amqp.Error
notifyConfirm chan amqp.Confirmation
isConnected *atomic.Bool
alive *atomic.Bool
}
func New(uri string, prefetchCount int, ctx context.Context) (client pubsub.Client) {
client = &Client{
uri: uri,
ctx: ctx,
alive: atomic.NewBool(false),
isConnected: atomic.NewBool(false),
prefetchCount: prefetchCount,
streams: []*pubsub.Stream{},
}
return client
}
func (c *Client) Connect() error {
conn, err := amqp.Dial(c.uri)
if err != nil {
log.Error("Client.connect MQ Dial issue " + err.Error())
return err
}
ch, err := conn.Channel()
if err != nil {
log.Error("Client.connect MQ Channel issue " + err.Error())
return err
}
err = ch.Confirm(false)
if err != nil {
log.Error("Client.connect MQ Channel issue " + err.Error())
}
log.Debug("Interface: ", c)
c.conn = conn
c.channel = ch
c.notifyClose = make(chan *amqp.Error)
c.notifyConfirm = make(chan amqp.Confirmation)
c.channel.NotifyClose(c.notifyClose)
c.channel.NotifyPublish(c.notifyConfirm)
c.isConnected.Store(true)
return nil
}
func (c *Client) Run() error {
log.Debug("Interface: ", c)
if c.conn == nil {
return errors.New("connect firstly")
}
go c.handleReconnect()
err := c.channel.Qos(c.prefetchCount, 0, false)
if err != nil {
return errors.New("Client.connect MQ Qos issue " + err.Error())
}
for _, stream := range c.streams {
if !(*stream).IsConnected() {
go (*stream).Connect(c.ctx)
}
}
return nil
}
func (c *Client) IsConnected() bool {
return c.isConnected.Load()
}
func (c *Client) AddStream(consumer *pubsub.Consumer, isWriteOnly bool) error {
var stream pubsub.Stream = &Stream{
consumer: consumer,
client: c,
isConnected: atomic.NewBool(false),
isWriteOnly: isWriteOnly,
channel: c.channel,
}
go stream.Connect(c.ctx) // Try connect, if client isn't run it will wait run
c.streams = append(c.streams, &stream)
return nil
}
func (c *Client) Push(queue string, data []byte) error {
if !c.isConnected.Load() {
// TODO: Is should wait connect to RabbitMQ or not?
return errors.New("failed to push push: not connected")
}
//todo Add stream waiting
for {
err := c.PushUnsafe(queue, data)
if err != nil {
log.Error("Client.Push MQ issue " + err.Error())
if err == pubsub.ErrDisconnected {
continue
}
return err
}
select {
case confirm := <-c.notifyConfirm:
if confirm.Ack {
return nil
}
case <-time.After(resendDelay):
}
}
}
func (c *Client) PushUnsafe(queue string, data []byte) error {
if !c.isConnected.Load() {
return pubsub.ErrDisconnected
}
return c.channel.Publish(
"", // Exchange
string(queue), // Routing key
false, // Mandatory
false, // Immediate
amqp.Publishing{
ContentType: "text/plain",
Body: data,
},
)
}
func (c *Client) Close() error {
err := c.channel.Close()
if err != nil {
return errors.New("Client.Close MQ issue " + err.Error())
}
err = c.conn.Close()
if err != nil {
return errors.New("Client.Close MQ issue " + err.Error())
}
return nil
}
func (c *Client) handleReconnect() {
for c.alive.Load() {
log.Debug("Try connect after alive")
c.isConnected.Store(false)
for c.Connect() != nil {
log.Debug("Try connect")
if !c.alive.Load() {
return
}
select {
case <-c.ctx.Done():
return
case <-time.After(reconnectDelay + time.Second):
// Add metric
}
}
select {
case <-c.ctx.Done():
log.Fatal("Fucking end")
return
case <-c.notifyClose:
}
}
}