Skip to content

Commit

Permalink
Merge pull request #264 from matrix-org/tadzik/gh-236/reinforce-xmpp-…
Browse files Browse the repository at this point in the history
…connection-handling

Make XMPP connection handling more resilient
  • Loading branch information
tadzik authored Jul 29, 2021
2 parents 452e4f4 + 91376a9 commit 87bb575
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
1 change: 1 addition & 0 deletions changelog.d/264.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make XMPP connection handling more resilient
22 changes: 14 additions & 8 deletions src/xmppjs/XJSInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ export class XmppJsInstance extends EventEmitter implements IBifrostInstance {
private myAddress!: JID;
private accounts: Map<string, XmppJsAccount>;
private seenMessages: Set<string>;
private canWrite: boolean;
private defaultRes!: string;
private connectionWasDropped: boolean;
private bufferedMessages: {xmlMsg: Element|string, resolve: (res: Promise<void>) => void}[];
Expand All @@ -78,7 +77,6 @@ export class XmppJsInstance extends EventEmitter implements IBifrostInstance {
private lastMessageInMUC: Map<string, {originIsMatrix: boolean, id: string}>;
constructor(private config: Config) {
super();
this.canWrite = false;
this.accounts = new Map();
this.bufferedMessages = [];
this.seenMessages = new Set();
Expand Down Expand Up @@ -148,7 +146,16 @@ export class XmppJsInstance extends EventEmitter implements IBifrostInstance {
const xml = typeof(xmlMsg) === "string" ? xmlMsg : xmlMsg.xml;
let p: Promise<unknown>;
if (this.canWrite) {
p = this.xmpp.write(xml);
this.xmpp.write(xml).catch((err: Error) => {
// This should only happen in case of a connection error
// that xmpp.js hasn't noticed yet for some reason.
// xmpp.js recovers from these automatically,
// so we can reschedule this for post-connection and hope it goes through then.
log.error("Error writing xmpp stanza:", err.toString(), "scheduling it for later");
p = new Promise((resolve) => {
this.bufferedMessages.push({xmlMsg: xml, resolve});
});
});
} else {
p = new Promise((resolve) => {
this.bufferedMessages.push({xmlMsg: xml, resolve});
Expand Down Expand Up @@ -242,7 +249,6 @@ export class XmppJsInstance extends EventEmitter implements IBifrostInstance {
xmpp.on("online", (address) => {
xLog.info("gone online as " + address);
this.myAddress = address;
this.canWrite = true;
log.info(`flushing ${this.bufferedMessages.length} buffered messages`);
if (this.connectionWasDropped) {
log.warn("Connection was dropped, attempting reconnect..");
Expand All @@ -262,9 +268,6 @@ export class XmppJsInstance extends EventEmitter implements IBifrostInstance {

// Debug
xmpp.on("status", (status) => {
if (status === "disconnecting" || status === "disconnected") {
this.canWrite = false;
}
if (status === "disconnect") {
log.error("Connection to XMPP server was lost..");
this.connectionWasDropped = true;
Expand All @@ -280,7 +283,6 @@ export class XmppJsInstance extends EventEmitter implements IBifrostInstance {
xLog.info("status: reconnecting");
});


if (opts.logRawStream) {
xmpp.on("input", (input) => {
xLog.debug("RX:", input);
Expand Down Expand Up @@ -938,4 +940,8 @@ export class XmppJsInstance extends EventEmitter implements IBifrostInstance {
}
}
}

private get canWrite(): boolean {
return this.xmpp?.status === 'online';
}
}

0 comments on commit 87bb575

Please sign in to comment.