This repository has been archived by the owner on May 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch_acknowledger.go
94 lines (81 loc) · 2.04 KB
/
batch_acknowledger.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
package rabbitmq
type BatchAcknowledger struct {
control chan<- interface{}
input <-chan interface{}
count uint64
maximum uint64
finalTag uint64
finalConsumer interface{}
receivedTag uint64
receivedConsumer interface{}
closing bool
pending *DeliveryReceipt
}
func newAcknowledger(control chan<- interface{}, input <-chan interface{}) *BatchAcknowledger {
return &BatchAcknowledger{control: control, input: input}
}
func (this *BatchAcknowledger) Listen() {
this.listen()
this.acknowledge()
this.control <- acknowledgementCompleted{Acknowledgements: this.count}
}
func (this *BatchAcknowledger) listen() {
for item := range this.input {
this.processItem(item)
if this.isComplete() {
return
}
}
}
func (this *BatchAcknowledger) processItem(entity interface{}) {
switch item := entity.(type) {
case subscriptionClosed:
this.processClosingEvent(item)
case DeliveryReceipt:
this.processAcknowledgment(item)
}
}
func (this *BatchAcknowledger) processClosingEvent(item subscriptionClosed) {
this.closing = true
this.maximum += item.DeliveryCount
this.finalTag = item.LatestDeliveryTag
this.finalConsumer = item.LatestConsumer
}
func (this *BatchAcknowledger) processAcknowledgment(item DeliveryReceipt) {
this.count++
this.receivedTag = item.deliveryTag
this.receivedConsumer = item.channel
if len(this.input) > 0 {
this.pending = &item
} else {
this.pending = nil
acknowledge(item)
}
}
func (this *BatchAcknowledger) acknowledge() {
if this.pending != nil {
acknowledge(*this.pending)
this.pending = nil
}
}
func acknowledge(receipt DeliveryReceipt) {
_ = receipt.channel.AcknowledgeMultipleMessages(receipt.deliveryTag)
}
func (this *BatchAcknowledger) isComplete() bool {
if !this.closing {
return false
}
if len(this.input) > 0 {
return false
}
if this.maximum <= this.count {
return true
}
if this.finalTag != this.receivedTag {
return false
}
if this.finalConsumer != this.receivedConsumer {
return false
}
return true
}