vector-im-hydrogen-web/src/matrix/room/sending/SendQueue.js

284 lines
11 KiB
JavaScript
Raw Normal View History

2020-08-05 18:38:55 +02:00
/*
Copyright 2020 Bruno Windels <bruno@windels.cloud>
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.
*/
import {SortedArray} from "../../../observable/list/SortedArray.js";
2020-04-19 19:05:12 +02:00
import {ConnectionError} from "../../error.js";
2021-05-20 11:43:09 +02:00
import {PendingEvent, SendStatus} from "./PendingEvent.js";
import {makeTxnId, isTxnId} from "../../common.js";
2021-05-20 10:01:30 +02:00
import {REDACTION_TYPE} from "../common.js";
2019-07-01 10:00:29 +02:00
export class SendQueue {
constructor({roomId, storage, hsApi, pendingEvents}) {
pendingEvents = pendingEvents || [];
2019-07-26 22:03:57 +02:00
this._roomId = roomId;
this._storage = storage;
this._hsApi = hsApi;
2019-07-26 22:03:57 +02:00
this._pendingEvents = new SortedArray((a, b) => a.queueIndex - b.queueIndex);
this._pendingEvents.setManyUnsorted(pendingEvents.map(data => this._createPendingEvent(data)));
2019-07-26 22:03:57 +02:00
this._isSending = false;
2019-06-28 00:52:54 +02:00
this._offline = false;
this._roomEncryption = null;
}
_createPendingEvent(data, attachments = null) {
const pendingEvent = new PendingEvent({
data,
remove: () => this._removeEvent(pendingEvent),
emitUpdate: () => this._pendingEvents.update(pendingEvent),
attachments
});
return pendingEvent;
}
enableEncryption(roomEncryption) {
this._roomEncryption = roomEncryption;
2019-06-28 00:52:54 +02:00
}
2021-02-23 19:22:59 +01:00
_sendLoop(log) {
2019-07-26 22:03:57 +02:00
this._isSending = true;
2021-02-23 19:22:59 +01:00
this._sendLoopLogItem = log.runDetached("send queue flush", async log => {
try {
for (const pendingEvent of this._pendingEvents) {
2021-02-23 19:22:59 +01:00
await log.wrap("send event", async log => {
log.set("queueIndex", pendingEvent.queueIndex);
2021-02-23 19:22:59 +01:00
try {
await this._sendEvent(pendingEvent, log);
} catch(err) {
if (err instanceof ConnectionError) {
this._offline = true;
log.set("offline", true);
pendingEvent.setWaiting();
2021-02-23 19:22:59 +01:00
} else {
log.catch(err);
const isPermanentError = err.name === "HomeServerError" && (
err.statusCode === 400 || // bad request, must be a bug on our end
err.statusCode === 403 || // forbidden
err.statusCode === 404 // not found
);
if (isPermanentError) {
log.set("remove", true);
await pendingEvent.abort();
} else {
pendingEvent.setError(err);
}
2021-02-23 19:22:59 +01:00
}
}
});
}
} finally {
this._isSending = false;
this._sendLoopLogItem = null;
2019-06-28 00:52:54 +02:00
}
2021-02-23 19:22:59 +01:00
});
2019-06-28 00:52:54 +02:00
}
2021-02-23 19:22:59 +01:00
async _sendEvent(pendingEvent, log) {
if (pendingEvent.needsUpload) {
2021-02-23 19:22:59 +01:00
await log.wrap("upload attachments", log => pendingEvent.uploadAttachments(this._hsApi, log));
await this._tryUpdateEvent(pendingEvent);
}
if (pendingEvent.needsEncryption) {
pendingEvent.setEncrypting();
2021-02-23 19:22:59 +01:00
const {type, content} = await log.wrap("encrypt", log => this._roomEncryption.encrypt(
pendingEvent.eventType, pendingEvent.content, this._hsApi, log));
pendingEvent.setEncrypted(type, content);
await this._tryUpdateEvent(pendingEvent);
}
if (pendingEvent.needsSending) {
2021-02-23 19:22:59 +01:00
await pendingEvent.send(this._hsApi, log);
// we now have a remoteId, but this pending event may be removed at any point in the future
// once the remote echo comes in. So if we have any related events that need to resolve
// the relatedTxnId to a related event id, they need to do so now.
// We ensure this by writing the new remote id for the pending event and all related events
// with unresolved relatedTxnId in the queue in one transaction.
const relatedEvents = this._pendingEvents.array.filter(pe => pe.relatedTxnId === pendingEvent.txnId);
const txn = await this._storage.readWriteTxn([this._storage.storeNames.pendingEvents]);
try {
await this._tryUpdateEventWithTxn(pendingEvent, txn);
for (const relatedPE of relatedEvents) {
relatedPE.setRelatedEventId(pendingEvent.remoteId);
await this._tryUpdateEventWithTxn(relatedPE, txn);
// emit that we now have a related remote id
this._pendingEvents.update(relatedPE)
}
} catch (err) {
txn.abort();
throw err;
}
await txn.complete();
}
}
removeRemoteEchos(events, txn, parentLog) {
const removed = [];
for (const event of events) {
const txnId = event.unsigned && event.unsigned.transaction_id;
let idx;
if (txnId) {
idx = this._pendingEvents.array.findIndex(pe => pe.txnId === txnId);
} else {
idx = this._pendingEvents.array.findIndex(pe => pe.remoteId === event.event_id);
}
if (idx !== -1) {
const pendingEvent = this._pendingEvents.get(idx);
2021-04-08 18:39:52 +02:00
parentLog.log({l: "removeRemoteEcho", queueIndex: pendingEvent.queueIndex, remoteId: event.event_id, txnId});
txn.pendingEvents.remove(pendingEvent.roomId, pendingEvent.queueIndex);
removed.push(pendingEvent);
}
}
return removed;
}
2019-06-28 00:52:54 +02:00
async _removeEvent(pendingEvent) {
const idx = this._pendingEvents.array.indexOf(pendingEvent);
if (idx !== -1) {
const txn = await this._storage.readWriteTxn([this._storage.storeNames.pendingEvents]);
try {
txn.pendingEvents.remove(pendingEvent.roomId, pendingEvent.queueIndex);
} catch (err) {
txn.abort();
}
await txn.complete();
this._pendingEvents.remove(idx);
}
pendingEvent.dispose();
}
emitRemovals(pendingEvents) {
for (const pendingEvent of pendingEvents) {
const idx = this._pendingEvents.array.indexOf(pendingEvent);
if (idx !== -1) {
this._pendingEvents.remove(idx);
}
pendingEvent.dispose();
2019-07-26 22:03:57 +02:00
}
2019-06-28 00:52:54 +02:00
}
2021-02-23 19:22:59 +01:00
resumeSending(parentLog) {
2019-07-26 22:03:57 +02:00
this._offline = false;
2021-02-23 19:22:59 +01:00
if (this._pendingEvents.length) {
parentLog.wrap("resumeSending", log => {
log.set("id", this._roomId);
log.set("pendingEvents", this._pendingEvents.length);
if (!this._isSending) {
this._sendLoop(log);
}
if (this._sendLoopLogItem) {
log.refDetached(this._sendLoopLogItem);
}
});
2019-07-26 22:03:57 +02:00
}
2019-06-28 00:52:54 +02:00
}
2019-07-01 10:00:29 +02:00
2021-02-23 19:22:59 +01:00
async enqueueEvent(eventType, content, attachments, log) {
await this._enqueueEvent(eventType, content, attachments, null, log);
}
async _enqueueEvent(eventType, content, attachments, redacts, log) {
const pendingEvent = await this._createAndStoreEvent(eventType, content, redacts, attachments);
2019-07-26 22:03:57 +02:00
this._pendingEvents.set(pendingEvent);
2021-02-23 19:22:59 +01:00
log.set("queueIndex", pendingEvent.queueIndex);
log.set("pendingEvents", this._pendingEvents.length);
2019-07-26 22:03:57 +02:00
if (!this._isSending && !this._offline) {
2021-02-23 19:22:59 +01:00
this._sendLoop(log);
}
if (this._sendLoopLogItem) {
log.refDetached(this._sendLoopLogItem);
2019-07-26 22:03:57 +02:00
}
2019-07-01 10:00:29 +02:00
}
2019-06-28 00:52:54 +02:00
async enqueueRedaction(eventIdOrTxnId, reason, log) {
if (isTxnId(eventIdOrTxnId)) {
log.set("txnIdToRedact", eventIdOrTxnId);
const txnId = eventIdOrTxnId;
const pe = this._pendingEvents.array.find(pe => pe.txnId === txnId);
if (pe && !pe.remoteId && pe.status !== SendStatus.Sending) {
// haven't started sending this event yet,
// just remove it from the queue
await pe.abort();
return;
} else if (!pe) {
// we don't have the pending event anymore,
// the remote echo must have arrived in the meantime.
// we could look for it in the timeline, but for now
// we don't do anything as this race is quite unlikely
// and a bit complicated to fix.
return;
}
} else {
log.set("eventIdToRedact", eventIdOrTxnId);
}
await this._enqueueEvent(REDACTION_TYPE, {reason}, null, eventIdOrTxnId, log);
}
2019-07-26 22:03:57 +02:00
get pendingEvents() {
return this._pendingEvents;
2019-07-01 10:00:29 +02:00
}
2019-07-26 22:03:57 +02:00
async _tryUpdateEvent(pendingEvent) {
const txn = await this._storage.readWriteTxn([this._storage.storeNames.pendingEvents]);
2019-07-26 22:03:57 +02:00
try {
this._tryUpdateEventWithTxn(pendingEvent, txn);
2019-07-26 22:03:57 +02:00
} catch (err) {
txn.abort();
throw err;
}
await txn.complete();
}
2019-07-01 10:00:29 +02:00
async _tryUpdateEventWithTxn(pendingEvent, txn) {
// pendingEvent might have been removed already here
// by a racing remote echo, so check first so we don't recreate it
if (await txn.pendingEvents.exists(pendingEvent.roomId, pendingEvent.queueIndex)) {
txn.pendingEvents.update(pendingEvent.data);
}
}
async _createAndStoreEvent(eventType, content, redacts, attachments) {
const txn = await this._storage.readWriteTxn([this._storage.storeNames.pendingEvents]);
2019-07-26 22:03:57 +02:00
let pendingEvent;
try {
const pendingEventsStore = txn.pendingEvents;
const maxQueueIndex = await pendingEventsStore.getMaxQueueIndex(this._roomId) || 0;
const queueIndex = maxQueueIndex + 1;
const needsEncryption = eventType !== REDACTION_TYPE && !!this._roomEncryption;
pendingEvent = this._createPendingEvent({
2019-07-26 22:03:57 +02:00
roomId: this._roomId,
queueIndex,
eventType,
content,
redacts,
txnId: makeTxnId(),
needsEncryption,
needsUpload: !!attachments
2020-11-13 17:19:19 +01:00
}, attachments);
2019-07-26 22:03:57 +02:00
pendingEventsStore.add(pendingEvent.data);
} catch (err) {
txn.abort();
throw err;
}
2019-07-01 10:00:29 +02:00
await txn.complete();
2019-07-26 22:03:57 +02:00
return pendingEvent;
2019-07-01 10:00:29 +02:00
}
dispose() {
for (const pe of this._pendingEvents) {
pe.dispose();
}
}
2019-06-28 00:52:54 +02:00
}