diff --git a/src/indexing/EventIndex.js b/src/indexing/EventIndex.js index f034df888c..1e15fcaa5a 100644 --- a/src/indexing/EventIndex.js +++ b/src/indexing/EventIndex.js @@ -16,6 +16,8 @@ limitations under the License. import PlatformPeg from "../PlatformPeg"; import {MatrixClientPeg} from "../MatrixClientPeg"; +import SettingsStore from '../settings/SettingsStore'; +import {SettingLevel} from "../settings/SettingsStore"; import {sleep} from "../utils/promise"; /* @@ -24,9 +26,9 @@ import {sleep} from "../utils/promise"; export default class EventIndex { constructor() { this.crawlerCheckpoints = []; - // The time that the crawler will wait between /rooms/{room_id}/messages - // requests - this._crawlerTimeout = 3000; + // The time in ms that the crawler will wait loop iterations if there + // have not been any checkpoints to consume in the last iteration. + this._crawlerIdleTime = 5000; // The maximum number of events our crawler should fetch in a single // crawl. this._eventsPerCrawl = 100; @@ -194,11 +196,22 @@ export default class EventIndex { cancelled = true; }; + let idle = false; + while (!cancelled) { // This is a low priority task and we don't want to spam our // homeserver with /messages requests so we set a hefty timeout // here. - await sleep(this._crawlerTimeout); + let sleepTime = SettingsStore.getValueAt(SettingLevel.DEVICE, 'crawlerSleepTime'); + + // Don't let the user configure a lower sleep time than 100 ms. + sleepTime = Math.max(sleepTime, 100); + + if (idle) { + sleepTime = this._crawlerIdleTime; + } + + await sleep(sleepTime); console.log("EventIndex: Running the crawler loop."); @@ -211,9 +224,12 @@ export default class EventIndex { /// There is no checkpoint available currently, one may appear if // a sync with limited room timelines happens, so go back to sleep. if (checkpoint === undefined) { + idle = true; continue; } + idle = false; + console.log("EventIndex: crawling using checkpoint", checkpoint); // We have a checkpoint, let us fetch some messages, again, very diff --git a/src/settings/Settings.js b/src/settings/Settings.js index eacf63e55d..e967becf98 100644 --- a/src/settings/Settings.js +++ b/src/settings/Settings.js @@ -486,4 +486,9 @@ export const SETTINGS = { supportedLevels: LEVELS_DEVICE_ONLY_SETTINGS, default: RIGHT_PANEL_PHASES.GroupMemberList, }, + "crawlerSleepTime": { + supportedLevels: LEVELS_DEVICE_ONLY_SETTINGS, + displayName: _td("How long should the crawler wait between requests"), + default: 3000, + } };