Skip to content
This repository was archived by the owner on Dec 20, 2023. It is now read-only.

Commit 6c40ad2

Browse files
Merge pull request #9 from C2FO/monkey_patch_reconnet
Monkey patch reconnect due to big in node-amqp
2 parents e059c1d + 3fee0a8 commit 6c40ad2

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

lib/index.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function errorHandler(err) {
2121
}
2222

2323
var connect = (function connectWrapper() {
24-
var connectionPromise, connection, isConnected = false;
24+
var connectionPromise, connection, isConnected = false, connectOpts = {};
2525

2626
function ready() {
2727
isConnected = true;
@@ -41,16 +41,29 @@ var connect = (function connectWrapper() {
4141
function checkConnected() {
4242
if (!isConnected) {
4343
connectionError(new Error("Connection closed prematurely, please check your credentials"));
44+
} else if (connectOpts.reconnect){
45+
// monkey patch b/c node-amqp seems to be not maintained anymore based
46+
// upon number of open issues, comments within issues stating it is no
47+
// longer maintained and the age of open pull requests. node-amqp only
48+
// publishes an error on the "end" event if the "ready" event has not
49+
// yet been fired which means you probably have an issue authing with
50+
// Rabbit. They should also have an else block and look to see if the
51+
// socket is destroyed and if so, emit an error to which node-amqp
52+
// will attempt to reconnect using the specified strategy.
53+
connection.emit("error", new Error("Connection closed, attempting to reconnect"));
4454
}
4555
}
4656

4757
return function connect(url, opts) {
4858
if (!connectionPromise) {
59+
connectOpts = opts;
4960
connectionPromise = new Promise();
5061
connection = amqp.createConnection(url, opts);
5162
connection.once('ready', ready);
5263
connection.once('error', connectionError);
53-
connection.once("close", checkConnected);
64+
// use on instead of once for `close` so we can handle multiple
65+
// subsequent disconnects.
66+
connection.on("close", checkConnected);
5467
}
5568
return connectionPromise;
5669
};
@@ -150,7 +163,8 @@ var Hare = comb.define(_Options, {
150163

151164
"static": {
152165
OPTIONS: ["defaultExchangeName", "reconnect", "reconnectBackoffStrategy",
153-
"reconnectExponentialLimit", "reconnectExponentialLimit", "reconnectBackoffTime"]
166+
"reconnectExponentialLimit", "reconnectExponentialLimit", "reconnectBackoffTime",
167+
"heartbeatForceReconnect"]
154168
}
155169

156170
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hare",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "Wrapper around amqp to make common patterns easier",
55
"keywords": [
66
"amqp",

0 commit comments

Comments
 (0)