Skip to content

Commit

Permalink
fix: do not use setInterval for auth renewal
Browse files Browse the repository at this point in the history
can come into a race condition with promises
  • Loading branch information
targos committed May 30, 2017
1 parent 25f37d0 commit e56c92c
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@ const CouchError = require('./util/CouchError');
const debug = require('./util/debug')('main:connect');
const nanoPromise = require('./util/nanoPromise');

let authInterval;
const authRenewal = config.authRenewal * 1000;

let globalNano;
let lastAuthentication = 0;

async function open() {
if (globalNano) {
return globalNano;
}
debug('initialize connection to CouchDB');
authInterval = setInterval(() => {
const currentDate = Date.now();
if (currentDate - lastAuthentication > authRenewal) {
if (lastAuthentication === 0) {
debug('initialize connection to CouchDB');
}
globalNano = getGlobalNano();
}, config.authRenewal * 1000);
return globalNano = getGlobalNano();
lastAuthentication = currentDate;
}
return globalNano;
}

async function getGlobalNano() {
Expand All @@ -40,8 +43,6 @@ async function getGlobalNano() {
}

function close() {
clearInterval(authInterval);
authInterval = null;
globalNano = null;
}

Expand Down

0 comments on commit e56c92c

Please sign in to comment.