mirror of
https://git.sr.ht/~emerson/reflectionircd
synced 2025-08-05 16:59:10 +00:00
rework sync so we don't pile requests up
This commit is contained in:
parent
2e973a18db
commit
5a5991db50
3 changed files with 20 additions and 13 deletions
|
@ -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.sendMessage(this.server.name, 'NOTICE', [this.user.nick, 'You are now synced to the network!'], message.tags);
|
||||||
this.user.addClient(this, message.tags);
|
this.user.addClient(this, message.tags);
|
||||||
}).catch(function (error) {
|
}).catch(function (error) {
|
||||||
if (error.response) {
|
console.log(error);
|
||||||
console.log(error.response.data);
|
|
||||||
} else if (error.request) {
|
|
||||||
console.log(error.request);
|
|
||||||
} else {
|
|
||||||
console.log('Error', error.message);
|
|
||||||
console.log(error.config);
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,8 @@ export class IRCUser {
|
||||||
private txnIdStore: Map<string, Client>
|
private txnIdStore: Map<string, Client>
|
||||||
public nextBatch: string
|
public nextBatch: string
|
||||||
private initialSync: boolean
|
private initialSync: boolean
|
||||||
|
private isSyncing: boolean
|
||||||
|
private currentSyncTime: number
|
||||||
private syncIntervalID: NodeJS.Timeout;
|
private syncIntervalID: NodeJS.Timeout;
|
||||||
constructor(public mxid: string, private accessToken: string, public homeserver: string, private server: Server) {
|
constructor(public mxid: string, private accessToken: string, public homeserver: string, private server: Server) {
|
||||||
this.clients = new Set();
|
this.clients = new Set();
|
||||||
|
@ -30,6 +32,8 @@ export class IRCUser {
|
||||||
this.txnIdStore = new Map();
|
this.txnIdStore = new Map();
|
||||||
this.nextBatch = "";
|
this.nextBatch = "";
|
||||||
this.initialSync = false;
|
this.initialSync = false;
|
||||||
|
this.isSyncing = false;
|
||||||
|
this.currentSyncTime = 0;
|
||||||
this.syncIntervalID = setInterval(this.doSync.bind(this), 2000);
|
this.syncIntervalID = setInterval(this.doSync.bind(this), 2000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,7 +71,14 @@ export class IRCUser {
|
||||||
console.log("not syncing, initial sync not completed");
|
console.log("not syncing, initial sync not completed");
|
||||||
return;
|
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 => {
|
axios.get(endpoint).then(response => {
|
||||||
const data = response.data;
|
const data = response.data;
|
||||||
this.nextBatch = data.next_batch;
|
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) {
|
if (error.response) {
|
||||||
console.log(error.response.data);
|
console.log(`Response error: ${error.response.status}`);
|
||||||
console.log(error.response.status);
|
|
||||||
} else if (error.request) {
|
} else if (error.request) {
|
||||||
console.log(error.request);
|
console.log(error.request);
|
||||||
|
console.log(error.config);
|
||||||
} else {
|
} else {
|
||||||
console.log('Error', error.message);
|
console.log('Error', error.message);
|
||||||
console.log(error.config);
|
console.log(error.config);
|
||||||
}
|
}
|
||||||
|
this.isSyncing = false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -116,6 +116,6 @@ export function parseIRCMessage(rawLine: string) {
|
||||||
if (lastParam !== '') {
|
if (lastParam !== '') {
|
||||||
params.push(lastParam);
|
params.push(lastParam);
|
||||||
}
|
}
|
||||||
console.log(parsedTags, prefix, command, params);
|
//console.log(parsedTags, prefix, command, params);
|
||||||
return new IRCMessage(parsedTags, prefix, command, params);
|
return new IRCMessage(parsedTags, prefix, command, params);
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue