-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathchannel.go
148 lines (135 loc) · 2.67 KB
/
channel.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
package amqp
import (
"context"
"sync"
"time"
"github.com/devimteam/amqp/conn"
"github.com/devimteam/amqp/logger"
"github.com/streadway/amqp"
)
// Channel is a wrapper of *amqp.Channel
type Channel struct {
conn *conn.Connection
channel *amqp.Channel
// Mutex to prevent multiple calls.
callMx sync.Mutex
closed bool
logger logger.Logger
}
func (c *Channel) publish(exchangeName string, msg amqp.Publishing, pub Publish) error {
c.callMx.Lock()
defer c.callMx.Unlock()
return c.channel.Publish(
exchangeName,
pub.Key,
pub.Mandatory,
pub.Immediate,
msg,
)
}
func (c *Channel) consume(queueName string, cfg Consumer) (<-chan amqp.Delivery, error) {
c.callMx.Lock()
defer c.callMx.Unlock()
return c.channel.Consume(
queueName,
cfg.Consumer,
cfg.AutoAck,
cfg.Exclusive,
cfg.NoLocal,
cfg.NoWait,
cfg.Args,
)
}
func (c *Channel) qos(count, size int) error {
c.callMx.Lock()
defer c.callMx.Unlock()
return c.channel.Qos(count, size, false)
}
func (c *Channel) declareExchange(exchange Exchange) error {
c.callMx.Lock()
defer c.callMx.Unlock()
return c.channel.ExchangeDeclare(
exchange.Name,
exchange.Kind,
exchange.Durable,
exchange.AutoDelete,
exchange.Internal,
exchange.NoWait,
exchange.Args,
)
}
func (c *Channel) declareQueue(queue Queue) (amqp.Queue, error) {
c.callMx.Lock()
defer c.callMx.Unlock()
return c.channel.QueueDeclare(
queue.Name,
queue.Durable,
queue.AutoDelete,
queue.Exclusive,
queue.NoWait,
queue.Args,
)
}
func (c *Channel) bind(binding Binding) error {
c.callMx.Lock()
defer c.callMx.Unlock()
return c.channel.QueueBind(
binding.Queue,
binding.Key,
binding.Exchange,
binding.NoWait,
binding.Args,
)
}
func (c *Channel) keepalive(ctx context.Context, timeout time.Duration) {
Loop:
for { // wait for success connection
select {
case <-ctx.Done():
return
default:
if err := c.newChan(timeout); err != nil {
_ = c.logger.Log(err)
continue
}
break Loop
}
}
c.callMx.Unlock()
for ; !c.closed; c.callMx.Unlock() {
select {
case <-ctx.Done():
if err := c.close(); err != nil {
_ = c.logger.Log(err)
}
return
case <-c.channel.NotifyClose(make(chan *amqp.Error)):
c.callMx.Lock()
if c.closed {
break
}
if err := c.newChan(timeout); err != nil {
_ = c.logger.Log(err)
continue
}
}
}
}
func (c *Channel) newChan(timeout time.Duration) error {
err := c.conn.NotifyConnected(timeout)
if err != nil {
return err
}
channel, err := c.conn.Channel()
if err != nil {
return err
}
c.channel = channel
return nil
}
func (c *Channel) close() error {
c.callMx.Lock()
defer c.callMx.Unlock()
c.closed = true
return c.channel.Close()
}