This repository has been archived by the owner on Dec 9, 2019. It is now read-only.
/
outbox.go
139 lines (117 loc) · 3.19 KB
/
outbox.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
package gbus
import (
"log"
"sync"
"github.com/streadway/amqp"
)
//AMQPOutbox sends messages to the amqp transport
type AMQPOutbox struct {
channel *amqp.Channel
confirm bool
resendOnNack bool
sequence uint64
ack chan uint64
nack chan uint64
resends chan pendingConfirmation
locker *sync.Mutex
pending map[uint64]pendingConfirmation
stop chan bool
}
func (out *AMQPOutbox) init(amqp *amqp.Channel, confirm, resendOnNack bool) error {
out.stop = make(chan bool)
out.pending = make(map[uint64]pendingConfirmation)
out.locker = &sync.Mutex{}
out.channel = amqp
out.confirm = confirm
if confirm {
out.ack = make(chan uint64, 10)
out.nack = make(chan uint64, 10)
out.resends = make(chan pendingConfirmation)
err := out.channel.Confirm(false /*noWait*/)
if err != nil {
return err
}
if resendOnNack {
out.resendOnNack = resendOnNack
out.channel.NotifyConfirm(out.ack, out.nack)
go out.confirmationLoop()
}
}
return nil
}
func (out *AMQPOutbox) shutdown() {
out.stop <- true
}
//Post implements Outbox.Send
func (out *AMQPOutbox) Post(exchange, routingKey string, amqpMessage amqp.Publishing) (uint64, error) {
out.locker.Lock()
defer out.locker.Unlock()
//generate the delivery tag for this message
nextSequence := out.sequence + 1
if out.confirm {
p := pendingConfirmation{
exchange: exchange,
routingKey: routingKey,
amqpMessage: amqpMessage}
out.pending[nextSequence] = p
}
sendErr := out.sendToChannel(exchange, routingKey, amqpMessage)
if sendErr != nil {
//if an error was received then move the pending confirmation from the pending map
delete(out.pending, nextSequence)
return 0, sendErr
}
// only update the global sequence if the send optation on the channel does not return an error
// so that the global sequence and the channel delivery tag counter stay in sync
out.sequence = nextSequence
return out.sequence, nil
}
func (out *AMQPOutbox) confirmationLoop() {
for {
select {
case <-out.stop:
return
case ack := <-out.ack:
if ack <= 0 {
continue
}
out.locker.Lock()
pending := out.pending[ack]
if pending.deliveryTag > 0 {
log.Printf("ack received for a pending delivery with tag %v", ack)
}
delete(out.pending, ack)
out.locker.Unlock()
case nack := <-out.nack:
if nack <= 0 {
continue
}
log.Printf("nack received for delivery tag %v", nack)
out.locker.Lock()
pending := out.pending[nack]
pending.deliveryTag = nack
out.resends <- pending
delete(out.pending, nack)
out.locker.Unlock()
case resend := <-out.resends:
out.Post(resend.exchange, resend.routingKey, resend.amqpMessage)
}
}
}
func (out *AMQPOutbox) sendToChannel(exchange, routingKey string, amqpMessage amqp.Publishing) error {
return out.channel.Publish(exchange, /*exchange*/
routingKey, /*key*/
false, /*mandatory*/
false, /*immediate*/
amqpMessage /*msg*/)
}
//NotifyConfirm send an amqp notification
func (out *AMQPOutbox) NotifyConfirm(ack, nack chan uint64) {
out.channel.NotifyConfirm(ack, nack)
}
type pendingConfirmation struct {
deliveryTag uint64
exchange string
routingKey string
amqpMessage amqp.Publishing
}