2026-09-13 14:14:38 -04:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
|
|
|
value: true
|
|
|
|
|
});
|
|
|
|
|
exports.ToDeviceMessageQueue = void 0;
|
|
|
|
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
|
|
|
var _event = require("./@types/event");
|
|
|
|
|
var _logger = require("./logger");
|
|
|
|
|
var _client = require("./client");
|
|
|
|
|
var _scheduler = require("./scheduler");
|
|
|
|
|
var _sync = require("./sync");
|
|
|
|
|
var _utils = require("./utils");
|
2026-09-12 23:57:45 -04:00
|
|
|
/*
|
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
const MAX_BATCH_SIZE = 20;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Maintains a queue of outgoing to-device messages, sending them
|
|
|
|
|
* as soon as the homeserver is reachable.
|
|
|
|
|
*/
|
2026-09-13 14:14:38 -04:00
|
|
|
class ToDeviceMessageQueue {
|
|
|
|
|
constructor(client) {
|
|
|
|
|
this.client = client;
|
|
|
|
|
(0, _defineProperty2.default)(this, "sending", false);
|
|
|
|
|
(0, _defineProperty2.default)(this, "running", true);
|
|
|
|
|
(0, _defineProperty2.default)(this, "retryTimeout", null);
|
|
|
|
|
(0, _defineProperty2.default)(this, "retryAttempts", 0);
|
|
|
|
|
(0, _defineProperty2.default)(this, "sendQueue", async () => {
|
2026-09-12 23:57:45 -04:00
|
|
|
if (this.retryTimeout !== null) clearTimeout(this.retryTimeout);
|
|
|
|
|
this.retryTimeout = null;
|
|
|
|
|
if (this.sending || !this.running) return;
|
2026-09-13 14:14:38 -04:00
|
|
|
_logger.logger.debug("Attempting to send queued to-device messages");
|
2026-09-12 23:57:45 -04:00
|
|
|
this.sending = true;
|
|
|
|
|
let headBatch;
|
|
|
|
|
try {
|
|
|
|
|
while (this.running) {
|
|
|
|
|
headBatch = await this.client.store.getOldestToDeviceBatch();
|
|
|
|
|
if (headBatch === null) break;
|
|
|
|
|
await this.sendBatch(headBatch);
|
|
|
|
|
await this.client.store.removeToDeviceBatch(headBatch.id);
|
|
|
|
|
this.retryAttempts = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make sure we're still running after the async tasks: if not, stop.
|
|
|
|
|
if (!this.running) return;
|
2026-09-13 14:14:38 -04:00
|
|
|
_logger.logger.debug("All queued to-device messages sent");
|
2026-09-12 23:57:45 -04:00
|
|
|
} catch (e) {
|
|
|
|
|
++this.retryAttempts;
|
2026-09-13 14:14:38 -04:00
|
|
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
|
|
|
// eslint-disable-next-line new-cap
|
|
|
|
|
const retryDelay = _scheduler.MatrixScheduler.RETRY_BACKOFF_RATELIMIT(null, this.retryAttempts, e);
|
2026-09-12 23:57:45 -04:00
|
|
|
if (retryDelay === -1) {
|
|
|
|
|
// the scheduler function doesn't differentiate between fatal errors and just getting
|
|
|
|
|
// bored and giving up for now
|
|
|
|
|
if (Math.floor(e.httpStatus / 100) === 4) {
|
2026-09-13 14:14:38 -04:00
|
|
|
_logger.logger.error("Fatal error when sending to-device message - dropping to-device batch!", e);
|
2026-09-12 23:57:45 -04:00
|
|
|
await this.client.store.removeToDeviceBatch(headBatch.id);
|
|
|
|
|
} else {
|
2026-09-13 14:14:38 -04:00
|
|
|
_logger.logger.info("Automatic retry limit reached for to-device messages.");
|
2026-09-12 23:57:45 -04:00
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-09-13 14:14:38 -04:00
|
|
|
_logger.logger.info(`Failed to send batch of to-device messages. Will retry in ${retryDelay}ms`, e);
|
2026-09-12 23:57:45 -04:00
|
|
|
this.retryTimeout = setTimeout(this.sendQueue, retryDelay);
|
|
|
|
|
} finally {
|
|
|
|
|
this.sending = false;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
/**
|
|
|
|
|
* Listen to sync state changes and automatically resend any pending events
|
|
|
|
|
* once syncing is resumed
|
|
|
|
|
*/
|
2026-09-13 14:14:38 -04:00
|
|
|
(0, _defineProperty2.default)(this, "onResumedSync", (state, oldState) => {
|
|
|
|
|
if (state === _sync.SyncState.Syncing && oldState !== _sync.SyncState.Syncing) {
|
|
|
|
|
_logger.logger.info(`Resuming queue after resumed sync`);
|
|
|
|
|
this.sendQueue();
|
2026-09-12 23:57:45 -04:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
start() {
|
|
|
|
|
this.running = true;
|
|
|
|
|
this.sendQueue();
|
2026-09-13 14:14:38 -04:00
|
|
|
this.client.on(_client.ClientEvent.Sync, this.onResumedSync);
|
2026-09-12 23:57:45 -04:00
|
|
|
}
|
|
|
|
|
stop() {
|
|
|
|
|
this.running = false;
|
|
|
|
|
if (this.retryTimeout !== null) clearTimeout(this.retryTimeout);
|
|
|
|
|
this.retryTimeout = null;
|
2026-09-13 14:14:38 -04:00
|
|
|
this.client.removeListener(_client.ClientEvent.Sync, this.onResumedSync);
|
2026-09-12 23:57:45 -04:00
|
|
|
}
|
|
|
|
|
async queueBatch(batch) {
|
|
|
|
|
const batches = [];
|
|
|
|
|
for (let i = 0; i < batch.batch.length; i += MAX_BATCH_SIZE) {
|
|
|
|
|
const batchWithTxnId = {
|
|
|
|
|
eventType: batch.eventType,
|
|
|
|
|
batch: batch.batch.slice(i, i + MAX_BATCH_SIZE),
|
|
|
|
|
txnId: this.client.makeTxnId()
|
|
|
|
|
};
|
|
|
|
|
batches.push(batchWithTxnId);
|
2026-09-13 14:14:38 -04:00
|
|
|
const msgmap = batchWithTxnId.batch.map(msg => `${msg.userId}/${msg.deviceId} (msgid ${msg.payload[_event.ToDeviceMessageId]})`);
|
|
|
|
|
_logger.logger.info(`Enqueuing batch of to-device messages. type=${batch.eventType} txnid=${batchWithTxnId.txnId}`, msgmap);
|
2026-09-12 23:57:45 -04:00
|
|
|
}
|
|
|
|
|
await this.client.store.saveToDeviceBatches(batches);
|
2026-09-13 14:14:38 -04:00
|
|
|
this.sendQueue();
|
2026-09-12 23:57:45 -04:00
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Attempts to send a batch of to-device messages.
|
|
|
|
|
*/
|
|
|
|
|
async sendBatch(batch) {
|
2026-09-13 14:14:38 -04:00
|
|
|
const contentMap = new _utils.MapWithDefault(() => new Map());
|
2026-09-12 23:57:45 -04:00
|
|
|
for (const item of batch.batch) {
|
|
|
|
|
contentMap.getOrCreate(item.userId).set(item.deviceId, item.payload);
|
|
|
|
|
}
|
2026-09-13 14:14:38 -04:00
|
|
|
_logger.logger.info(`Sending batch of ${batch.batch.length} to-device messages with ID ${batch.id} and txnId ${batch.txnId}`);
|
2026-09-12 23:57:45 -04:00
|
|
|
await this.client.sendToDevice(batch.eventType, contentMap, batch.txnId);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-09-13 14:14:38 -04:00
|
|
|
exports.ToDeviceMessageQueue = ToDeviceMessageQueue;
|
2026-09-12 23:57:45 -04:00
|
|
|
//# sourceMappingURL=ToDeviceMessageQueue.js.map
|