Skip to content

Commit

Permalink
Optimize fetchInstanceAP queries
Browse files Browse the repository at this point in the history
  • Loading branch information
TheKinrar committed Dec 12, 2022
1 parent f6741bf commit bb642f7
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,24 @@ async function fetchInstanceAP(options) {
await instance.save();

try {
let peers = await request({
let peers = (await request({
url: `https://${instance.name}/api/v1/instance/peers`,
json: true
});
})).filter(p => isValidDomain(p, {allowUnicode: true, subdomain: true}))
.map(p => p.toLowerCase());

let existing = await pg.query(pgFormat('SELECT lower(name) AS name FROM instances WHERE lower(name) IN (%L)', peers));
let missing = peers.filter(p => !existing.rows.some(r => r.name === p));

let values = peers
.filter(p => isValidDomain(p, {allowUnicode: true, subdomain: true}))
let values = missing
.map(p => [
flake.gen(),
p.toLowerCase()
p
]);

await pg.query(pgFormat('INSERT INTO instances(id, name) VALUES %L ON CONFLICT DO NOTHING RETURNING id,name', values));
if(values.length !== 0) {
await pg.query(pgFormat('INSERT INTO instances(id, name) VALUES %L', values));
}
} catch(e) {}

try {
Expand Down

0 comments on commit bb642f7

Please sign in to comment.