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 pathchannel_reader.go
105 lines (91 loc) · 2.66 KB
/
channel_reader.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
package rabbitmq
import (
"sync"
"github.com/smartystreets/messaging/v2"
)
type ChannelReader struct {
mutex *sync.Mutex
controller Controller
queue string
bindings []string
control chan interface{}
acknowledgements chan interface{}
deliveries chan messaging.Delivery
shutdown bool
shutdownRequested bool
deliveryCount uint64
}
func newReader(controller Controller, queue string, bindings []string) *ChannelReader {
return &ChannelReader{
mutex: &sync.Mutex{},
controller: controller,
queue: queue,
bindings: bindings,
control: make(chan interface{}, 32),
acknowledgements: make(chan interface{}, 1024*2-1),
deliveries: make(chan messaging.Delivery, 1024*32-1),
}
}
func (this *ChannelReader) Listen() {
acknowledger := newAcknowledger(this.control, this.acknowledgements)
go acknowledger.Listen()
for this.listen() {
}
close(this.deliveries)
this.controller.removeReader(this)
}
func (this *ChannelReader) listen() bool {
channel := this.controller.openChannel(this.isActive)
if channel == nil {
return false // broker no longer allowed to give me a channel, it has been manually closed
}
subscription := this.subscribe(channel)
for element := range this.control {
switch item := element.(type) {
case shutdownRequested:
this.shutdown = true
subscription.Close()
case subscriptionClosed:
this.deliveryCount += item.DeliveryCount
if this.shutdown {
// keep channel alive and gracefully stop acknowledgement listener
item.DeliveryCount = this.deliveryCount
this.acknowledgements <- item
this.deliveryCount = 0
} else {
// channel failure; reconnect
channel.Close()
return true
}
case acknowledgementCompleted:
channel.Close() // we don't need the channel anymore
return false // the shutdown process for this reader is complete
}
}
return true
}
func (this *ChannelReader) subscribe(channel Channel) *Subscription {
subscription := newSubscription(channel, this.queue, this.bindings, this.control, this.deliveries)
go subscription.Listen()
return subscription
}
func (this *ChannelReader) Close() {
this.mutex.Lock()
if !this.shutdownRequested {
this.control <- shutdownRequested{}
this.shutdownRequested = true
}
this.mutex.Unlock()
}
func (this *ChannelReader) isActive() bool {
this.mutex.Lock()
active := !this.shutdownRequested
this.mutex.Unlock()
return active
}
func (this *ChannelReader) Deliveries() <-chan messaging.Delivery {
return this.deliveries
}
func (this *ChannelReader) Acknowledgements() chan<- interface{} {
return this.acknowledgements
}