| 1 |
package server |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"os" |
| 6 |
"sync" |
| 7 |
"sync/atomic" |
| 8 |
|
| 9 |
"github.com/NeowayLabs/wabbit" |
| 10 |
"github.com/NeowayLabs/wabbit/utils" |
| 11 |
amqp "github.com/rabbitmq/amqp091-go" |
| 12 |
) |
| 13 |
|
| 14 |
type ( |
| 15 |
Channel struct { |
| 16 |
*VHost |
| 17 |
|
| 18 |
unacked []unackData |
| 19 |
muUnacked *sync.RWMutex |
| 20 |
consumers map[string]consumer |
| 21 |
muConsumer *sync.RWMutex |
| 22 |
|
| 23 |
_ uint32 |
| 24 |
deliveryTagCounter uint64 |
| 25 |
|
| 26 |
confirm bool |
| 27 |
|
| 28 |
publishListeners []chan wabbit.Confirmation |
| 29 |
muPublishListeners *sync.RWMutex |
| 30 |
|
| 31 |
errSpread *utils.ErrBroadcast |
| 32 |
} |
| 33 |
|
| 34 |
unackData struct { |
| 35 |
d wabbit.Delivery |
| 36 |
q *Queue |
| 37 |
} |
| 38 |
|
| 39 |
consumer struct { |
| 40 |
tag string |
| 41 |
deliveries chan wabbit.Delivery |
| 42 |
done chan bool |
| 43 |
} |
| 44 |
) |
| 45 |
|
| 46 |
var consumerSeq uint64 |
| 47 |
|
| 48 |
func uniqueConsumerTag() string { |
| 49 |
return fmt.Sprintf("ctag-%s-%d", os.Args[0], atomic.AddUint64(&consumerSeq, 1)) |
| 50 |
} |
| 51 |
|
| 52 |
func NewChannel(vhost *VHost) *Channel { |
| 53 |
c := Channel{ |
| 54 |
VHost: vhost, |
| 55 |
unacked: make([]unackData, 0, QueueMaxLen), |
| 56 |
muUnacked: &sync.RWMutex{}, |
| 57 |
muConsumer: &sync.RWMutex{}, |
| 58 |
consumers: make(map[string]consumer), |
| 59 |
muPublishListeners: &sync.RWMutex{}, |
| 60 |
errSpread: utils.NewErrBroadcast(), |
| 61 |
} |
| 62 |
|
| 63 |
return &c |
| 64 |
} |
| 65 |
|
| 66 |
func (ch *Channel) QueueInspect(name string) (wabbit.Queue, error) { |
| 67 |
q, ok := ch.queues[name] |
| 68 |
if ok { |
| 69 |
return q, nil |
| 70 |
} |
| 71 |
return nil, fmt.Errorf("no queue named %s", name) |
| 72 |
} |
| 73 |
|
| 74 |
func (ch *Channel) Confirm(noWait bool) error { |
| 75 |
ch.confirm = true |
| 76 |
|
| 77 |
return nil |
| 78 |
} |
| 79 |
|
| 80 |
func (ch *Channel) NotifyPublish(confirm chan wabbit.Confirmation) chan wabbit.Confirmation { |
| 81 |
aux := make(chan wabbit.Confirmation, 2<<8) |
| 82 |
|
| 83 |
ch.muPublishListeners.Lock() |
| 84 |
ch.publishListeners = append(ch.publishListeners, aux) |
| 85 |
ch.muPublishListeners.Unlock() |
| 86 |
|
| 87 |
// aux is set to the maximum size of a queue: 512. |
| 88 |
// In theory it is possible that a publisher could deliver >512 messages |
| 89 |
// on a channel by sending to multiple queues, in which case Publish() |
| 90 |
// will block trying to send confirmations. However this is a pathological |
| 91 |
// case which can be ignored since there should be an attached listener |
| 92 |
// on the other end of the confirm channel. In any case, a buffered queue, |
| 93 |
// while seemingly inefficient is a good enough solution to make sure |
| 94 |
// Publish() doesn't block. |
| 95 |
go func() { |
| 96 |
for c := range aux { |
| 97 |
confirm <- c |
| 98 |
} |
| 99 |
close(confirm) |
| 100 |
}() |
| 101 |
|
| 102 |
return confirm |
| 103 |
} |
| 104 |
|
| 105 |
func (ch *Channel) Publish(exc, route string, msg []byte, opt wabbit.Option) error { |
| 106 |
hdrs, _ := opt["headers"].(amqp.Table) |
| 107 |
messageId, _ := opt["messageId"].(string) |
| 108 |
contentType, _ := opt["contentType"].(string) |
| 109 |
|
| 110 |
d := NewDelivery(ch, |
| 111 |
msg, |
| 112 |
atomic.AddUint64(&ch.deliveryTagCounter, 1), |
| 113 |
messageId, |
| 114 |
wabbit.Option(hdrs), |
| 115 |
contentType, |
| 116 |
) |
| 117 |
|
| 118 |
err := ch.VHost.Publish(exc, route, d, nil) |
| 119 |
|
| 120 |
if err != nil { |
| 121 |
return err |
| 122 |
} |
| 123 |
|
| 124 |
if ch.confirm { |
| 125 |
confirm := Confirmation{ch.deliveryTagCounter, true} |
| 126 |
for _, l := range ch.publishListeners { |
| 127 |
l <- confirm |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
return nil |
| 132 |
} |
| 133 |
|
| 134 |
// Consume starts a fake consumer of queue |
| 135 |
func (ch *Channel) Consume(queue, consumerName string, _ wabbit.Option) (<-chan wabbit.Delivery, error) { |
| 136 |
var ( |
| 137 |
c consumer |
| 138 |
) |
| 139 |
|
| 140 |
if consumerName == "" { |
| 141 |
consumerName = uniqueConsumerTag() |
| 142 |
} |
| 143 |
|
| 144 |
c = consumer{ |
| 145 |
tag: consumerName, |
| 146 |
deliveries: make(chan wabbit.Delivery), |
| 147 |
done: make(chan bool), |
| 148 |
} |
| 149 |
|
| 150 |
ch.muConsumer.RLock() |
| 151 |
|
| 152 |
if c2, found := ch.consumers[consumerName]; found { |
| 153 |
c2.done <- true |
| 154 |
} |
| 155 |
|
| 156 |
ch.consumers[consumerName] = c |
| 157 |
|
| 158 |
ch.muConsumer.RUnlock() |
| 159 |
|
| 160 |
q, ok := ch.queues[queue] |
| 161 |
|
| 162 |
if !ok { |
| 163 |
return nil, fmt.Errorf("Unknown queue '%s'", queue) |
| 164 |
} |
| 165 |
|
| 166 |
go func() { |
| 167 |
for { |
| 168 |
select { |
| 169 |
case <-c.done: |
| 170 |
close(c.deliveries) |
| 171 |
return |
| 172 |
case d := <-q.data: |
| 173 |
// since we keep track of unacked messages for |
| 174 |
// the channel, we need to rebind the delivery |
| 175 |
// to the consumer channel. |
| 176 |
d = NewDelivery(ch, d.Body(), d.DeliveryTag(), d.MessageId(), d.Headers(), d.ContentType()) |
| 177 |
|
| 178 |
ch.addUnacked(d, q) |
| 179 |
|
| 180 |
// sub-select required for cases when |
| 181 |
// client attempts to close the channel |
| 182 |
// concurrently with re-enqueues of messages |
| 183 |
select { |
| 184 |
case c.deliveries <- d: |
| 185 |
case <-c.done: |
| 186 |
close(c.deliveries) |
| 187 |
return |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
}() |
| 192 |
|
| 193 |
return c.deliveries, nil |
| 194 |
} |
| 195 |
|
| 196 |
func (ch *Channel) addUnacked(d wabbit.Delivery, q *Queue) { |
| 197 |
ch.muUnacked.Lock() |
| 198 |
defer ch.muUnacked.Unlock() |
| 199 |
|
| 200 |
ch.unacked = append(ch.unacked, unackData{d, q}) |
| 201 |
} |
| 202 |
|
| 203 |
func (ch *Channel) enqueueUnacked() { |
| 204 |
ch.muUnacked.Lock() |
| 205 |
defer ch.muUnacked.Unlock() |
| 206 |
|
| 207 |
for _, ud := range ch.unacked { |
| 208 |
ud.q.data <- ud.d |
| 209 |
} |
| 210 |
|
| 211 |
ch.unacked = make([]unackData, 0, QueueMaxLen) |
| 212 |
} |
| 213 |
|
| 214 |
func (ch *Channel) Ack(tag uint64, multiple bool) error { |
| 215 |
var ( |
| 216 |
pos int |
| 217 |
ud unackData |
| 218 |
) |
| 219 |
|
| 220 |
if !multiple { |
| 221 |
ch.muUnacked.Lock() |
| 222 |
defer ch.muUnacked.Unlock() |
| 223 |
|
| 224 |
found := false |
| 225 |
for pos, ud = range ch.unacked { |
| 226 |
if ud.d.DeliveryTag() == tag { |
| 227 |
found = true |
| 228 |
break |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
if !found { |
| 233 |
return fmt.Errorf("Delivery tag %d not found", tag) |
| 234 |
} |
| 235 |
|
| 236 |
ch.unacked = ch.unacked[:pos+copy(ch.unacked[pos:], ch.unacked[pos+1:])] |
| 237 |
} else { |
| 238 |
ackMessages := make([]uint64, 0, QueueMaxLen) |
| 239 |
|
| 240 |
ch.muUnacked.Lock() |
| 241 |
|
| 242 |
found := false |
| 243 |
for _, ud = range ch.unacked { |
| 244 |
udTag := ud.d.DeliveryTag() |
| 245 |
|
| 246 |
if udTag <= tag { |
| 247 |
found = true |
| 248 |
ackMessages = append(ackMessages, udTag) |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
ch.muUnacked.Unlock() |
| 253 |
|
| 254 |
if !found { |
| 255 |
return fmt.Errorf("Delivery tag %d not found", tag) |
| 256 |
} |
| 257 |
|
| 258 |
for _, udTag := range ackMessages { |
| 259 |
ch.Ack(udTag, false) |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
return nil |
| 264 |
} |
| 265 |
|
| 266 |
func (ch *Channel) Nack(tag uint64, multiple bool, requeue bool) error { |
| 267 |
var ( |
| 268 |
pos int |
| 269 |
ud unackData |
| 270 |
) |
| 271 |
|
| 272 |
if !multiple { |
| 273 |
ch.muUnacked.Lock() |
| 274 |
defer ch.muUnacked.Unlock() |
| 275 |
found := false |
| 276 |
for pos, ud = range ch.unacked { |
| 277 |
if ud.d.DeliveryTag() == tag { |
| 278 |
found = true |
| 279 |
break |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
if !found { |
| 284 |
return fmt.Errorf("Delivery tag %d not found", tag) |
| 285 |
} |
| 286 |
|
| 287 |
if requeue { |
| 288 |
ud.q.data <- ud.d |
| 289 |
} |
| 290 |
|
| 291 |
ch.unacked = ch.unacked[:pos+copy(ch.unacked[pos:], ch.unacked[pos+1:])] |
| 292 |
} else { |
| 293 |
nackMessages := make([]uint64, 0, QueueMaxLen) |
| 294 |
|
| 295 |
for _, ud = range ch.unacked { |
| 296 |
udTag := ud.d.DeliveryTag() |
| 297 |
|
| 298 |
if udTag <= tag { |
| 299 |
nackMessages = append(nackMessages, udTag) |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
for _, udTag := range nackMessages { |
| 304 |
ch.Nack(udTag, false, requeue) |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
return nil |
| 309 |
} |
| 310 |
|
| 311 |
func (ch *Channel) Reject(tag uint64, requeue bool) error { |
| 312 |
return ch.Nack(tag, false, requeue) |
| 313 |
} |
| 314 |
|
| 315 |
func (ch *Channel) Close() error { |
| 316 |
ch.muConsumer.Lock() |
| 317 |
defer ch.muConsumer.Unlock() |
| 318 |
|
| 319 |
for _, consumer := range ch.consumers { |
| 320 |
consumer.done <- true |
| 321 |
} |
| 322 |
|
| 323 |
ch.consumers = make(map[string]consumer) |
| 324 |
|
| 325 |
// enqueue shall happens only after every consumer of this channel |
| 326 |
// has stopped. |
| 327 |
ch.enqueueUnacked() |
| 328 |
|
| 329 |
ch.muPublishListeners.Lock() |
| 330 |
defer ch.muPublishListeners.Unlock() |
| 331 |
for _, c := range ch.publishListeners { |
| 332 |
close(c) |
| 333 |
} |
| 334 |
ch.publishListeners = []chan wabbit.Confirmation{} |
| 335 |
|
| 336 |
return nil |
| 337 |
} |
| 338 |
|
| 339 |
// NotifyClose publishs notifications about errors in the given channel |
| 340 |
func (ch *Channel) NotifyClose(c chan wabbit.Error) chan wabbit.Error { |
| 341 |
ch.errSpread.Add(c) |
| 342 |
return c |
| 343 |
} |
| 344 |
|
| 345 |
// Cancel closes deliveries for all consumers |
| 346 |
func (ch *Channel) Cancel(consumer string, noWait bool) error { |
| 347 |
ch.Close() |
| 348 |
return nil |
| 349 |
} |