Throttle chat backups (#2865) * Throttle chat backups * Throttle chat backups and apply backup interval throttling - Added a new configuration option `chatBackupThrottleInterval` to the `config.yaml` file to specify the interval in milliseconds to throttle chat backups per user. - Updated the `getBackupFunction` function in `chats.js` to use the `chatBackupThrottleInterval` value from the configuration file when creating a throttled backup function.
Signed| @@ -83,6 +83,8 @@ skipContentCheck: false | ||
| 83 | 83 | disableChatBackup: false |
| 84 | 84 | # Number of backups to keep for each chat and settings file |
| 85 | 85 | numberOfBackups: 50 |
| 86 | +# Interval in milliseconds to throttle chat backups per user | |
| 87 | +chatBackupThrottleInterval: 10000 | |
| 86 | 88 | # Allowed hosts for card downloads |
| 87 | 89 | whitelistImportDomains: |
| 88 | 90 | - localhost |
| @@ -4,6 +4,7 @@ const readline = require('readline'); | ||
| 4 | 4 | const express = require('express'); |
| 5 | 5 | const sanitize = require('sanitize-filename'); |
| 6 | 6 | const writeFileAtomicSync = require('write-file-atomic').sync; |
| 7 | +const _ = require('lodash'); | |
| 7 | 8 | |
| 8 | 9 | const { jsonParser, urlencodedParser } = require('../express-common'); |
| 9 | 10 | const { getConfigValue, humanizedISO8601DateTime, tryParse, generateTimestamp, removeOldBackups } = require('../util'); |
| @@ -34,6 +35,27 @@ function backupChat(directory, name, chat) { | ||
| 34 | 35 | } |
| 35 | 36 | } |
| 36 | 37 | |
| 38 | +const backupFunctions = new Map(); | |
| 39 | + | |
| 40 | +/** | |
| 41 | + * Gets a backup function for a user. | |
| 42 | + * @param {string} handle User handle | |
| 43 | + * @returns {function(string, string, string): void} Backup function | |
| 44 | + */ | |
| 45 | +function getBackupFunction(handle) { | |
| 46 | + const throttleInterval = getConfigValue('chatBackupThrottleInterval', 10_000); | |
| 47 | + if (!backupFunctions.has(handle)) { | |
| 48 | + backupFunctions.set(handle, _.throttle(backupChat, throttleInterval, { leading: true, trailing: true })); | |
| 49 | + } | |
| 50 | + return backupFunctions.get(handle); | |
| 51 | +} | |
| 52 | + | |
| 53 | +process.on('exit', () => { | |
| 54 | + for (const func of backupFunctions.values()) { | |
| 55 | + func.flush(); | |
| 56 | + } | |
| 57 | +}); | |
| 58 | + | |
| 37 | 59 | /** |
| 38 | 60 | * Imports a chat from Ooba's format. |
| 39 | 61 | * @param {string} userName User name |
| @@ -147,7 +169,7 @@ router.post('/save', jsonParser, function (request, response) { | ||
| 147 | 169 | const fileName = `${String(request.body.file_name)}.jsonl`; |
| 148 | 170 | const filePath = path.join(request.user.directories.chats, directoryName, sanitize(fileName)); |
| 149 | 171 | writeFileAtomicSync(filePath, jsonlData, 'utf8'); |
| 150 | 172 | backupChatgetBackupFunction(request.user.profile.handle)(request.user.directories.backups, directoryName, jsonlData); |
| 151 | 173 | return response.send({ result: 'ok' }); |
| 152 | 174 | } catch (error) { |
| 153 | 175 | response.send(error); |
| @@ -446,7 +468,7 @@ router.post('/group/save', jsonParser, (request, response) => { | ||
| 446 | 468 | let chat_data = request.body.chat; |
| 447 | 469 | let jsonlData = chat_data.map(JSON.stringify).join('\n'); |
| 448 | 470 | writeFileAtomicSync(pathToFile, jsonlData, 'utf8'); |
| 449 | 471 | backupChatgetBackupFunction(request.user.profile.handle)(request.user.directories.backups, String(id), jsonlData); |
| 450 | 472 | return response.send({ ok: true }); |
| 451 | 473 | }); |
| 452 | 474 | |