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 pathbroker.go
250 lines (207 loc) · 5.3 KB
/
broker.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package rabbitmq
import (
"net/url"
"sync"
"time"
"github.com/smartystreets/messaging/v2"
)
type Broker struct {
mutex *sync.Mutex
target url.URL
connector Connector
connection Connection
readers []messaging.Reader
writers []messaging.Writer
state uint64
updates func(uint64)
sleep func(time.Duration)
}
func NewBroker(target url.URL, connector Connector) *Broker {
return &Broker{
mutex: &sync.Mutex{},
target: target,
connector: connector,
sleep: sleep,
}
}
func (this *Broker) Notify(callback func(uint64)) {
this.updates = callback
}
func (this *Broker) State() uint64 {
this.mutex.Lock()
state := this.state
this.mutex.Unlock()
return state
}
func (this *Broker) updateState(state uint64) {
this.state = state
updates := this.updates
if updates != nil {
updates(state)
}
}
func (this *Broker) Connect() error {
this.mutex.Lock()
defer this.mutex.Unlock()
if this.state == messaging.Disconnecting {
return messaging.ErrBrokerShuttingDown
} else if this.state == messaging.Disconnected {
this.updateState(messaging.Connecting)
}
return nil
}
func (this *Broker) Disconnect() {
this.mutex.Lock()
defer this.mutex.Unlock()
if this.state == messaging.Disconnecting || this.state == messaging.Disconnected {
return
}
this.updateState(messaging.Disconnecting)
this.initiateReaderShutdown()
this.initiateWriterShutdown()
this.completeShutdown()
}
func (this *Broker) initiateReaderShutdown() {
for _, reader := range this.readers {
reader.Close()
}
}
func (this *Broker) initiateWriterShutdown() {
if len(this.readers) > 0 {
return
}
for _, writer := range this.writers {
writer.Close()
}
this.writers = this.writers[0:0]
}
func (this *Broker) completeShutdown() {
if this.state != messaging.Disconnecting {
return
}
if len(this.readers) > 0 || len(this.writers) > 0 {
return
}
if this.connection != nil {
this.connection.Close()
this.connection = nil
}
this.updateState(messaging.Disconnected)
}
func (this *Broker) removeReader(reader messaging.Reader) {
this.mutex.Lock()
defer this.mutex.Unlock()
for i, item := range this.readers {
if reader != item {
continue
}
this.readers = append(this.readers[:i], this.readers[i+1:]...)
break
}
if this.state != messaging.Disconnecting {
return
}
this.initiateWriterShutdown() // when all readers shutdown processes have been completed
this.completeShutdown()
}
func (this *Broker) removeWriter(writer messaging.Writer) {
for i, item := range this.writers {
if writer != item {
continue
}
this.writers = append(this.writers[:i], this.writers[i+1:]...)
break
}
}
func (this *Broker) OpenReader(queue string, bindings ...string) messaging.Reader {
return this.openReader(queue, bindings)
}
func (this *Broker) OpenTransientReader(bindings []string) messaging.Reader {
return this.openReader("", bindings)
}
func (this *Broker) openReader(queue string, bindings []string) messaging.Reader {
this.mutex.Lock()
defer this.mutex.Unlock()
if this.state == messaging.Disconnecting || this.state == messaging.Disconnected {
return nil
}
reader := newReader(this, queue, bindings)
this.readers = append(this.readers, reader)
return reader
}
func (this *Broker) OpenWriter() messaging.Writer {
writer := this.openWriter(false)
this.writers = append(this.writers, writer)
return writer
}
func (this *Broker) OpenTransactionalWriter() messaging.CommitWriter {
writer := this.openWriter(true).(messaging.CommitWriter)
this.writers = append(this.writers, writer)
return writer
}
func (this *Broker) openWriter(transactional bool) messaging.Writer {
this.mutex.Lock()
defer this.mutex.Unlock()
if this.state == messaging.Disconnecting || this.state == messaging.Disconnected {
return nil
}
if transactional {
return transactionWriter(this)
}
return newWriter(this)
}
func (this *Broker) openChannel(callback func() bool) Channel {
// don't lock for the duration of the loop
// otherwise this can deadlock because we're dependent
// upon the broker to be online/active. By avoiding
// a lock here, we can try to connect and if that fails
// we can still shutdown properly
for this.isActive() {
if !callback() {
break
}
if channel := this.tryOpenChannel(); channel != nil {
return channel
}
this.sleep(time.Second * 4)
}
return nil
}
func (this *Broker) isActive() bool {
this.mutex.Lock()
active := this.state == messaging.Connecting || this.state == messaging.Connected
this.mutex.Unlock()
return active
}
func (this *Broker) tryOpenChannel() Channel {
this.mutex.Lock()
defer this.mutex.Unlock()
if !this.ensureConnection() {
return nil
}
return this.openChannelFromExistingConnection()
}
func (this *Broker) ensureConnection() bool {
if this.connection != nil {
return true
}
var err error
this.connection, err = this.connector.Connect(this.target)
return err == nil
}
func (this *Broker) openChannelFromExistingConnection() Channel {
// remember to only change the state (this.connection, this.state)
// within the protection of this.mutex
channel, err := this.connection.Channel()
if err != nil {
this.connection.Close()
this.connection = nil
this.updateState(messaging.Connecting)
return nil
}
this.updateState(messaging.Connected)
return channel
}
var sleep = func(duration time.Duration) {
time.Sleep(duration)
}