/
pstr_amqpv1.go
127 lines (114 loc) · 3.03 KB
/
pstr_amqpv1.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
/*
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 (
"context"
"fmt"
"sync"
"time"
amqpv1 "github.com/Azure/go-amqp"
"github.com/cgrates/cgrates/utils"
)
// NewAMQPv1Poster creates a poster for amqpv1
func NewAMQPv1Poster(dialURL string, attempts int) (Poster, error) {
URL, qID, err := parseURL(dialURL)
if err != nil {
return nil, err
}
return &AMQPv1Poster{
dialURL: URL,
queueID: "/" + qID,
attempts: attempts,
}, nil
}
// AMQPv1Poster a poster for amqpv1
type AMQPv1Poster struct {
sync.Mutex
dialURL string
queueID string // identifier of the CDR queue where we publish
attempts int
conn *amqpv1.Conn
}
// Close closes the connections
func (pstr *AMQPv1Poster) Close() {
pstr.Lock()
if pstr.conn != nil {
pstr.conn.Close()
}
pstr.conn = nil
pstr.Unlock()
}
// Post is the method being called when we need to post anything in the queue
func (pstr *AMQPv1Poster) Post(content []byte, _ string) (err error) {
var s *amqpv1.Session
fib := utils.Fib()
for i := 0; i < pstr.attempts; i++ {
if s, err = pstr.newPosterSession(); err == nil {
break
}
// reset client and try again
// used in case of closed connection because of idle time
if pstr.conn != nil {
pstr.conn.Close() // Make shure the connection is closed before reseting it
}
pstr.conn = nil
if i+1 < pstr.attempts {
time.Sleep(time.Duration(fib()) * time.Second)
}
}
if err != nil {
utils.Logger.Warning(fmt.Sprintf("<AMQPv1Poster> creating new post channel, err: %s", err.Error()))
return err
}
ctx := context.Background()
for i := 0; i < pstr.attempts; i++ {
sender, err := s.NewSender(ctx, pstr.queueID, nil)
if err != nil {
if i+1 < pstr.attempts {
time.Sleep(time.Duration(fib()) * time.Second)
}
continue
}
// Send message
err = sender.Send(ctx, amqpv1.NewMessage(content), nil)
sender.Close(ctx)
if err == nil {
break
}
if i+1 < pstr.attempts {
time.Sleep(time.Duration(fib()) * time.Second)
}
}
if err != nil {
return
}
if s != nil {
s.Close(ctx)
}
return
}
func (pstr *AMQPv1Poster) newPosterSession() (s *amqpv1.Session, err error) {
pstr.Lock()
defer pstr.Unlock()
if pstr.conn == nil {
var client *amqpv1.Conn
client, err = amqpv1.Dial(context.TODO(), pstr.dialURL, nil)
if err != nil {
return nil, err
}
pstr.conn = client
}
return pstr.conn.NewSession(context.TODO(), nil)
}