-
Notifications
You must be signed in to change notification settings - Fork 6
/
session.go
328 lines (260 loc) · 5.92 KB
/
session.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// RabbitMQ driver with RPC functionality included, session object
// can be rpc server and client at the same time
package amqp
import (
"os"
"time"
"github.com/streadway/amqp"
//"os/signal"
"errors"
"math/rand"
"github.com/sirupsen/logrus"
)
// Errors
var (
ErrRpcTimeout = errors.New("RPC timeout")
ErrUnknown = errors.New("Unknown error")
ErrRPCChannelsRequired = errors.New("In order to make RPC calls you need to init receiver and sender")
)
const (
defaultReqX = "request"
defaultResX = "response"
)
type (
Config struct {
AMQPUrl string
Host string
VirtualHost string
Port int
User string
Password string
LogLevel uint8
}
Session interface {
Consumer(consumerCfg ConsumerConfig) (Consumer, error)
Server(cfg ServerConfig) (Server, error)
Publisher(cfg PublisherConfig) (Publisher, error)
Client(cfg ClientConfig) (Client, error)
Connect() (err error)
Close()
HealthCheck() error
}
session struct {
conn *amqp.Connection
servers []*server
clients []*client
cfg Config
closeCh chan bool
log *logrus.Logger
}
ConsumerConfig struct {
PrefetchCount int
PrefetchSize int
PrefetchGlobal bool
}
PublisherConfig struct {
// TODO: add publisher config options
}
)
func NewSession(cfg Config) Session {
rand.Seed(time.Now().UTC().UnixNano())
return &session{
cfg: cfg,
closeCh: make(chan bool),
servers: []*server{},
clients: []*client{},
log: &logrus.Logger{
Out: os.Stdout,
Level: logrus.Level(cfg.LogLevel),
Formatter: &logrus.TextFormatter{
FullTimestamp: true,
},
//Formatter: &logrus.JSONFormatter{},
},
}
}
func (sess *session) Consumer(consumerCfg ConsumerConfig) (Consumer, error) {
rec, err := sess.conn.Channel()
if err != nil {
return nil, err
}
err = rec.Qos(consumerCfg.PrefetchCount, consumerCfg.PrefetchSize, consumerCfg.PrefetchGlobal)
if err != nil {
return nil, err
}
srv := &server{
sess: sess,
qs: []*Queue{},
close: make(chan bool),
rec: rec,
}
go func() {
sess.log.Warn("rec channel is closing", <-rec.NotifyClose(make(chan *amqp.Error)))
//srv.Stop()
}()
sess.servers = append(sess.servers, srv)
return srv, nil
}
func (sess *session) createServer(cfg ServerConfig) (*server, error) {
if cfg.RequestX == "" {
cfg.RequestX = defaultReqX
}
if cfg.ResponseX == "" {
cfg.ResponseX = defaultResX
}
rec, err := sess.conn.Channel()
if err != nil {
return nil, err
}
sen, err := sess.conn.Channel()
if err != nil {
return nil, err
}
srv := &server{
sess: sess,
responseX: cfg.ResponseX,
requestX: cfg.RequestX,
qs: []*Queue{},
xs: []*Exchange{},
close: make(chan bool),
sen: sen,
rec: rec,
unbindExAtStop: cfg.UnbindExAtStop,
unbindQsAtStop: cfg.UnbindQsAtStop,
}
senCh := sen.NotifyClose(make(chan *amqp.Error))
recCh := rec.NotifyClose(make(chan *amqp.Error))
go func() {
select {
case err := <-senCh:
sess.log.Warn("sen channel is closing", err)
//rec.Close()
//srv.Stop()
case err := <-recCh:
sess.log.Warn("rec channel is closing", err)
//sen.Close()
//srv.Stop()
}
}()
return srv, nil
}
func (sess *session) Server(cfg ServerConfig) (Server, error) {
srv, err := sess.createServer(cfg)
if err != nil {
return nil, err
}
sess.servers = append(sess.servers, srv)
return srv, nil
}
func (sess *session) createPublisher(cfg PublisherConfig) (*client, error) {
sen, err := sess.conn.Channel()
if err != nil {
return nil, err
}
clt := &client{
sess: sess,
sen: sen,
}
go func() {
sess.log.Warn("sen channel is closing", <-sen.NotifyClose(make(chan *amqp.Error)))
}()
return clt, nil
}
func (sess *session) Publisher(cfg PublisherConfig) (Publisher, error) {
clt, err := sess.createPublisher(cfg)
if err != nil {
return nil, err
}
sess.clients = append(sess.clients, clt)
return clt, nil
}
func (sess *session) createClient(cfg ClientConfig) (*client, error) {
hostname := os.Getenv("HOSTNAME")
if hostname == "" {
hostname = "localhost"
}
correlationId := correlationId(32)
if cfg.RequestX == "" {
cfg.RequestX = defaultReqX
}
if cfg.ResponseX == "" {
cfg.ResponseX = defaultResX
}
responseQ := cfg.ResponseQ
if responseQ == "" {
responseQ = cfg.ResponseX + "." + hostname + "." + correlationId
}
clt := &client{
sess: sess,
responseX: cfg.ResponseX,
requestX: cfg.RequestX,
responseQ: responseQ,
rpcChannels: map[string]chan Message{},
close: make(chan bool),
}
sen, err := sess.conn.Channel()
if err != nil {
return nil, err
}
clt.sen = sen
rec, err := sess.conn.Channel()
if err != nil {
return nil, err
}
clt.rec = rec
senCh := sen.NotifyClose(make(chan *amqp.Error))
recCh := rec.NotifyClose(make(chan *amqp.Error))
go func() {
select {
case err := <-senCh:
sess.log.Warn("sen channel is closing", err)
//rec.Close()
//clt.stop()
case err := <-recCh:
sess.log.Warn("rec channel is closing", err)
//sen.Close()
//clt.stop()
}
}()
clt.run()
return clt, nil
}
func (sess *session) Client(cfg ClientConfig) (Client, error) {
clt, err := sess.createClient(cfg)
if err != nil {
return nil, err
}
sess.clients = append(sess.clients, clt)
return clt, nil
}
func (sess *session) Connect() (err error) {
if sess.cfg.AMQPUrl == "" {
sess.cfg.AMQPUrl = buildUrl(sess.cfg)
}
sess.log.Info("connecting to ", sess.cfg.AMQPUrl)
sess.conn, err = amqp.Dial(sess.cfg.AMQPUrl)
if err != nil {
sess.log.Warn("amqp.Dial", err)
return err
}
go func() {
sess.log.Fatal("connection is closing", <-sess.conn.NotifyClose(make(chan *amqp.Error)))
sess.Close()
}()
return nil
}
func (sess *session) Close() {
sess.closeClients()
sess.closeServers()
sess.conn.Close()
}
func (sess *session) closeServers() {
for _, s := range sess.servers {
s.Stop()
}
}
func (sess *session) closeClients() {
for _, c := range sess.clients {
c.stop()
}
}