Skip to content

Commit

Permalink
INMEM_MAX_SESSIONS
Browse files Browse the repository at this point in the history
  • Loading branch information
Liza K committed Dec 7, 2020
1 parent 5b9782d commit 0858b98
Showing 1 changed file with 1 addition and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import moment, { Moment } from 'moment';
import { orderBy } from 'lodash';
import { from, Observable } from 'rxjs';
import { first, switchMap } from 'rxjs/operators';
import {
Expand Down Expand Up @@ -95,7 +94,7 @@ export class BackgroundSessionService implements ISessionService {
.map((sessionId) => `"${sessionId}"`)
.join(' | ');
const res = await this.internalSavedObjectsClient.find<BackgroundSessionSavedObjectAttributes>({
perPage: INMEM_MAX_SESSIONS,
perPage: INMEM_MAX_SESSIONS, // If there are more sessions in memory, they will be synced when some items are cleared out.
type: BACKGROUND_SESSION_TYPE,
search: activeMappingIds,
searchFields: ['sessionId'],
Expand All @@ -108,26 +107,6 @@ export class BackgroundSessionService implements ISessionService {
private clearSessions = () => {
const curTime = moment();

// Drop old items if map size exceeds max.
if (this.sessionSearchMap.size > INMEM_MAX_SESSIONS) {
const sortedSessionIds = orderBy(
Array.from(this.sessionSearchMap.keys()).map((sessionId) => {
return {
sessionId,
insertTime: this.sessionSearchMap.get(sessionId)!.insertTime,
};
}),
['insertTime'],
['asc']
);

while (this.sessionSearchMap.size > INMEM_MAX_SESSIONS) {
const { sessionId } = sortedSessionIds.shift()!;
this.logger.warn(`clearSessions | Map full | Dropping ${sessionId}`);
this.sessionSearchMap.delete(sessionId);
}
}

this.sessionSearchMap.forEach((sessionInfo, sessionId) => {
if (
moment.duration(curTime.diff(sessionInfo.insertTime)).asSeconds() >
Expand Down

0 comments on commit 0858b98

Please sign in to comment.