@@ -5,8 +5,6 @@ package rabbitmq
55import (
66 "encoding/base64"
77 "encoding/json"
8- "fmt"
9- "log"
108 "time"
119
1210 amqp "github.com/rabbitmq/amqp091-go"
@@ -61,7 +59,7 @@ func WithClient(c *amqp.Connection) BrokerOption {
6159
6260// NewBroker creates a broker backed by RabbitMQ.
6361// By default, it connects to localhost.
64- func NewBroker (options ... BrokerOption ) * Broker {
62+ func NewBroker (options ... BrokerOption ) ( * Broker , error ) {
6563 br := Broker {
6664 amqpUri : DefaultAmqpUri ,
6765 receiveTimeout : DefaultReceiveTimeout * time .Second ,
@@ -75,32 +73,34 @@ func NewBroker(options ...BrokerOption) *Broker {
7573 if br .conn == nil {
7674 conn , err := amqp .Dial (br .amqpUri )
7775 if err != nil {
78- log .Panicf ("Failed to connect to RabbitMQ: %s" , err )
79- return nil
76+ return nil , err
8077 }
78+
8179 br .conn = conn
8280 }
8381
8482 channel , err := br .conn .Channel ()
8583 if err != nil {
86- log .Panicf ("Failed to open a channel: %s" , err )
87- return nil
84+ return nil , err
8885 }
86+
8987 br .channel = channel
9088
91- return & br
89+ return & br , nil
9290}
9391
9492// Send inserts the specified message at the head of the queue.
9593// Note, the method is safe to call concurrently.
9694func (br * Broker ) Send (m []byte , q string ) error {
97- var headers map [string ]interface {}
98- var body []byte
99- var contentType string
100- var contentEncoding string
101- var deliveryMode uint8
102- var correlationId string
103- var replyTo string
95+ var (
96+ headers map [string ]interface {}
97+ body []byte
98+ contentType string
99+ contentEncoding string
100+ deliveryMode uint8
101+ correlationId string
102+ replyTo string
103+ )
104104
105105 if br .rawMode {
106106 headers = make (amqp.Table )
@@ -131,7 +131,7 @@ func (br *Broker) Send(m []byte, q string) error {
131131 replyTo = properties_in ["reply_to" ].(string )
132132 }
133133
134- err := br .channel .Publish (
134+ return br .channel .Publish (
135135 "" , // exchange
136136 q , // routing key
137137 false , // mandatory
@@ -145,21 +145,22 @@ func (br *Broker) Send(m []byte, q string) error {
145145 ReplyTo : replyTo ,
146146 Body : body ,
147147 })
148-
149- return err
150148}
151149
152150// Observe sets the queues from which the tasks should be received.
153151// Note, the method is not concurrency safe.
154- func (br * Broker ) Observe (queues []string ) {
152+ func (br * Broker ) Observe (queues []string ) error {
155153 br .queues = queues
156- for _ , queue := range queues {
157- durable := true
158- autoDelete := false
159- exclusive := false
160- noWait := false
161154
155+ var (
156+ durable = true
157+ autoDelete = false
158+ exclusive = false
159+ noWait = false
160+ )
161+ for _ , queue := range queues {
162162 // Check whether the queue exists.
163+ // If the queue doesn't exist, attempt to create it.
163164 _ , err := br .channel .QueueDeclarePassive (
164165 queue ,
165166 durable ,
@@ -168,32 +169,33 @@ func (br *Broker) Observe(queues []string) {
168169 noWait ,
169170 nil ,
170171 )
171-
172- // If the queue doesn't exist, attempt to create it.
173172 if err != nil {
174- // QueueDeclarePassive() will close the channel if the queue does not exist, so we have to create a new channel when this happens.
173+ // QueueDeclarePassive() will close the channel if the queue does not exist,
174+ // so we have to create a new channel when this happens.
175175 if br .channel .IsClosed () {
176176 channel , err := br .conn .Channel ()
177177 if err != nil {
178- log . Panicf ( "Failed to open a channel: %s" , err )
178+ return err
179179 }
180+
180181 br .channel = channel
181182 }
182183
183- _ , err : = br .channel .QueueDeclare (
184+ _ , err = br .channel .QueueDeclare (
184185 queue ,
185186 durable ,
186187 autoDelete ,
187188 exclusive ,
188189 noWait ,
189190 nil ,
190191 )
191-
192192 if err != nil {
193- log . Panicf ( "Failed to declare a queue: %s" , err )
193+ return err
194194 }
195195 }
196196 }
197+
198+ return nil
197199}
198200
199201// Receive fetches a Celery task message from a tail of one of the queues in RabbitMQ.
@@ -205,8 +207,8 @@ func (br *Broker) Receive() ([]byte, error) {
205207
206208 var err error
207209
208- delivery , delivery_exists := br .delivery [queue ]
209- if ! delivery_exists {
210+ delivery , deliveryExists := br .delivery [queue ]
211+ if ! deliveryExists {
210212 delivery , err = br .channel .Consume (
211213 queue , // queue
212214 "" , // consumer
@@ -216,7 +218,6 @@ func (br *Broker) Receive() ([]byte, error) {
216218 false , // noWait
217219 nil , // args
218220 )
219-
220221 if err != nil {
221222 return nil , err
222223 }
@@ -254,10 +255,9 @@ func (br *Broker) Receive() ([]byte, error) {
254255 var result []byte
255256 result , err := json .Marshal (imsg )
256257 if err != nil {
257- err_str := fmt .Errorf ("%w" , err )
258- log .Printf ("json encode: %s" , err_str )
259258 return nil , err
260259 }
260+
261261 return result , nil
262262
263263 case <- time .After (br .receiveTimeout ):
0 commit comments