summaryrefslogtreecommitdiff
path: root/includes/external/matrix/node_modules/matrix-js-sdk/lib/ToDeviceMessageQueue.js.map
blob: f3369e4f4037fd2f6c19db18af0a6c331c23e31e (plain)
1
{"version":3,"file":"ToDeviceMessageQueue.js","names":["_event","require","_logger","_client","_scheduler","_sync","_utils","MAX_BATCH_SIZE","ToDeviceMessageQueue","constructor","client","_defineProperty2","default","retryTimeout","clearTimeout","sending","running","logger","debug","headBatch","store","getOldestToDeviceBatch","sendBatch","removeToDeviceBatch","id","retryAttempts","e","retryDelay","MatrixScheduler","RETRY_BACKOFF_RATELIMIT","Math","floor","httpStatus","error","info","setTimeout","sendQueue","state","oldState","SyncState","Syncing","start","on","ClientEvent","Sync","onResumedSync","stop","removeListener","queueBatch","batch","batches","i","length","batchWithTxnId","eventType","slice","txnId","makeTxnId","push","msgmap","map","msg","userId","deviceId","payload","ToDeviceMessageId","saveToDeviceBatches","contentMap","MapWithDefault","Map","item","getOrCreate","set","sendToDevice","exports"],"sources":["../src/ToDeviceMessageQueue.ts"],"sourcesContent":["/*\nCopyright 2022 The Matrix.org Foundation C.I.C.\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n    http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\nimport { ToDeviceMessageId } from \"./@types/event\";\nimport { logger } from \"./logger\";\nimport { MatrixClient, ClientEvent } from \"./client\";\nimport { MatrixError } from \"./http-api\";\nimport { IndexedToDeviceBatch, ToDeviceBatch, ToDeviceBatchWithTxnId, ToDevicePayload } from \"./models/ToDeviceMessage\";\nimport { MatrixScheduler } from \"./scheduler\";\nimport { SyncState } from \"./sync\";\nimport { MapWithDefault } from \"./utils\";\n\nconst MAX_BATCH_SIZE = 20;\n\n/**\n * Maintains a queue of outgoing to-device messages, sending them\n * as soon as the homeserver is reachable.\n */\nexport class ToDeviceMessageQueue {\n    private sending = false;\n    private running = true;\n    private retryTimeout: ReturnType<typeof setTimeout> | null = null;\n    private retryAttempts = 0;\n\n    public constructor(private client: MatrixClient) {}\n\n    public start(): void {\n        this.running = true;\n        this.sendQueue();\n        this.client.on(ClientEvent.Sync, this.onResumedSync);\n    }\n\n    public stop(): void {\n        this.running = false;\n        if (this.retryTimeout !== null) clearTimeout(this.retryTimeout);\n        this.retryTimeout = null;\n        this.client.removeListener(ClientEvent.Sync, this.onResumedSync);\n    }\n\n    public async queueBatch(batch: ToDeviceBatch): Promise<void> {\n        const batches: ToDeviceBatchWithTxnId[] = [];\n        for (let i = 0; i < batch.batch.length; i += MAX_BATCH_SIZE) {\n            const batchWithTxnId = {\n                eventType: batch.eventType,\n                batch: batch.batch.slice(i, i + MAX_BATCH_SIZE),\n                txnId: this.client.makeTxnId(),\n            };\n            batches.push(batchWithTxnId);\n            const msgmap = batchWithTxnId.batch.map(\n                (msg) => `${msg.userId}/${msg.deviceId} (msgid ${msg.payload[ToDeviceMessageId]})`,\n            );\n            logger.info(\n                `Enqueuing batch of to-device messages. type=${batch.eventType} txnid=${batchWithTxnId.txnId}`,\n                msgmap,\n            );\n        }\n\n        await this.client.store.saveToDeviceBatches(batches);\n        this.sendQueue();\n    }\n\n    public sendQueue = async (): Promise<void> => {\n        if (this.retryTimeout !== null) clearTimeout(this.retryTimeout);\n        this.retryTimeout = null;\n\n        if (this.sending || !this.running) return;\n\n        logger.debug(\"Attempting to send queued to-device messages\");\n\n        this.sending = true;\n        let headBatch: IndexedToDeviceBatch | null;\n        try {\n            while (this.running) {\n                headBatch = await this.client.store.getOldestToDeviceBatch();\n                if (headBatch === null) break;\n                await this.sendBatch(headBatch);\n                await this.client.store.removeToDeviceBatch(headBatch.id);\n                this.retryAttempts = 0;\n            }\n\n            // Make sure we're still running after the async tasks: if not, stop.\n            if (!this.running) return;\n\n            logger.debug(\"All queued to-device messages sent\");\n        } catch (e) {\n            ++this.retryAttempts;\n            // eslint-disable-next-line @typescript-eslint/naming-convention\n            // eslint-disable-next-line new-cap\n            const retryDelay = MatrixScheduler.RETRY_BACKOFF_RATELIMIT(null, this.retryAttempts, <MatrixError>e);\n            if (retryDelay === -1) {\n                // the scheduler function doesn't differentiate between fatal errors and just getting\n                // bored and giving up for now\n                if (Math.floor((<MatrixError>e).httpStatus! / 100) === 4) {\n                    logger.error(\"Fatal error when sending to-device message - dropping to-device batch!\", e);\n                    await this.client.store.removeToDeviceBatch(headBatch!.id);\n                } else {\n                    logger.info(\"Automatic retry limit reached for to-device messages.\");\n                }\n                return;\n            }\n\n            logger.info(`Failed to send batch of to-device messages. Will retry in ${retryDelay}ms`, e);\n            this.retryTimeout = setTimeout(this.sendQueue, retryDelay);\n        } finally {\n            this.sending = false;\n        }\n    };\n\n    /**\n     * Attempts to send a batch of to-device messages.\n     */\n    private async sendBatch(batch: IndexedToDeviceBatch): Promise<void> {\n        const contentMap: MapWithDefault<string, Map<string, ToDevicePayload>> = new MapWithDefault(() => new Map());\n        for (const item of batch.batch) {\n            contentMap.getOrCreate(item.userId).set(item.deviceId, item.payload);\n        }\n\n        logger.info(\n            `Sending batch of ${batch.batch.length} to-device messages with ID ${batch.id} and txnId ${batch.txnId}`,\n        );\n\n        await this.client.sendToDevice(batch.eventType, contentMap, batch.txnId);\n    }\n\n    /**\n     * Listen to sync state changes and automatically resend any pending events\n     * once syncing is resumed\n     */\n    private onResumedSync = (state: SyncState | null, oldState: SyncState | null): void => {\n        if (state === SyncState.Syncing && oldState !== SyncState.Syncing) {\n            logger.info(`Resuming queue after resumed sync`);\n            this.sendQueue();\n        }\n    };\n}\n"],"mappings":";;;;;;;;AAgBA,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,OAAA,GAAAD,OAAA;AACA,IAAAE,OAAA,GAAAF,OAAA;AAGA,IAAAG,UAAA,GAAAH,OAAA;AACA,I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