Skip to content

Commit

Permalink
Added expanded followers data to the db
Browse files Browse the repository at this point in the history
We're running into some performance issues when fetching large followers lists.
Ultimately we need to move away from using MySQL as a key value store - but for
now we can store the expanded list which should help with performance.
  • Loading branch information
allouis committed Aug 6, 2024
1 parent 6385c8c commit 11a2c46
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions src/dispatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export async function handleFollow(
ctx.data.globaldb.set([sender.id.href], senderJson);
await addToList(ctx.data.db, ['inbox'], follow.id.href);
await addToList(ctx.data.db, ['followers'], sender.id.href);
await addToList(ctx.data.db, ['followers', 'expanded'], senderJson);

const acceptId = ctx.getObjectUri(Accept, { id: uuidv4() });
const accept = new Accept({
Expand Down Expand Up @@ -218,13 +219,24 @@ export async function inboxErrorHandler(

async function lookupPerson(ctx: RequestContext<ContextData>, url: string) {
try {
console.log('Looking up person locally', url);
const local = await ctx.data.globaldb.get([url]);
return await Person.fromJsonLd(local);
} catch (err) {
const remote = await lookupObject(url);
if (remote instanceof Person) {
await ctx.data.globaldb.set([url], await remote.toJsonLd());
return remote;
console.log('Error looking up person locally', url);
console.log(err);
console.log('Looking up person remotely', url);
const documentLoader = await ctx.getDocumentLoader({handle: 'index'});
try {
const remote = await lookupObject(url, {documentLoader});
if (remote instanceof Person) {
await ctx.data.globaldb.set([url], await remote.toJsonLd());
return remote;
}
} catch (err) {
console.log('Error looking up person remotely', url);
console.log(err)
return null;
}
}
return null;
Expand All @@ -235,18 +247,15 @@ export async function followersDispatcher(
handle: string,
) {
console.log('Followers Dispatcher');
const results = (await ctx.data.db.get<string[]>(['followers'])) || [];
console.log(results);
let items: Person[] = [];
for (const result of results) {
try {
const thing = await lookupPerson(ctx, result);
if (thing instanceof Person) {
items.push(thing);
}
} catch (err) {
console.log(err);
}
const fullResults = await ctx.data.db.get<any[]>(['followers', 'expanded']);
if (fullResults) {
items = await Promise.all(fullResults.map(result => Person.fromJsonLd(result)));
} else {
const results = (await ctx.data.db.get<string[]>(['followers'])) || [];
const things = (await Promise.all(results.map((result) => lookupPerson(ctx, result)))).filter(thing => thing instanceof Person);
items = things.filter(thing => thing !== null);
await ctx.data.db.set(['followers', 'expanded'], await Promise.all(items.map(person => person.toJsonLd())));
}
return {
items,
Expand Down

0 comments on commit 11a2c46

Please sign in to comment.