Update user file without reading, debounce all saves

This commit is contained in:
Pavel Djundik 2019-12-15 17:26:18 +02:00
parent def56dc694
commit f269ac3bee
3 changed files with 23 additions and 51 deletions

View File

@ -315,29 +315,23 @@ Client.prototype.updateSession = function(token, ip, request) {
agent: friendlyAgent, agent: friendlyAgent,
}); });
client.manager.updateUser(client.name, { client.save();
browser: client.config.browser,
sessions: client.config.sessions,
});
}; };
Client.prototype.setPassword = function(hash, callback) { Client.prototype.setPassword = function(hash, callback) {
const client = this; const client = this;
client.manager.updateUser( const oldHash = client.config.password;
client.name, client.config.password = hash;
{ client.manager.saveUser(client, function(err) {
password: hash,
},
function(err) {
if (err) { if (err) {
// If user file fails to write, reset it back
client.config.password = oldHash;
return callback(false); return callback(false);
} }
client.config.password = hash;
return callback(true); return callback(true);
} });
);
}; };
Client.prototype.input = function(data) { Client.prototype.input = function(data) {
@ -662,9 +656,7 @@ Client.prototype.registerPushSubscription = function(session, subscription, noSa
session.pushSubscription = data; session.pushSubscription = data;
if (!noSave) { if (!noSave) {
this.manager.updateUser(this.name, { this.save();
sessions: this.config.sessions,
});
} }
return data; return data;
@ -672,9 +664,7 @@ Client.prototype.registerPushSubscription = function(session, subscription, noSa
Client.prototype.unregisterPushSubscription = function(token) { Client.prototype.unregisterPushSubscription = function(token) {
this.config.sessions[token].pushSubscription = null; this.config.sessions[token].pushSubscription = null;
this.manager.updateUser(this.name, { this.save();
sessions: this.config.sessions,
});
}; };
Client.prototype.save = _.debounce( Client.prototype.save = _.debounce(
@ -684,9 +674,7 @@ Client.prototype.save = _.debounce(
} }
const client = this; const client = this;
const json = {}; client.manager.saveUser(client);
json.networks = this.networks.map((n) => n.export());
client.manager.updateUser(client.name, json);
}, },
1000, 1000,
{maxWait: 10000} {maxWait: 10000}

View File

@ -179,25 +179,13 @@ ClientManager.prototype.addUser = function(name, password, enableLog) {
return true; return true;
}; };
ClientManager.prototype.updateUser = function(name, opts, callback) { ClientManager.prototype.saveUser = function(client, callback) {
const user = readUserConfig(name); const json = Object.assign({}, client.config, {
networks: client.networks.map((n) => n.export()),
});
const newUser = JSON.stringify(json, null, "\t");
if (!user) { const pathReal = Helper.getUserConfigPath(client.name);
return callback ? callback(true) : false;
}
const currentUser = JSON.stringify(user, null, "\t");
_.assign(user, opts);
const newUser = JSON.stringify(user, null, "\t");
// Do not touch the disk if object has not changed
if (currentUser === newUser) {
console.log("same");
return callback ? callback() : true;
}
const pathReal = Helper.getUserConfigPath(name);
const pathTemp = pathReal + ".tmp"; const pathTemp = pathReal + ".tmp";
try { try {
@ -208,7 +196,7 @@ ClientManager.prototype.updateUser = function(name, opts, callback) {
return callback ? callback() : true; return callback ? callback() : true;
} catch (e) { } catch (e) {
log.error(`Failed to update user ${colors.green(name)} (${e})`); log.error(`Failed to update user ${colors.green(client.name)} (${e})`);
if (callback) { if (callback) {
callback(e); callback(e);

View File

@ -591,9 +591,7 @@ function initializeClient(socket, client, token, lastMessage, openChannel) {
value: newSetting.value, value: newSetting.value,
}); });
client.manager.updateUser(client.name, { client.save();
clientSettings: client.config.clientSettings,
});
if (newSetting.name === "highlights") { if (newSetting.name === "highlights") {
client.compileCustomHighlights(); client.compileCustomHighlights();
@ -630,9 +628,7 @@ function initializeClient(socket, client, token, lastMessage, openChannel) {
delete client.config.sessions[tokenToSignOut]; delete client.config.sessions[tokenToSignOut];
client.manager.updateUser(client.name, { client.save();
sessions: client.config.sessions,
});
_.map(client.attachedClients, (attachedClient, socketId) => { _.map(client.attachedClients, (attachedClient, socketId) => {
if (attachedClient.token !== tokenToSignOut) { if (attachedClient.token !== tokenToSignOut) {