moved the session cache to be longer lasting

This commit is contained in:
tigerbot 2017-10-19 12:58:04 -06:00
parent 019e4fa063
commit c23f5ae25b
2 changed files with 36 additions and 30 deletions

View File

@ -40,31 +40,28 @@ module.exports.create = function (deps, conf) {
return deps.PromiseA.all(promises.filter(Boolean)); return deps.PromiseA.all(promises.filter(Boolean));
} }
var tunnelActive = false;
async function connectTunnel() {
var sessionCache = {};
var sessionOverride;
if (conf.ddns.tunnel) {
sessionOverride = await deps.storage.tokens.get(conf.ddns.tunnel.tokenId);
}
async function getSession(id) { async function getSession(id) {
if (sessionOverride) { var session = await deps.storage.tokens.get(id);
return sessionOverride; if (!session) {
}
if (!sessionCache.hasOwnProperty(id)) {
sessionCache[id] = await deps.storage.tokens.get(conf.ddns.tunnel.tokenId);
}
if (!sessionCache[id]) {
throw new Error('no user token with ID "'+id+'"'); throw new Error('no user token with ID "'+id+'"');
} }
return sessionCache[id]; return session;
}
var tunnelActive = false;
async function connectTunnel() {
var tunnelSession;
if (conf.ddns.tunnel) {
// In the case of a non-existant token, I'm not sure if we want to throw here and prevent
// any tunnel connections, or if we want to ignore it and fall back to the DNS tokens
tunnelSession = await deps.storage.tokens.get(conf.ddns.tunnel.tokenId);
} }
await iterateAllModules(function startTunnel(mod, domainsList) { await iterateAllModules(function startTunnel(mod, domainsList) {
if (mod.type !== 'dns@oauth3.org') { return null; } if (mod.type !== 'dns@oauth3.org') { return null; }
return getSession(mod.token_id).then(function (session) { return getSession(mod.token_id).then(function (dnsSession) {
return deps.tunnelClients.start(session, domainsList); return deps.tunnelClients.start(tunnelSession || dnsSession, domainsList);
}).catch(function (err) { }).catch(function (err) {
console.log('error starting tunnel for', domainsList.join(', ')); console.log('error starting tunnel for', domainsList.join(', '));
console.log(err); console.log(err);
@ -131,17 +128,6 @@ module.exports.create = function (deps, conf) {
} }
publicAddress = addr; publicAddress = addr;
var sessionCache = {};
async function getSession(id) {
if (!sessionCache.hasOwnProperty(id)) {
sessionCache[id] = await deps.storage.tokens.get(conf.ddns.tunnel.tokenId);
}
if (!sessionCache[id]) {
throw new Error('no user token with ID "'+id+'"');
}
return sessionCache[id];
}
await iterateAllModules(function setModuleDNS(mod, domainsList) { await iterateAllModules(function setModuleDNS(mod, domainsList) {
if (mod.type !== 'dns@oauth3.org' || mod.disabled) { return null; } if (mod.type !== 'dns@oauth3.org' || mod.disabled) { return null; }

View File

@ -97,7 +97,8 @@ module.exports.create = function (deps, conf) {
var userTokens = { var userTokens = {
_filename: 'user-tokens.json' _filename: 'user-tokens.json'
, _convertToken(id, token) { , _cache: {}
, _convertToken: function convertToken(id, token) {
// convert the token into something that looks more like what OAuth3 uses internally // convert the token into something that looks more like what OAuth3 uses internally
// as sessions so we can use it with OAuth3. We don't use OAuth3's internal session // as sessions so we can use it with OAuth3. We don't use OAuth3's internal session
// storage because it effectively only supports storing tokens based on provider URI. // storage because it effectively only supports storing tokens based on provider URI.
@ -118,16 +119,31 @@ module.exports.create = function (deps, conf) {
} }
, all: function allUserTokens() { , all: function allUserTokens() {
var self = this; var self = this;
if (self._cacheComplete) {
return deps.PromiseA.resolve(Object.values(self._cache));
}
return read(self._filename).then(function (tokens) { return read(self._filename).then(function (tokens) {
// We will read every single token into our cache, so it will be complete once we finish
// creating the result (it's set out of order so we can directly return the result).
self._cacheComplete = true;
return Object.keys(tokens).map(function (id) { return Object.keys(tokens).map(function (id) {
return self._convertToken(id, tokens[id]); self._cache[id] = self._convertToken(id, tokens[id]);
return self._cache[id];
}); });
}); });
} }
, get: function getUserToken(id) { , get: function getUserToken(id) {
var self = this; var self = this;
if (self._cache.hasOwnProperty(id) || self._cacheComplete) {
return deps.PromiseA.resolve(self._cache[id] || null);
}
return read(self._filename).then(function (tokens) { return read(self._filename).then(function (tokens) {
return self._convertToken(id, tokens[id]); return self._convertToken(id, tokens[id]);
}).then(function (session) {
self._cache[id] = session;
}); });
} }
, save: function saveUserToken(newToken) { , save: function saveUserToken(newToken) {
@ -155,6 +171,9 @@ module.exports.create = function (deps, conf) {
var id = idHash.digest('hex'); var id = idHash.digest('hex');
tokens[id] = rawToken; tokens[id] = rawToken;
return write(self._filename, tokens).then(function () { return write(self._filename, tokens).then(function () {
// Delete the current cache so that if this is an update it will refresh
// the cache once we read the ID.
delete self._cache[id];
return self.get(id); return self.get(id);
}); });
}); });
@ -168,6 +187,7 @@ module.exports.create = function (deps, conf) {
} }
return write(self._filename, tokens).then(function () { return write(self._filename, tokens).then(function () {
delete self._cache[id];
return true; return true;
}); });
}); });