| 1 |
package amqp |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
|
| 6 |
"github.com/NeowayLabs/wabbit" |
| 7 |
"github.com/NeowayLabs/wabbit/utils" |
| 8 |
amqp "github.com/rabbitmq/amqp091-go" |
| 9 |
) |
| 10 |
|
| 11 |
// Channel is a wrapper channel structure for amqp.Channel |
| 12 |
type Channel struct { |
| 13 |
*amqp.Channel |
| 14 |
} |
| 15 |
|
| 16 |
func (ch *Channel) Publish(exc, route string, msg []byte, opt wabbit.Option) error { |
| 17 |
amqpOpt, err := utils.ConvertOpt(opt) |
| 18 |
|
| 19 |
if err != nil { |
| 20 |
return err |
| 21 |
} |
| 22 |
|
| 23 |
amqpOpt.Body = msg |
| 24 |
|
| 25 |
return ch.Channel.Publish( |
| 26 |
exc, // publish to an exchange |
| 27 |
route, // routing to 0 or more queues |
| 28 |
false, // mandatory |
| 29 |
false, // immediate |
| 30 |
amqpOpt, |
| 31 |
) |
| 32 |
} |
| 33 |
|
| 34 |
func (ch *Channel) Confirm(noWait bool) error { |
| 35 |
return ch.Channel.Confirm(noWait) |
| 36 |
} |
| 37 |
|
| 38 |
func (ch *Channel) NotifyPublish(confirm chan wabbit.Confirmation) chan wabbit.Confirmation { |
| 39 |
amqpConfirms := ch.Channel.NotifyPublish(make(chan amqp.Confirmation, cap(confirm))) |
| 40 |
|
| 41 |
go func() { |
| 42 |
for c := range amqpConfirms { |
| 43 |
confirm <- Confirmation{c} |
| 44 |
} |
| 45 |
|
| 46 |
close(confirm) |
| 47 |
}() |
| 48 |
|
| 49 |
return confirm |
| 50 |
} |
| 51 |
|
| 52 |
func (ch *Channel) Consume(queue, consumer string, opt wabbit.Option) (<-chan wabbit.Delivery, error) { |
| 53 |
var ( |
| 54 |
autoAck, exclusive, noLocal, noWait bool |
| 55 |
args amqp.Table |
| 56 |
) |
| 57 |
|
| 58 |
if v, ok := opt["autoAck"]; ok { |
| 59 |
autoAck, ok = v.(bool) |
| 60 |
|
| 61 |
if !ok { |
| 62 |
return nil, errors.New("durable option is of type bool") |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
if v, ok := opt["exclusive"]; ok { |
| 67 |
exclusive, ok = v.(bool) |
| 68 |
|
| 69 |
if !ok { |
| 70 |
return nil, errors.New("exclusive option is of type bool") |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
if v, ok := opt["noLocal"]; ok { |
| 75 |
noLocal, ok = v.(bool) |
| 76 |
|
| 77 |
if !ok { |
| 78 |
return nil, errors.New("noLocal option is of type bool") |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
if v, ok := opt["noWait"]; ok { |
| 83 |
noWait, ok = v.(bool) |
| 84 |
|
| 85 |
if !ok { |
| 86 |
return nil, errors.New("noWait option is of type bool") |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
if v, ok := opt["args"]; ok { |
| 91 |
args, ok = v.(amqp.Table) |
| 92 |
|
| 93 |
if !ok { |
| 94 |
return nil, errors.New("args is of type amqp.Table") |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
amqpd, err := ch.Channel.Consume(queue, consumer, autoAck, exclusive, noLocal, noWait, args) |
| 99 |
|
| 100 |
if err != nil { |
| 101 |
return nil, err |
| 102 |
} |
| 103 |
|
| 104 |
deliveries := make(chan wabbit.Delivery) |
| 105 |
|
| 106 |
go func() { |
| 107 |
for d := range amqpd { |
| 108 |
delivery := d |
| 109 |
deliveries <- &Delivery{&delivery} |
| 110 |
} |
| 111 |
|
| 112 |
close(deliveries) |
| 113 |
}() |
| 114 |
|
| 115 |
return deliveries, nil |
| 116 |
} |
| 117 |
|
| 118 |
func (ch *Channel) ExchangeDeclare(name, kind string, opt wabbit.Option) error { |
| 119 |
return ch.exchangeDeclare(name, kind, false, opt) |
| 120 |
} |
| 121 |
|
| 122 |
func (ch *Channel) ExchangeDeclarePassive(name, kind string, opt wabbit.Option) error { |
| 123 |
return ch.exchangeDeclare(name, kind, true, opt) |
| 124 |
} |
| 125 |
|
| 126 |
func (ch *Channel) exchangeDeclare(name, kind string, passive bool, opt wabbit.Option) error { |
| 127 |
var ( |
| 128 |
durable, autoDelete, internal, noWait bool |
| 129 |
args amqp.Table |
| 130 |
) |
| 131 |
|
| 132 |
if v, ok := opt["durable"]; ok { |
| 133 |
durable, ok = v.(bool) |
| 134 |
|
| 135 |
if !ok { |
| 136 |
return errors.New("durable option is of type bool") |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
if v, ok := opt["autoDelete"]; ok { |
| 141 |
autoDelete, ok = v.(bool) |
| 142 |
|
| 143 |
if !ok { |
| 144 |
return errors.New("autoDelete option is of type bool") |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
if v, ok := opt["internal"]; ok { |
| 149 |
internal, ok = v.(bool) |
| 150 |
|
| 151 |
if !ok { |
| 152 |
return errors.New("internal option is of type bool") |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
if v, ok := opt["noWait"]; ok { |
| 157 |
noWait, ok = v.(bool) |
| 158 |
|
| 159 |
if !ok { |
| 160 |
return errors.New("noWait option is of type bool") |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
if v, ok := opt["args"]; ok { |
| 165 |
args, ok = v.(amqp.Table) |
| 166 |
|
| 167 |
if !ok { |
| 168 |
return errors.New("args is of type amqp.Table") |
| 169 |
} |
| 170 |
} |
| 171 |
if passive { |
| 172 |
return ch.Channel.ExchangeDeclarePassive(name, kind, durable, autoDelete, internal, noWait, args) |
| 173 |
} |
| 174 |
return ch.Channel.ExchangeDeclare(name, kind, durable, autoDelete, internal, noWait, args) |
| 175 |
} |
| 176 |
|
| 177 |
func (ch *Channel) QueueInspect(name string) (wabbit.Queue, error) { |
| 178 |
q, err := ch.Channel.QueueInspect(name) |
| 179 |
return &Queue{&q}, err |
| 180 |
} |
| 181 |
|
| 182 |
func (ch *Channel) QueueUnbind(name, route, exchange string, _ wabbit.Option) error { |
| 183 |
return ch.Channel.QueueUnbind(name, route, exchange, nil) |
| 184 |
} |
| 185 |
|
| 186 |
// QueueBind binds the route key to queue |
| 187 |
func (ch *Channel) QueueBind(name, key, exchange string, opt wabbit.Option) error { |
| 188 |
var ( |
| 189 |
noWait bool |
| 190 |
args amqp.Table |
| 191 |
) |
| 192 |
|
| 193 |
if v, ok := opt["noWait"]; ok { |
| 194 |
noWait, ok = v.(bool) |
| 195 |
|
| 196 |
if !ok { |
| 197 |
return errors.New("noWait option is of type bool") |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
if v, ok := opt["args"]; ok { |
| 202 |
args, ok = v.(amqp.Table) |
| 203 |
|
| 204 |
if !ok { |
| 205 |
return errors.New("args is of type amqp.Table") |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
return ch.Channel.QueueBind(name, key, exchange, noWait, args) |
| 210 |
} |
| 211 |
|
| 212 |
// QueueDeclare declares a new AMQP queue |
| 213 |
func (ch *Channel) QueueDeclare(name string, opt wabbit.Option) (wabbit.Queue, error) { |
| 214 |
return ch.queueDeclare(name, false, opt) |
| 215 |
} |
| 216 |
|
| 217 |
// QueueDeclarePassive declares an existing AMQP queue |
| 218 |
func (ch *Channel) QueueDeclarePassive(name string, opt wabbit.Option) (wabbit.Queue, error) { |
| 219 |
return ch.queueDeclare(name, true, opt) |
| 220 |
} |
| 221 |
|
| 222 |
func (ch *Channel) queueDeclare(name string, passive bool, opt wabbit.Option) (wabbit.Queue, error) { |
| 223 |
var ( |
| 224 |
durable, autoDelete, exclusive, noWait bool |
| 225 |
args amqp.Table |
| 226 |
) |
| 227 |
|
| 228 |
if v, ok := opt["durable"]; ok { |
| 229 |
durable, ok = v.(bool) |
| 230 |
|
| 231 |
if !ok { |
| 232 |
return nil, errors.New("durable option is of type bool") |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
if v, ok := opt["autoDelete"]; ok { |
| 237 |
autoDelete, ok = v.(bool) |
| 238 |
|
| 239 |
if !ok { |
| 240 |
return nil, errors.New("autoDelete option is of type bool") |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
if v, ok := opt["exclusive"]; ok { |
| 245 |
exclusive, ok = v.(bool) |
| 246 |
|
| 247 |
if !ok { |
| 248 |
return nil, errors.New("Exclusive option is of type bool") |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
if v, ok := opt["noWait"]; ok { |
| 253 |
noWait, ok = v.(bool) |
| 254 |
|
| 255 |
if !ok { |
| 256 |
return nil, errors.New("noWait option is of type bool") |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
if v, ok := opt["args"]; ok { |
| 261 |
args, ok = v.(amqp.Table) |
| 262 |
|
| 263 |
if !ok { |
| 264 |
return nil, errors.New("args is of type amqp.Table") |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
var q amqp.Queue |
| 269 |
var err error |
| 270 |
if passive { |
| 271 |
q, err = ch.Channel.QueueDeclarePassive(name, durable, autoDelete, exclusive, noWait, args) |
| 272 |
} else { |
| 273 |
q, err = ch.Channel.QueueDeclare(name, durable, autoDelete, exclusive, noWait, args) |
| 274 |
} |
| 275 |
|
| 276 |
if err != nil { |
| 277 |
return nil, err |
| 278 |
} |
| 279 |
|
| 280 |
return &Queue{&q}, nil |
| 281 |
} |
| 282 |
|
| 283 |
func (ch *Channel) QueueDelete(name string, opt wabbit.Option) (int, error) { |
| 284 |
var ( |
| 285 |
ifUnused, ifEmpty, noWait bool |
| 286 |
) |
| 287 |
|
| 288 |
if v, ok := opt["ifUnused"]; ok { |
| 289 |
ifUnused, ok = v.(bool) |
| 290 |
|
| 291 |
if !ok { |
| 292 |
return 0, errors.New("ifUnused option is of type bool") |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
if v, ok := opt["ifEmpty"]; ok { |
| 297 |
ifEmpty, ok = v.(bool) |
| 298 |
|
| 299 |
if !ok { |
| 300 |
return 0, errors.New("ifEmpty option is of type bool") |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
if v, ok := opt["noWait"]; ok { |
| 305 |
noWait, ok = v.(bool) |
| 306 |
|
| 307 |
if !ok { |
| 308 |
return 0, errors.New("noWait option is of type bool") |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
return ch.Channel.QueueDelete(name, ifUnused, ifEmpty, noWait) |
| 313 |
} |
| 314 |
|
| 315 |
// Qos controls how many bytes or messages will be handled by channel or connection. |
| 316 |
func (ch *Channel) Qos(prefetchCount, prefetchSize int, global bool) error { |
| 317 |
return ch.Channel.Qos(prefetchCount, prefetchSize, global) |
| 318 |
} |
| 319 |
|
| 320 |
// NotifyClose registers a listener for close events. |
| 321 |
// For more information see: https://godoc.org/github.com/rabbitmq/amqp091-go#Channel.NotifyClose |
| 322 |
func (ch *Channel) NotifyClose(c chan wabbit.Error) chan wabbit.Error { |
| 323 |
amqpErr := ch.Channel.NotifyClose(make(chan *amqp.Error, cap(c))) |
| 324 |
|
| 325 |
go func() { |
| 326 |
for err := range amqpErr { |
| 327 |
var ne wabbit.Error |
| 328 |
if err != nil { |
| 329 |
ne = utils.NewError( |
| 330 |
err.Code, |
| 331 |
err.Reason, |
| 332 |
err.Server, |
| 333 |
err.Recover, |
| 334 |
) |
| 335 |
} else { |
| 336 |
ne = nil |
| 337 |
} |
| 338 |
c <- ne |
| 339 |
} |
| 340 |
close(c) |
| 341 |
}() |
| 342 |
|
| 343 |
return c |
| 344 |
} |