mirror of
https://github.com/sergeyyarkov/educt-server.git
synced 2026-10-11 10:19:20 +03:00
fix: cleaning up sessions
This commit is contained in:
parent
e22c80733a
commit
7e81d7b4ff
@ -4,6 +4,12 @@
|
||||
|
||||
- API deployment with Docker and Nginx web server.
|
||||
|
||||
## [1.1.1] -
|
||||
|
||||
### Fix
|
||||
|
||||
- Cleaning up session store before starting websocket server
|
||||
|
||||
## [1.1.0] - 2022-01-31
|
||||
|
||||
### Added
|
||||
|
||||
@ -59,8 +59,20 @@ class WsService {
|
||||
});
|
||||
this.sessionStore = new RedisSessionStore(Redis.connection('session'));
|
||||
|
||||
this.setupMiddlewares();
|
||||
this.listen();
|
||||
/**
|
||||
* Clean up sessions before start
|
||||
*/
|
||||
this.sessionStore.destroyAllSessions().then(() => {
|
||||
/**
|
||||
* Middlewares
|
||||
*/
|
||||
this.setupMiddlewares();
|
||||
|
||||
/**
|
||||
* On connection socket
|
||||
*/
|
||||
this.listen();
|
||||
});
|
||||
}
|
||||
|
||||
private listen() {
|
||||
|
||||
@ -25,6 +25,20 @@ class RedisSessionStore {
|
||||
};
|
||||
}
|
||||
|
||||
private async scanSessionKeys(): Promise<Set<string>> {
|
||||
const keys = new Set<string>();
|
||||
let nextIndex = 0;
|
||||
|
||||
do {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const [nextIndexAsStr, result] = await this.redisClient.scan(nextIndex, 'MATCH', '*', 'COUNT', 100);
|
||||
nextIndex = parseInt(nextIndexAsStr, 10);
|
||||
result.forEach(s => keys.add(s));
|
||||
} while (nextIndex !== 0);
|
||||
|
||||
return keys;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return session data by session id
|
||||
*
|
||||
@ -40,15 +54,7 @@ class RedisSessionStore {
|
||||
* Returns the all sessions from store
|
||||
*/
|
||||
public async getSessions(): Promise<Array<SessionDataType | undefined>> {
|
||||
const keys = new Set<string>();
|
||||
let nextIndex = 0;
|
||||
|
||||
do {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const [nextIndexAsStr, result] = await this.redisClient.scan(nextIndex, 'MATCH', '*', 'COUNT', 100);
|
||||
nextIndex = parseInt(nextIndexAsStr, 10);
|
||||
result.forEach(s => keys.add(s));
|
||||
} while (nextIndex !== 0);
|
||||
const keys = await this.scanSessionKeys();
|
||||
const commands = Array.from(keys).map(key => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const [prefix, ...id] = key.split('-');
|
||||
@ -103,6 +109,27 @@ class RedisSessionStore {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all sessions from redis store
|
||||
*
|
||||
* @returns Count of deleted sessions
|
||||
*/
|
||||
public async destroyAllSessions() {
|
||||
const keys = await this.scanSessionKeys().then(values =>
|
||||
Array.from(values).map(v => {
|
||||
const [, ...key] = v.split('-');
|
||||
return key.join('-');
|
||||
})
|
||||
);
|
||||
|
||||
if (keys.length > 0) {
|
||||
const deleted = await this.redisClient.del(keys);
|
||||
return deleted;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
export default RedisSessionStore;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user