Set up onReconnect after initial sub on the connection to log-reader.

If we set it up before `subscribeAndWait` returns, then we'll end up
with two subscriptions; we don't have the log-reader sub yet, so we
can't stop it when `onReconnect` runs the first time, so we end up with
a redundant subscription. This means that if a real reconnect happens
later, we'll stop the sub that we set up inside `onReconnect`, but not
the initial sub, so we've leaked a sub and end up with duplicate
messages after reconnect.
This commit is contained in:
Emily Stark
2014-02-27 07:56:36 -08:00
parent 9d5782b9a0
commit bc4524b544

View File

@@ -457,7 +457,21 @@ exports.logs = function (options) {
throw new Error("Can't listen to messages on the logs collection");
var logsSubscription = null;
// In case of reconnect recover the state so user sees only new logs
try {
logsSubscription =
logReader.subscribeAndWait("logsForApp", options.app,
{ streaming: options.streaming });
} catch (e) {
return handleError(e, galaxy, {
"no-such-app": "No such app: " + options.app
});
}
// In case of reconnect recover the state so user sees only new logs.
// Only set up the onReconnect handler after the subscribe and wait
// has returned; if we set it up before, then we'll end up with two
// subscriptions, because the onReconnect handler will run for the
// first time before the subscribeAndWait returns.
logReader.connection.onReconnect = function () {
logsSubscription && logsSubscription.stop();
var opts = { streaming: options.streaming };
@@ -476,16 +490,6 @@ exports.logs = function (options) {
);
};
try {
logsSubscription =
logReader.subscribeAndWait("logsForApp", options.app,
{ streaming: options.streaming });
} catch (e) {
return handleError(e, galaxy, {
"no-such-app": "No such app: " + options.app
});
}
return options.streaming ? null : 0;
} finally {
// If not streaming, close the connection to log-reader so that