| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package amqp091 |
| 7 |
|
| 8 |
import ( |
| 9 |
"context" |
| 10 |
"sync" |
| 11 |
) |
| 12 |
|
| 13 |
|
| 14 |
type confirms struct { |
| 15 |
m sync.Mutex |
| 16 |
listeners []chan Confirmation |
| 17 |
sequencer map[uint64]Confirmation |
| 18 |
deferredConfirmations *deferredConfirmations |
| 19 |
published uint64 |
| 20 |
publishedMut sync.Mutex |
| 21 |
expecting uint64 |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
func newConfirms() *confirms { |
| 26 |
return &confirms{ |
| 27 |
sequencer: map[uint64]Confirmation{}, |
| 28 |
deferredConfirmations: newDeferredConfirmations(), |
| 29 |
published: 0, |
| 30 |
expecting: 1, |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
func (c *confirms) Listen(l chan Confirmation) { |
| 35 |
c.m.Lock() |
| 36 |
defer c.m.Unlock() |
| 37 |
|
| 38 |
c.listeners = append(c.listeners, l) |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
func (c *confirms) publish() *DeferredConfirmation { |
| 43 |
c.publishedMut.Lock() |
| 44 |
defer c.publishedMut.Unlock() |
| 45 |
|
| 46 |
c.published++ |
| 47 |
return c.deferredConfirmations.Add(c.published) |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
func (c *confirms) unpublish() { |
| 53 |
c.publishedMut.Lock() |
| 54 |
defer c.publishedMut.Unlock() |
| 55 |
c.deferredConfirmations.remove(c.published) |
| 56 |
c.published-- |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
func (c *confirms) confirm(confirmation Confirmation) { |
| 62 |
delete(c.sequencer, c.expecting) |
| 63 |
c.expecting++ |
| 64 |
for _, l := range c.listeners { |
| 65 |
l <- confirmation |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
func (c *confirms) resequence() { |
| 71 |
c.publishedMut.Lock() |
| 72 |
defer c.publishedMut.Unlock() |
| 73 |
|
| 74 |
for c.expecting <= c.published { |
| 75 |
sequenced, found := c.sequencer[c.expecting] |
| 76 |
if !found { |
| 77 |
return |
| 78 |
} |
| 79 |
c.confirm(sequenced) |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
func (c *confirms) One(confirmed Confirmation) { |
| 85 |
c.m.Lock() |
| 86 |
defer c.m.Unlock() |
| 87 |
|
| 88 |
c.deferredConfirmations.Confirm(confirmed) |
| 89 |
|
| 90 |
if c.expecting == confirmed.DeliveryTag { |
| 91 |
c.confirm(confirmed) |
| 92 |
} else { |
| 93 |
c.sequencer[confirmed.DeliveryTag] = confirmed |
| 94 |
} |
| 95 |
c.resequence() |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
func (c *confirms) Multiple(confirmed Confirmation) { |
| 100 |
c.m.Lock() |
| 101 |
defer c.m.Unlock() |
| 102 |
|
| 103 |
c.deferredConfirmations.ConfirmMultiple(confirmed) |
| 104 |
|
| 105 |
for c.expecting <= confirmed.DeliveryTag { |
| 106 |
c.confirm(Confirmation{c.expecting, confirmed.Ack}) |
| 107 |
} |
| 108 |
c.resequence() |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
func (c *confirms) Close() error { |
| 114 |
c.m.Lock() |
| 115 |
defer c.m.Unlock() |
| 116 |
|
| 117 |
c.deferredConfirmations.Close() |
| 118 |
|
| 119 |
for _, l := range c.listeners { |
| 120 |
close(l) |
| 121 |
} |
| 122 |
c.listeners = nil |
| 123 |
return nil |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
func (c *confirms) reset() { |
| 129 |
c.m.Lock() |
| 130 |
defer c.m.Unlock() |
| 131 |
|
| 132 |
c.publishedMut.Lock() |
| 133 |
defer c.publishedMut.Unlock() |
| 134 |
|
| 135 |
c.published = 0 |
| 136 |
c.expecting = 1 |
| 137 |
c.deferredConfirmations.Close() |
| 138 |
c.sequencer = map[uint64]Confirmation{} |
| 139 |
} |
| 140 |
|
| 141 |
type deferredConfirmations struct { |
| 142 |
m sync.Mutex |
| 143 |
confirmations map[uint64]*DeferredConfirmation |
| 144 |
} |
| 145 |
|
| 146 |
func newDeferredConfirmations() *deferredConfirmations { |
| 147 |
return &deferredConfirmations{ |
| 148 |
confirmations: map[uint64]*DeferredConfirmation{}, |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
func (d *deferredConfirmations) Add(tag uint64) *DeferredConfirmation { |
| 153 |
d.m.Lock() |
| 154 |
defer d.m.Unlock() |
| 155 |
|
| 156 |
dc := &DeferredConfirmation{DeliveryTag: tag} |
| 157 |
dc.done = make(chan struct{}) |
| 158 |
d.confirmations[tag] = dc |
| 159 |
return dc |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
func (d *deferredConfirmations) remove(tag uint64) { |
| 164 |
d.m.Lock() |
| 165 |
defer d.m.Unlock() |
| 166 |
dc, found := d.confirmations[tag] |
| 167 |
if !found { |
| 168 |
return |
| 169 |
} |
| 170 |
close(dc.done) |
| 171 |
delete(d.confirmations, tag) |
| 172 |
} |
| 173 |
|
| 174 |
func (d *deferredConfirmations) Confirm(confirmation Confirmation) { |
| 175 |
d.m.Lock() |
| 176 |
defer d.m.Unlock() |
| 177 |
|
| 178 |
dc, found := d.confirmations[confirmation.DeliveryTag] |
| 179 |
if !found { |
| 180 |
|
| 181 |
|
| 182 |
return |
| 183 |
} |
| 184 |
dc.setAck(confirmation.Ack) |
| 185 |
delete(d.confirmations, confirmation.DeliveryTag) |
| 186 |
} |
| 187 |
|
| 188 |
func (d *deferredConfirmations) ConfirmMultiple(confirmation Confirmation) { |
| 189 |
d.m.Lock() |
| 190 |
defer d.m.Unlock() |
| 191 |
|
| 192 |
for k, v := range d.confirmations { |
| 193 |
if k <= confirmation.DeliveryTag { |
| 194 |
v.setAck(confirmation.Ack) |
| 195 |
delete(d.confirmations, k) |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
func (d *deferredConfirmations) Close() { |
| 202 |
d.m.Lock() |
| 203 |
defer d.m.Unlock() |
| 204 |
|
| 205 |
for k, v := range d.confirmations { |
| 206 |
v.setAck(false) |
| 207 |
delete(d.confirmations, k) |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
func (d *DeferredConfirmation) setAck(ack bool) { |
| 214 |
d.ack = ack |
| 215 |
close(d.done) |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
func (d *DeferredConfirmation) Done() <-chan struct{} { |
| 221 |
return d.done |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
func (d *DeferredConfirmation) Acked() bool { |
| 228 |
select { |
| 229 |
case <-d.done: |
| 230 |
default: |
| 231 |
return false |
| 232 |
} |
| 233 |
return d.ack |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
func (d *DeferredConfirmation) Wait() bool { |
| 239 |
<-d.done |
| 240 |
return d.ack |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
func (d *DeferredConfirmation) WaitContext(ctx context.Context) (bool, error) { |
| 247 |
select { |
| 248 |
case <-ctx.Done(): |
| 249 |
return false, ctx.Err() |
| 250 |
case <-d.done: |
| 251 |
} |
| 252 |
return d.ack, nil |
| 253 |
} |