mirror of
https://github.com/misskey-dev/misskey.git
synced 2024-12-27 22:39:33 +09:00
Merge pull request #176 from KOBA789/debounce-note-pack
Debounce SQL in note pack
This commit is contained in:
commit
99e14d3a54
@ -26,6 +26,7 @@ import type {
|
||||
} from '@/models/index.js';
|
||||
import { bindThis } from '@/decorators.js';
|
||||
import { isNotNull } from '@/misc/is-not-null.js';
|
||||
import { DebounceLoader } from '@/misc/loader.js';
|
||||
import type { OnModuleInit } from '@nestjs/common';
|
||||
import type { CustomEmojiService } from '../CustomEmojiService.js';
|
||||
import type { ReactionService } from '../ReactionService.js';
|
||||
@ -38,6 +39,7 @@ export class NoteEntityService implements OnModuleInit {
|
||||
private driveFileEntityService: DriveFileEntityService;
|
||||
private customEmojiService: CustomEmojiService;
|
||||
private reactionService: ReactionService;
|
||||
private noteLoader = new DebounceLoader(this.findNoteOrFail);
|
||||
|
||||
constructor(
|
||||
private moduleRef: ModuleRef,
|
||||
@ -304,7 +306,7 @@ export class NoteEntityService implements OnModuleInit {
|
||||
}, options);
|
||||
|
||||
const meId = me ? me.id : null;
|
||||
const note = typeof src === 'object' ? src : await this.notesRepository.findOneOrFail({ where: { id: src }, relations: ['user'] });
|
||||
const note = typeof src === 'object' ? src : await this.noteLoader.load(src);
|
||||
const host = note.userHost;
|
||||
|
||||
let text = note.text;
|
||||
@ -483,4 +485,12 @@ export class NoteEntityService implements OnModuleInit {
|
||||
|
||||
return await query.getCount();
|
||||
}
|
||||
|
||||
@bindThis
|
||||
private async findNoteOrFail(id: string): Promise<MiNote> {
|
||||
return await this.notesRepository.findOneOrFail({
|
||||
where: { id },
|
||||
relations: ["user"],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
49
packages/backend/src/misc/loader.ts
Normal file
49
packages/backend/src/misc/loader.ts
Normal file
@ -0,0 +1,49 @@
|
||||
export type FetchFunction<K, V> = (key: K) => Promise<V>;
|
||||
type ResolveReject<V> = Parameters<ConstructorParameters<typeof Promise<V>>[0]>;
|
||||
type ResolverPair<V> = {
|
||||
resolve: ResolveReject<V>[0];
|
||||
reject: ResolveReject<V>[1];
|
||||
};
|
||||
export class DebounceLoader<K, V> {
|
||||
private resolverMap = new Map<K, ResolverPair<V>>();
|
||||
private promiseMap = new Map<K, Promise<V>>();
|
||||
private resolvedPromise = Promise.resolve();
|
||||
constructor(private loadFn: FetchFunction<K, V>) {}
|
||||
|
||||
public load(key: K): Promise<V> {
|
||||
const promise = this.promiseMap.get(key);
|
||||
if (typeof promise !== 'undefined') {
|
||||
return promise;
|
||||
}
|
||||
|
||||
const isFirst = this.promiseMap.size === 0;
|
||||
const newPromise = new Promise<V>((resolve, reject) => {
|
||||
this.resolverMap.set(key, { resolve, reject });
|
||||
});
|
||||
this.promiseMap.set(key, newPromise);
|
||||
|
||||
if (isFirst) {
|
||||
this.enqueueDebouncedLoadJob();
|
||||
}
|
||||
|
||||
return newPromise;
|
||||
}
|
||||
|
||||
private runDebouncedLoad(): void {
|
||||
const resolvers = [...this.resolverMap];
|
||||
this.resolverMap.clear();
|
||||
this.promiseMap.clear();
|
||||
|
||||
for (const [key, { resolve, reject }] of resolvers) {
|
||||
this.loadFn(key).then(resolve, reject);
|
||||
}
|
||||
}
|
||||
|
||||
private enqueueDebouncedLoadJob(): void {
|
||||
this.resolvedPromise.then(() => {
|
||||
process.nextTick(() => {
|
||||
this.runDebouncedLoad();
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
88
packages/backend/test/unit/misc/loader.ts
Normal file
88
packages/backend/test/unit/misc/loader.ts
Normal file
@ -0,0 +1,88 @@
|
||||
import { DebounceLoader } from '@/misc/loader.js';
|
||||
|
||||
class Mock {
|
||||
loadCountByKey = new Map<number, number>();
|
||||
load = async (key: number): Promise<number> => {
|
||||
const count = this.loadCountByKey.get(key);
|
||||
if (typeof count === 'undefined') {
|
||||
this.loadCountByKey.set(key, 1);
|
||||
} else {
|
||||
this.loadCountByKey.set(key, count + 1);
|
||||
}
|
||||
return key * 2;
|
||||
};
|
||||
reset() {
|
||||
this.loadCountByKey.clear();
|
||||
}
|
||||
}
|
||||
|
||||
describe(DebounceLoader, () => {
|
||||
describe('single request', () => {
|
||||
it('loads once', async () => {
|
||||
const mock = new Mock();
|
||||
const loader = new DebounceLoader(mock.load);
|
||||
expect(await loader.load(7)).toBe(14);
|
||||
expect(mock.loadCountByKey.size).toBe(1);
|
||||
expect(mock.loadCountByKey.get(7)).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('two duplicated requests at same time', () => {
|
||||
it('loads once', async () => {
|
||||
const mock = new Mock();
|
||||
const loader = new DebounceLoader(mock.load);
|
||||
const [v1, v2] = await Promise.all([
|
||||
loader.load(7),
|
||||
loader.load(7),
|
||||
]);
|
||||
expect(v1).toBe(14);
|
||||
expect(v2).toBe(14);
|
||||
expect(mock.loadCountByKey.size).toBe(1);
|
||||
expect(mock.loadCountByKey.get(7)).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('two different requests at same time', () => {
|
||||
it('loads twice', async () => {
|
||||
const mock = new Mock();
|
||||
const loader = new DebounceLoader(mock.load);
|
||||
const [v1, v2] = await Promise.all([
|
||||
loader.load(7),
|
||||
loader.load(13),
|
||||
]);
|
||||
expect(v1).toBe(14);
|
||||
expect(v2).toBe(26);
|
||||
expect(mock.loadCountByKey.size).toBe(2);
|
||||
expect(mock.loadCountByKey.get(7)).toBe(1);
|
||||
expect(mock.loadCountByKey.get(13)).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('non-continuous same two requests', () => {
|
||||
it('loads twice', async () => {
|
||||
const mock = new Mock();
|
||||
const loader = new DebounceLoader(mock.load);
|
||||
expect(await loader.load(7)).toBe(14);
|
||||
expect(mock.loadCountByKey.size).toBe(1);
|
||||
expect(mock.loadCountByKey.get(7)).toBe(1);
|
||||
mock.reset();
|
||||
expect(await loader.load(7)).toBe(14);
|
||||
expect(mock.loadCountByKey.size).toBe(1);
|
||||
expect(mock.loadCountByKey.get(7)).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('non-continuous different two requests', () => {
|
||||
it('loads twice', async () => {
|
||||
const mock = new Mock();
|
||||
const loader = new DebounceLoader(mock.load);
|
||||
expect(await loader.load(7)).toBe(14);
|
||||
expect(mock.loadCountByKey.size).toBe(1);
|
||||
expect(mock.loadCountByKey.get(7)).toBe(1);
|
||||
mock.reset();
|
||||
expect(await loader.load(13)).toBe(26);
|
||||
expect(mock.loadCountByKey.size).toBe(1);
|
||||
expect(mock.loadCountByKey.get(13)).toBe(1);
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user