fix: Using Parse Server option extendSessionOnUse does not correctly clear memory and functions as a debounce instead of a throttle (#8683)
This commit is contained in:
@@ -106,6 +106,8 @@ describe('Auth', () => {
|
|||||||
updatedAt: updatedAt.toISOString(),
|
updatedAt: updatedAt.toISOString(),
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
Parse.Server.cacheController.clear();
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||||
await session.fetch();
|
await session.fetch();
|
||||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||||
await session.fetch();
|
await session.fetch();
|
||||||
|
|||||||
14
src/Auth.js
14
src/Auth.js
@@ -2,6 +2,7 @@ const Parse = require('parse/node');
|
|||||||
import { isDeepStrictEqual } from 'util';
|
import { isDeepStrictEqual } from 'util';
|
||||||
import { getRequestObject, resolveError } from './triggers';
|
import { getRequestObject, resolveError } from './triggers';
|
||||||
import { logger } from './logger';
|
import { logger } from './logger';
|
||||||
|
import { LRUCache as LRU } from 'lru-cache';
|
||||||
import RestQuery from './RestQuery';
|
import RestQuery from './RestQuery';
|
||||||
import RestWrite from './RestWrite';
|
import RestWrite from './RestWrite';
|
||||||
|
|
||||||
@@ -67,6 +68,10 @@ function nobody(config) {
|
|||||||
return new Auth({ config, isMaster: false });
|
return new Auth({ config, isMaster: false });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const throttle = new LRU({
|
||||||
|
max: 10000,
|
||||||
|
ttl: 500,
|
||||||
|
});
|
||||||
/**
|
/**
|
||||||
* Checks whether session should be updated based on last update time & session length.
|
* Checks whether session should be updated based on last update time & session length.
|
||||||
*/
|
*/
|
||||||
@@ -78,13 +83,14 @@ function shouldUpdateSessionExpiry(config, session) {
|
|||||||
return lastUpdated <= skipRange;
|
return lastUpdated <= skipRange;
|
||||||
}
|
}
|
||||||
|
|
||||||
const throttle = {};
|
|
||||||
const renewSessionIfNeeded = async ({ config, session, sessionToken }) => {
|
const renewSessionIfNeeded = async ({ config, session, sessionToken }) => {
|
||||||
if (!config?.extendSessionOnUse) {
|
if (!config?.extendSessionOnUse) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
clearTimeout(throttle[sessionToken]);
|
if (throttle.get(sessionToken)) {
|
||||||
throttle[sessionToken] = setTimeout(async () => {
|
return;
|
||||||
|
}
|
||||||
|
throttle.set(sessionToken, true);
|
||||||
try {
|
try {
|
||||||
if (!session) {
|
if (!session) {
|
||||||
const query = await RestQuery({
|
const query = await RestQuery({
|
||||||
@@ -99,6 +105,7 @@ const renewSessionIfNeeded = async ({ config, session, sessionToken }) => {
|
|||||||
const { results } = await query.execute();
|
const { results } = await query.execute();
|
||||||
session = results[0];
|
session = results[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!shouldUpdateSessionExpiry(config, session) || !session) {
|
if (!shouldUpdateSessionExpiry(config, session) || !session) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -115,7 +122,6 @@ const renewSessionIfNeeded = async ({ config, session, sessionToken }) => {
|
|||||||
logger.error('Could not update session expiry: ', e);
|
logger.error('Could not update session expiry: ', e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, 500);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Returns a promise that resolves to an Auth object
|
// Returns a promise that resolves to an Auth object
|
||||||
|
|||||||
Reference in New Issue
Block a user