Debian Codegraph @

confirms.go

raw symbols SCIP
1 // Copyright (c) 2021 VMware, Inc. or its affiliates. All Rights Reserved.
2 // Copyright (c) 2012-2021, Sean Treadway, SoundCloud Ltd.
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file.
5
6 package amqp091
7
8 import (
9 "context"
10 "sync"
11 )
12
13 // confirms resequences and notifies one or multiple publisher confirmation listeners
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 // newConfirms allocates a confirms
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 // Publish increments the publishing counter
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 // unpublish decrements the publishing counter and removes the
51 // DeferredConfirmation. It must be called immediately after a publish fails.
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 // confirm confirms one publishing, increments the expecting delivery tag, and
60 // removes bookkeeping for that delivery tag.
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 // resequence confirms any out of order delivered confirmations
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 // One confirms one publishing and all following in the publishing sequence
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 // Multiple confirms all publishings up until the delivery tag
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 // Cleans up the confirms struct and its dependencies.
112 // Closes all listeners, discarding any out of sequence confirmations
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 // reset clears any pending deferred confirmations and resets the sequencer
127 // state for recovery, while keeping the listeners intact.
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 // remove is only used to drop a tag whose publish failed
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 // We should never receive a confirmation for a tag that hasn't
181 // been published, but a test causes this to happen.
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 // Close nacks all pending DeferredConfirmations being blocked by dc.Wait().
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 // setAck sets the acknowledgement status of the confirmation. Note that it must
212 // not be called more than once.
213 func (d *DeferredConfirmation) setAck(ack bool) {
214 d.ack = ack
215 close(d.done)
216 }
217
218 // Done returns the channel that can be used to wait for the publisher
219 // confirmation.
220 func (d *DeferredConfirmation) Done() <-chan struct{} {
221 return d.done
222 }
223
224 // Acked returns the publisher confirmation in a non-blocking manner. It returns
225 // false if the confirmation was not acknowledged yet or received negative
226 // acknowledgement.
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 // Wait blocks until the publisher confirmation. It returns true if the server
237 // successfully received the publishing.
238 func (d *DeferredConfirmation) Wait() bool {
239 <-d.done
240 return d.ack
241 }
242
243 // WaitContext waits until the publisher confirmation. It returns true if the
244 // server successfully received the publishing. If the context expires before
245 // that, ctx.Err() is returned.
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 }