rework sync so we don't pile requests up

This commit is contained in:
emerson 2022-01-24 13:59:41 -05:00
parent 2e973a18db
commit 5a5991db50
3 changed files with 20 additions and 13 deletions

View file

@ -343,14 +343,7 @@ export class Client {
this.sendMessage(this.server.name, 'NOTICE', [this.user.nick, 'You are now synced to the network!'], message.tags);
this.user.addClient(this, message.tags);
}).catch(function (error) {
if (error.response) {
console.log(error.response.data);
} else if (error.request) {
console.log(error.request);
} else {
console.log('Error', error.message);
console.log(error.config);
}
console.log(error);
})
}
}

View file

@ -17,6 +17,8 @@ export class IRCUser {
private txnIdStore: Map<string, Client>
public nextBatch: string
private initialSync: boolean
private isSyncing: boolean
private currentSyncTime: number
private syncIntervalID: NodeJS.Timeout;
constructor(public mxid: string, private accessToken: string, public homeserver: string, private server: Server) {
this.clients = new Set();
@ -30,6 +32,8 @@ export class IRCUser {
this.txnIdStore = new Map();
this.nextBatch = "";
this.initialSync = false;
this.isSyncing = false;
this.currentSyncTime = 0;
this.syncIntervalID = setInterval(this.doSync.bind(this), 2000);
}
@ -67,7 +71,14 @@ export class IRCUser {
console.log("not syncing, initial sync not completed");
return;
}
const endpoint = `https://${this.homeserver}/_matrix/client/v3/sync?access_token=${this.accessToken}&since=${this.nextBatch}`;
if (this.isSyncing) {
if ((Date.now() - this.currentSyncTime) > 15000)
console.log(`Sync is lagging, current sync has been running for ${Date.now() - this.currentSyncTime} milliseconds`);
return;
}
this.currentSyncTime = Date.now();
this.isSyncing = true;
const endpoint = `https://${this.homeserver}/_matrix/client/v3/sync?access_token=${this.accessToken}&since=${this.nextBatch}&timeout=15000`;
axios.get(endpoint).then(response => {
const data = response.data;
this.nextBatch = data.next_batch;
@ -80,16 +91,19 @@ export class IRCUser {
});
}
}
}).catch(function (error) {
this.isSyncing = false;
}).catch((error) => {
console.log(error);
if (error.response) {
console.log(error.response.data);
console.log(error.response.status);
console.log(`Response error: ${error.response.status}`);
} else if (error.request) {
console.log(error.request);
console.log(error.config);
} else {
console.log('Error', error.message);
console.log(error.config);
}
this.isSyncing = false;
});
}

View file

@ -116,6 +116,6 @@ export function parseIRCMessage(rawLine: string) {
if (lastParam !== '') {
params.push(lastParam);
}
console.log(parsedTags, prefix, command, params);
//console.log(parsedTags, prefix, command, params);
return new IRCMessage(parsedTags, prefix, command, params);
}