37
37
errShutdown = errors .New ("session is shutting down" )
38
38
)
39
39
40
- // New creates a new consumer state instance, and automatically
41
- // attempts to connect to the server.
42
40
func New (name string , addr string ) * Session {
43
41
session := Session {
44
42
logger : log .New (os .Stdout , "" , log .LstdFlags ),
@@ -49,8 +47,6 @@ func New(name string, addr string) *Session {
49
47
return & session
50
48
}
51
49
52
- // handleReconnect will wait for a connection error on
53
- // notifyConnClose, and then continuously attempt to reconnect.
54
50
func (session * Session ) handleReconnect (addr string ) {
55
51
for {
56
52
session .isReady = false
@@ -75,7 +71,6 @@ func (session *Session) handleReconnect(addr string) {
75
71
}
76
72
}
77
73
78
- // connect will create a new AMQP connection
79
74
func (session * Session ) connect (addr string ) (* amqp.Connection , error ) {
80
75
conn , err := amqp .Dial (addr )
81
76
@@ -88,8 +83,6 @@ func (session *Session) connect(addr string) (*amqp.Connection, error) {
88
83
return conn , nil
89
84
}
90
85
91
- // handleReconnect will wait for a channel error
92
- // and then continuously attempt to re-initialize both channels
93
86
func (session * Session ) handleReInit (conn * amqp.Connection ) bool {
94
87
for {
95
88
session .isReady = false
@@ -119,7 +112,6 @@ func (session *Session) handleReInit(conn *amqp.Connection) bool {
119
112
}
120
113
}
121
114
122
- // init will initialize channel & declare queue
123
115
func (session * Session ) init (conn * amqp.Connection ) error {
124
116
ch , err := conn .Channel ()
125
117
@@ -129,18 +121,6 @@ func (session *Session) init(conn *amqp.Connection) error {
129
121
130
122
err = ch .Confirm (false )
131
123
132
- if err != nil {
133
- return err
134
- }
135
- _ , err = ch .QueueDeclare (
136
- session .name ,
137
- true , // Durable
138
- false , // Delete when unused
139
- false , // Exclusive
140
- false , // No-wait
141
- nil ,
142
- )
143
-
144
124
if err != nil {
145
125
return err
146
126
}
@@ -149,54 +129,15 @@ func (session *Session) init(conn *amqp.Connection) error {
149
129
session .isReady = true
150
130
log .Println ("Setup!" )
151
131
152
- /*
153
- if err := channel.ExchangeDeclare(
154
- a.config.Create.ExchangeName,
155
- a.config.Create.ExchangeType,
156
- true,
157
- false,
158
- false,
159
- false,
160
- nil,
161
- ); err != nil {
162
- return errors.Wrap(err, "failed to declare exchange")
163
- }
164
-
165
- if _, err := channel.QueueDeclare(
166
- a.config.Create.QueueName,
167
- true,
168
- false,
169
- false,
170
- false,
171
- amqp.Table{"x-queue-mode": "lazy"},
172
- ); err != nil {
173
- return errors.Wrap(err, "failed to declare queue")
174
- }
175
-
176
- if err := channel.QueueBind(
177
- a.config.Create.QueueName,
178
- a.config.Create.RoutingKey,
179
- a.config.Create.ExchangeName,
180
- false,
181
- nil,
182
- ); err != nil {
183
- return errors.Wrap(err, "failed to bind queue")
184
- }
185
-
186
- */
187
132
return nil
188
133
}
189
134
190
- // changeConnection takes a new connection to the queue,
191
- // and updates the close listener to reflect this.
192
135
func (session * Session ) changeConnection (connection * amqp.Connection ) {
193
136
session .connection = connection
194
137
session .notifyConnClose = make (chan * amqp.Error )
195
138
session .connection .NotifyClose (session .notifyConnClose )
196
139
}
197
140
198
- // changeChannel takes a new channel to the queue,
199
- // and updates the channel listeners to reflect this.
200
141
func (session * Session ) changeChannel (channel * amqp.Channel ) {
201
142
session .channel = channel
202
143
session .notifyChanClose = make (chan * amqp.Error )
@@ -205,11 +146,6 @@ func (session *Session) changeChannel(channel *amqp.Channel) {
205
146
session .channel .NotifyPublish (session .notifyConfirm )
206
147
}
207
148
208
- // Push will push data onto the queue, and wait for a confirm.
209
- // If no confirms are received until within the resendTimeout,
210
- // it continuously re-sends messages until a confirm is received.
211
- // This will block until the server sends a confirm. Errors are
212
- // only returned if the push action itself fails, see UnsafePush.
213
149
func (session * Session ) Push (data []byte ) error {
214
150
if ! session .isReady {
215
151
return errors .New ("failed to push: not connected" )
@@ -228,21 +164,14 @@ func (session *Session) Push(data []byte) error {
228
164
select {
229
165
case confirm := <- session .notifyConfirm :
230
166
if confirm .Ack {
231
- //session.logger.Println("Push confirmed!")
232
167
return nil
233
168
} else {
234
- //session.logger.Println("Push error confirmed!")
235
169
}
236
170
case <- time .After (resendDelay ):
237
171
}
238
- //session.logger.Println("Push didn't confirm. Retrying...")
239
172
}
240
173
}
241
174
242
- // UnsafePush will push to the queue without checking for
243
- // confirmation. It returns an error if it fails to connect.
244
- // No guarantees are provided for whether the server will
245
- // recieve the message.
246
175
func (session * Session ) UnsafePush (data []byte ) error {
247
176
if ! session .isReady {
248
177
return errNotConnected
@@ -264,7 +193,6 @@ func (session *Session) UnsafePush(data []byte) error {
264
193
return err
265
194
}
266
195
267
- // Close will cleanly shutdown the channel and connection.
268
196
func (session * Session ) Close () error {
269
197
if ! session .isReady {
270
198
return errAlreadyClosed
0 commit comments