0
0
mirror of https://github.com/sp-tarkov/server.git synced 2025-02-13 02:50:44 -05:00

Small saveserver changes (#1101)

- Adds a saveguard to the current profile being saved to prevent it from
being called again for a save if one is already underway, this is only
ever possible in my testing to happen on certain race conditions where a
user might quit the game at the same time as the server is saving, none
the less it's better if this is only processed one-time
- Wraps current timed saves in a try-catch so that if one profile errors
out during saving this does not completely stop the method from running.
This commit is contained in:
Chomp 2025-01-31 10:06:47 +00:00 committed by GitHub
commit a4604709b1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -15,6 +15,7 @@ import { inject, injectAll, injectable } from "tsyringe";
export class SaveServer { export class SaveServer {
protected profileFilepath = "user/profiles/"; protected profileFilepath = "user/profiles/";
protected profiles: Map<string, ISptProfile> = new Map(); protected profiles: Map<string, ISptProfile> = new Map();
protected profilesBeingSaved: Set<string> = new Set();
protected onBeforeSaveCallbacks: Map<string, (profile: ISptProfile) => Promise<ISptProfile>> = new Map(); protected onBeforeSaveCallbacks: Map<string, (profile: ISptProfile) => Promise<ISptProfile>> = new Map();
protected saveSHA1: { [key: string]: string } = {}; protected saveSHA1: { [key: string]: string } = {};
@ -71,12 +72,19 @@ export class SaveServer {
*/ */
public async save(): Promise<void> { public async save(): Promise<void> {
const timer = new Timer(); const timer = new Timer();
let savedProfiles = 0;
for (const [sessionId] of this.profiles) { for (const [sessionId] of this.profiles) {
await this.saveProfile(sessionId); try {
await this.saveProfile(sessionId);
savedProfiles++;
} catch (error) {
this.logger.error(`Could not save profile ${sessionId} | ${error}`);
}
} }
const profileCount = this.profiles.size;
this.logger.debug( this.logger.debug(
`Saving ${profileCount} profile${profileCount > 1 ? "s" : ""} took ${timer.getTime("ms")}ms`, `Saving ${savedProfiles} profile${savedProfiles > 1 ? "s" : ""} took ${timer.getTime("ms")}ms`,
false, false,
); );
} }
@ -184,6 +192,12 @@ export class SaveServer {
throw new Error(`Profile ${sessionID} does not exist! Unable to save this profile!`); throw new Error(`Profile ${sessionID} does not exist! Unable to save this profile!`);
} }
if (this.profilesBeingSaved.has(sessionID)) {
throw new Error(`Profile ${sessionID} is already being saved!`);
}
this.profilesBeingSaved.add(sessionID);
const filePath = `${this.profileFilepath}${sessionID}.json`; const filePath = `${this.profileFilepath}${sessionID}.json`;
// Run pre-save callbacks before we save into json // Run pre-save callbacks before we save into json
@ -201,12 +215,16 @@ export class SaveServer {
this.profiles.get(sessionID), this.profiles.get(sessionID),
!this.configServer.getConfig<ICoreConfig>(ConfigTypes.CORE).features.compressProfile, !this.configServer.getConfig<ICoreConfig>(ConfigTypes.CORE).features.compressProfile,
); );
const sha1 = await this.hashUtil.generateSha1ForDataAsync(jsonProfile); const sha1 = await this.hashUtil.generateSha1ForDataAsync(jsonProfile);
if (typeof this.saveSHA1[sessionID] !== "string" || this.saveSHA1[sessionID] !== sha1) { if (typeof this.saveSHA1[sessionID] !== "string" || this.saveSHA1[sessionID] !== sha1) {
this.saveSHA1[sessionID] = sha1; this.saveSHA1[sessionID] = sha1;
// save profile to disk // save profile to disk
await this.fileSystem.write(filePath, jsonProfile); await this.fileSystem.write(filePath, jsonProfile);
} }
this.profilesBeingSaved.delete(sessionID);
} }
/** /**