@rxdi/rabbitmq-pubsub 0.7.247 → 0.7.248
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/connectionFactory.js +28 -1
- package/package.json +1 -1
|
@@ -47,7 +47,34 @@ class RabbitMqSingletonConnectionFactory {
|
|
|
47
47
|
return this.promise;
|
|
48
48
|
}
|
|
49
49
|
this.logger.debug('creating connection to %s', this.connection);
|
|
50
|
-
|
|
50
|
+
// Enable heartbeats so a half-open (silently dead) TCP connection is
|
|
51
|
+
// detected instead of hanging every publish/subscribe forever.
|
|
52
|
+
const url = this.connection.indexOf('heartbeat=') === -1
|
|
53
|
+
? this.connection +
|
|
54
|
+
(this.connection.indexOf('?') === -1 ? '?' : '&') +
|
|
55
|
+
'heartbeat=30'
|
|
56
|
+
: this.connection;
|
|
57
|
+
return (this.promise = Promise.resolve((0, amqplib_1.connect)(url))
|
|
58
|
+
.then((conn) => {
|
|
59
|
+
// If the connection dies, drop the cached promise so the NEXT
|
|
60
|
+
// create() re-establishes it instead of reusing a dead connection.
|
|
61
|
+
const reset = (err) => {
|
|
62
|
+
if (this.promise) {
|
|
63
|
+
this.logger.error(err, "connection to '%s' lost, will reconnect on next use", this.connection);
|
|
64
|
+
this.promise = undefined;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
conn.on('error', reset);
|
|
68
|
+
conn.on('close', () => reset());
|
|
69
|
+
return conn;
|
|
70
|
+
})
|
|
71
|
+
.catch((err) => {
|
|
72
|
+
// Never cache a rejected connection — clear it so we retry next time
|
|
73
|
+
// (e.g. when the broker finishes starting up).
|
|
74
|
+
this.promise = undefined;
|
|
75
|
+
this.logger.error("failed to create connection '%s'", this.connection);
|
|
76
|
+
return Promise.reject(err);
|
|
77
|
+
}));
|
|
51
78
|
}
|
|
52
79
|
}
|
|
53
80
|
exports.RabbitMqSingletonConnectionFactory = RabbitMqSingletonConnectionFactory;
|