amqp-connection-manager
Advanced tools
Comparing version 3.9.0 to 4.0.0
@@ -23,2 +23,3 @@ "use strict"; | ||
// * `connect({connection, url})` - Emitted whenever we connect to a broker. | ||
// * `connectFailed({err, url})` - Emitted whenever we fail to connect to a broker. | ||
// * `disconnect({err})` - Emitted whenever we disconnect from a broker. | ||
@@ -55,10 +56,2 @@ // * `blocked({reason})` - Emitted whenever connection is blocked by a broker. | ||
this._closed = false; | ||
/** | ||
* Keep track of whether a disconnect event has been sent or not. The problem | ||
* is that if we've never connected, and we encounter an error, we want to | ||
* generate a "disconnect" event, even though we're not disconnected, otherwise | ||
* the caller will never know there was an error. So we can't just rely on | ||
* this._currentConnection. | ||
*/ | ||
this._disconnectSent = false; | ||
if (!urls && !options.findServers) { | ||
@@ -87,3 +80,3 @@ throw new Error('Must supply either `urls` or `findServers`'); | ||
let reject; | ||
const onDisconnect = ({ err }) => { | ||
const onConnectFailed = ({ err }) => { | ||
// Ignore disconnects caused by dead servers etc., but throw on operational errors like bad credentials. | ||
@@ -99,3 +92,3 @@ if (err.isOperational) { | ||
reject = innerReject; | ||
this.on('disconnect', onDisconnect); | ||
this.on('connectFailed', onConnectFailed); | ||
}), | ||
@@ -112,3 +105,3 @@ ...(timeout | ||
finally { | ||
this.removeListener('disconnect', onDisconnect); | ||
this.removeListener('connectFailed', onConnectFailed); | ||
} | ||
@@ -173,3 +166,2 @@ } | ||
this._currentConnection = undefined; | ||
this._disconnectSent = true; | ||
this.emit('disconnect', { err: new Error('forced reconnect') }); | ||
@@ -196,2 +188,3 @@ return this._connect(); | ||
} | ||
let attemptedUrl; | ||
const result = (this._connectPromise = Promise.resolve() | ||
@@ -241,2 +234,3 @@ .then(() => { | ||
} | ||
attemptedUrl = originalUrl; | ||
// Add the `heartbeastIntervalInSeconds` to the connection options. | ||
@@ -264,3 +258,2 @@ if (typeof connect === 'string') { | ||
this._currentConnection = undefined; | ||
this._disconnectSent = true; | ||
this.emit('disconnect', { err }); | ||
@@ -275,3 +268,2 @@ const handle = (0, helpers_js_1.wait)(this.reconnectTimeInSeconds * 1000); | ||
this._connectPromise = undefined; | ||
this._disconnectSent = false; | ||
this.emit('connect', { connection, url: originalUrl }); | ||
@@ -283,6 +275,3 @@ // Need to return null here, or Bluebird will complain - #171. | ||
.catch((err) => { | ||
if (!this._disconnectSent) { | ||
this._disconnectSent = true; | ||
this.emit('disconnect', { err }); | ||
} | ||
this.emit('connectFailed', { err, url: attemptedUrl }); | ||
// Connection failed... | ||
@@ -289,0 +278,0 @@ this._currentConnection = undefined; |
@@ -17,2 +17,8 @@ /// <reference types="node" /> | ||
} | ||
export interface ConnectFailedListener { | ||
(arg: { | ||
err: Error; | ||
url: string | amqp.Options.Connect | undefined; | ||
}): void; | ||
} | ||
export declare type AmpqConnectionOptions = (ConnectionOptions | TcpSocketConnectOpts) & { | ||
@@ -56,2 +62,3 @@ noDelay?: boolean; | ||
addListener(event: 'connect', listener: ConnectListener): this; | ||
addListener(event: 'connectFailed', listener: ConnectFailedListener): this; | ||
addListener(event: 'blocked', listener: (arg: { | ||
@@ -67,2 +74,3 @@ reason: string; | ||
on(event: 'connect', listener: ConnectListener): this; | ||
on(event: 'connectFailed', listener: ConnectFailedListener): this; | ||
on(event: 'blocked', listener: (arg: { | ||
@@ -77,2 +85,3 @@ reason: string; | ||
once(event: 'connect', listener: ConnectListener): this; | ||
once(event: 'connectFailed', listener: ConnectFailedListener): this; | ||
once(event: 'blocked', listener: (arg: { | ||
@@ -87,2 +96,3 @@ reason: string; | ||
prependListener(event: 'connect', listener: ConnectListener): this; | ||
prependListener(event: 'connectFailed', listener: ConnectFailedListener): this; | ||
prependListener(event: 'blocked', listener: (arg: { | ||
@@ -97,2 +107,3 @@ reason: string; | ||
prependOnceListener(event: 'connect', listener: ConnectListener): this; | ||
prependOnceListener(event: 'connectFailed', listener: ConnectFailedListener): this; | ||
prependOnceListener(event: 'blocked', listener: (arg: { | ||
@@ -127,10 +138,2 @@ reason: string; | ||
private _urls?; | ||
/** | ||
* Keep track of whether a disconnect event has been sent or not. The problem | ||
* is that if we've never connected, and we encounter an error, we want to | ||
* generate a "disconnect" event, even though we're not disconnected, otherwise | ||
* the caller will never know there was an error. So we can't just rely on | ||
* this._currentConnection. | ||
*/ | ||
private _disconnectSent; | ||
connectionOptions: AmpqConnectionOptions | undefined; | ||
@@ -137,0 +140,0 @@ heartbeatIntervalInSeconds: number; |
@@ -18,2 +18,3 @@ import amqp from 'amqplib'; | ||
// * `connect({connection, url})` - Emitted whenever we connect to a broker. | ||
// * `connectFailed({err, url})` - Emitted whenever we fail to connect to a broker. | ||
// * `disconnect({err})` - Emitted whenever we disconnect from a broker. | ||
@@ -50,10 +51,2 @@ // * `blocked({reason})` - Emitted whenever connection is blocked by a broker. | ||
this._closed = false; | ||
/** | ||
* Keep track of whether a disconnect event has been sent or not. The problem | ||
* is that if we've never connected, and we encounter an error, we want to | ||
* generate a "disconnect" event, even though we're not disconnected, otherwise | ||
* the caller will never know there was an error. So we can't just rely on | ||
* this._currentConnection. | ||
*/ | ||
this._disconnectSent = false; | ||
if (!urls && !options.findServers) { | ||
@@ -82,3 +75,3 @@ throw new Error('Must supply either `urls` or `findServers`'); | ||
let reject; | ||
const onDisconnect = ({ err }) => { | ||
const onConnectFailed = ({ err }) => { | ||
// Ignore disconnects caused by dead servers etc., but throw on operational errors like bad credentials. | ||
@@ -94,3 +87,3 @@ if (err.isOperational) { | ||
reject = innerReject; | ||
this.on('disconnect', onDisconnect); | ||
this.on('connectFailed', onConnectFailed); | ||
}), | ||
@@ -107,3 +100,3 @@ ...(timeout | ||
finally { | ||
this.removeListener('disconnect', onDisconnect); | ||
this.removeListener('connectFailed', onConnectFailed); | ||
} | ||
@@ -168,3 +161,2 @@ } | ||
this._currentConnection = undefined; | ||
this._disconnectSent = true; | ||
this.emit('disconnect', { err: new Error('forced reconnect') }); | ||
@@ -191,2 +183,3 @@ return this._connect(); | ||
} | ||
let attemptedUrl; | ||
const result = (this._connectPromise = Promise.resolve() | ||
@@ -236,2 +229,3 @@ .then(() => { | ||
} | ||
attemptedUrl = originalUrl; | ||
// Add the `heartbeastIntervalInSeconds` to the connection options. | ||
@@ -259,3 +253,2 @@ if (typeof connect === 'string') { | ||
this._currentConnection = undefined; | ||
this._disconnectSent = true; | ||
this.emit('disconnect', { err }); | ||
@@ -270,3 +263,2 @@ const handle = wait(this.reconnectTimeInSeconds * 1000); | ||
this._connectPromise = undefined; | ||
this._disconnectSent = false; | ||
this.emit('connect', { connection, url: originalUrl }); | ||
@@ -278,6 +270,3 @@ // Need to return null here, or Bluebird will complain - #171. | ||
.catch((err) => { | ||
if (!this._disconnectSent) { | ||
this._disconnectSent = true; | ||
this.emit('disconnect', { err }); | ||
} | ||
this.emit('connectFailed', { err, url: attemptedUrl }); | ||
// Connection failed... | ||
@@ -284,0 +273,0 @@ this._currentConnection = undefined; |
{ | ||
"name": "amqp-connection-manager", | ||
"version": "3.9.0", | ||
"version": "4.0.0", | ||
"description": "Auto-reconnect and round robin support for amqplib.", | ||
@@ -5,0 +5,0 @@ "module": "./dist/esm/index.js", |
@@ -109,3 +109,3 @@ # amqp-connection-manager | ||
- `options.findServers(callback)` is a function which returns one or more servers to connect to. This should | ||
return either a single URL or an array of URLs. This is handy when you're using a service discovery mechanism | ||
return either a single URL or an array of URLs. This is handy when you're using a service discovery mechanism. | ||
such as Consul or etcd. Instead of taking a `callback`, this can also return a Promise. Note that if this | ||
@@ -118,2 +118,3 @@ is supplied, then `urls` is ignored. | ||
- `connect({connection, url})` - Emitted whenever we successfully connect to a broker. | ||
- `connectFailed({err, url})` - Emitted whenever we attempt to connect to a broker, but fail. | ||
- `disconnect({err})` - Emitted whenever we disconnect from a broker. | ||
@@ -120,0 +121,0 @@ - `blocked({reason})` - Emitted whenever a connection is blocked by a broker |
Sorry, the diff of this file is not supported yet
202
138259
2415