/
pstr_amqp.go
203 lines (186 loc) · 4.97 KB
/
pstr_amqp.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
Copyright (C) ITsysCOM GmbH
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package engine
import (
"fmt"
"net/url"
"strings"
"sync"
"time"
"github.com/cgrates/cgrates/utils"
amqp "github.com/rabbitmq/amqp091-go"
)
var amqpQuery = []string{"cacertfile", "certfile", "keyfile", "verify", "server_name_indication", "auth_mechanism", "heartbeat", "connection_timeout", "channel_max"}
// NewAMQPPoster creates a new amqp poster
// "amqp://guest:guest@localhost:5672/?queueID=cgrates_cdrs"
func NewAMQPPoster(dialURL string, attempts int) (*AMQPPoster, error) {
amqp := &AMQPPoster{
attempts: attempts,
}
if err := amqp.parseURL(dialURL); err != nil {
return nil, err
}
return amqp, nil
}
// AMQPPoster used to post cdrs to amqp
type AMQPPoster struct {
dialURL string
queueID string // identifier of the CDR queue where we publish
exchange string
exchangeType string
routingKey string
attempts int
sync.Mutex // protect connection
conn *amqp.Connection
}
func (pstr *AMQPPoster) parseURL(dialURL string) error {
u, err := url.Parse(dialURL)
if err != nil {
return err
}
qry := u.Query()
q := url.Values{}
for _, key := range amqpQuery {
if vals, has := qry[key]; has && len(vals) != 0 {
q.Add(key, vals[0])
}
}
pstr.dialURL = strings.Split(dialURL, "?")[0] + "?" + q.Encode()
pstr.queueID = defaultQueueID
pstr.routingKey = defaultQueueID
if vals, has := qry[queueID]; has && len(vals) != 0 {
pstr.queueID = vals[0]
}
if vals, has := qry[routingKey]; has && len(vals) != 0 {
pstr.routingKey = vals[0]
}
if vals, has := qry[exchange]; has && len(vals) != 0 {
pstr.exchange = vals[0]
pstr.exchangeType = defaultExchangeType
}
if vals, has := qry[exchangeType]; has && len(vals) != 0 {
pstr.exchangeType = vals[0]
}
return nil
}
// Post is the method being called when we need to post anything in the queue
// the optional chn will permits channel caching
func (pstr *AMQPPoster) Post(content []byte, _ string) (err error) {
var chn *amqp.Channel
fib := utils.Fib()
for i := 0; i < pstr.attempts; i++ {
if chn, err = pstr.newPostChannel(); err == nil {
break
}
if i+1 < pstr.attempts {
time.Sleep(time.Duration(fib()) * time.Second)
}
}
if err != nil {
utils.Logger.Warning(fmt.Sprintf("<AMQPPoster> creating new post channel, err: %s", err.Error()))
return
}
for i := 0; i < pstr.attempts; i++ {
if err = chn.Publish(
pstr.exchange, // exchange
pstr.routingKey, // routing key
false, // mandatory
false, // immediate
amqp.Publishing{
DeliveryMode: amqp.Persistent,
ContentType: utils.CONTENT_JSON,
Body: content,
}); err == nil {
break
}
if i+1 < pstr.attempts {
time.Sleep(time.Duration(fib()) * time.Second)
}
}
if err != nil {
return
}
if chn != nil {
chn.Close()
}
return
}
// Close closes the connections
func (pstr *AMQPPoster) Close() {
pstr.Lock()
if pstr.conn != nil {
pstr.conn.Close()
}
pstr.conn = nil
pstr.Unlock()
}
func (pstr *AMQPPoster) newPostChannel() (postChan *amqp.Channel, err error) {
pstr.Lock()
if pstr.conn == nil {
var conn *amqp.Connection
conn, err = amqp.Dial(pstr.dialURL)
if err == nil {
pstr.conn = conn
go func() { // monitor connection errors so we can restart
if err := <-pstr.conn.NotifyClose(make(chan *amqp.Error)); err != nil {
utils.Logger.Err(fmt.Sprintf("Connection error received: %s", err.Error()))
pstr.Close()
}
}()
}
}
pstr.Unlock()
if err != nil {
return nil, err
}
if postChan, err = pstr.conn.Channel(); err != nil {
return
}
if pstr.exchange != "" {
if err = postChan.ExchangeDeclare(
pstr.exchange, // name
pstr.exchangeType, // type
true, // durable
false, // audo-delete
false, // internal
false, // no-wait
nil, // args
); err != nil {
return
}
}
if _, err = postChan.QueueDeclare(
pstr.queueID, // name
true, // durable
false, // auto-delete
false, // exclusive
false, // no-wait
nil, // args
); err != nil {
return
}
if pstr.exchange != "" {
if err = postChan.QueueBind(
pstr.queueID, // queue
pstr.routingKey, // key
pstr.exchange, // exchange
false, // no-wait
nil, // args
); err != nil {
return
}
}
return
}