@@ -8,20 +8,21 @@ import (
88 "time"
99)
1010
11- // RabbitMQ struct defines Connection, Channel, queue, delay
11+ // RabbitMQ contains Connection and Channel
1212type RabbitMQ struct {
1313 Connection * amqp.Connection
1414 Channel * amqp.Channel
1515 queue amqp.Queue
16+ dsn string
1617 delay time.Duration
1718 done chan bool
1819}
1920
20- // Init function connect to RabbitMQ and open a channel
21- func Init (user , password , host , port string , reconnectTime time.Duration ) (* RabbitMQ , error ) {
22- url := fmt .Sprintf ("amqp://%s:%s@%s:%s/" , user , password , host , port )
21+ // New connects to RabbitMQ and open a channel
22+ func New (user , password , host , port string , reconnectTime time.Duration ) (* RabbitMQ , error ) {
23+ dsn := fmt .Sprintf ("amqp://%s:%s@%s:%s/" , user , password , host , port )
2324
24- conn , connErr := amqp .Dial (url )
25+ conn , connErr := amqp .Dial (dsn )
2526 if connErr != nil {
2627 return nil , fmt .Errorf ("failed to connect to RabbitMQ: %v" , connErr )
2728 }
@@ -32,15 +33,16 @@ func Init(user, password, host, port string, reconnectTime time.Duration) (*Rabb
3233 }
3334
3435 done := make (chan bool )
35- rabbitMQ := & RabbitMQ {Connection : conn , Channel : ch , delay : reconnectTime , done : done }
3636
37- go reconnectConnection (rabbitMQ , url )
38- go reconnectChannel (rabbitMQ )
37+ rabbitMQ := & RabbitMQ {Connection : conn , Channel : ch , dsn : dsn , delay : reconnectTime , done : done }
38+
39+ rabbitMQ .reconnectConnection ()
40+ rabbitMQ .reconnectChannel ()
3941
4042 return rabbitMQ , nil
4143}
4244
43- // ExchangeDeclare function declare an exchange
45+ // ExchangeDeclare declare an exchange
4446func (r * RabbitMQ ) ExchangeDeclare (name , exchangeType string , durable , autoDelete , internal , noWait bool , arguments amqp.Table ) error {
4547 err := r .Channel .ExchangeDeclare (name , exchangeType , durable , autoDelete , internal , noWait , arguments )
4648 if err != nil {
@@ -49,7 +51,7 @@ func (r *RabbitMQ) ExchangeDeclare(name, exchangeType string, durable, autoDelet
4951 return nil
5052}
5153
52- // QueueDeclare function declare a queue
54+ // QueueDeclare declare a queue
5355func (r * RabbitMQ ) QueueDeclare (name string , durable , autoDelete , exclusive , noWait bool , arguments amqp.Table ) error {
5456 queue , queueErr := r .Channel .QueueDeclare (name , durable , autoDelete , exclusive , noWait , arguments )
5557 if queueErr != nil {
@@ -59,7 +61,7 @@ func (r *RabbitMQ) QueueDeclare(name string, durable, autoDelete, exclusive, noW
5961 return nil
6062}
6163
62- // QueueBind function bind a queue
64+ // QueueBind bind a queue
6365func (r * RabbitMQ ) QueueBind (key , exchangeName string , noWait bool , arguments amqp.Table ) error {
6466 err := r .Channel .QueueBind (r .queue .Name , key , exchangeName , noWait , arguments )
6567 if err != nil {
@@ -68,7 +70,7 @@ func (r *RabbitMQ) QueueBind(key, exchangeName string, noWait bool, arguments am
6870 return nil
6971}
7072
71- // Qos function controls how many messages or how many bytes the server will try to keep on
73+ // Qos controls how many messages or how many bytes the server will try to keep on
7274// the network for consumers before receiving delivery ack
7375func (r * RabbitMQ ) Qos (prefetchCount , prefetchSize int , global bool ) error {
7476 err := r .Channel .Qos (prefetchCount , prefetchSize , global )
@@ -78,7 +80,7 @@ func (r *RabbitMQ) Qos(prefetchCount, prefetchSize int, global bool) error {
7880 return nil
7981}
8082
81- // Consume function starts delivering queued messages
83+ // Consume starts delivering queued messages
8284func (r * RabbitMQ ) Consume (consumer string , autoAck , exclusive , noLocal , noWait bool , arguments amqp.Table ) (<- chan amqp.Delivery , error ) {
8385 deliveries := make (chan amqp.Delivery )
8486
@@ -102,5 +104,4 @@ func (r *RabbitMQ) Consume(consumer string, autoAck, exclusive, noLocal, noWait
102104 }()
103105
104106 return deliveries , nil
105-
106107}
0 commit comments