perf(server): Cache user keypair

This commit is contained in:
syuilo 2021-03-22 15:14:54 +09:00
parent 202e943d55
commit 7c3086e9d9
5 changed files with 21 additions and 15 deletions

10
src/misc/keypair-store.ts Normal file
View File

@ -0,0 +1,10 @@
import { UserKeypairs } from '../models';
import { User } from '../models/entities/user';
import { UserKeypair } from '../models/entities/user-keypair';
import { Cache } from './cache';
const cache = new Cache<UserKeypair>(Infinity);
export async function getUserKeypair(userId: User['id']): Promise<UserKeypair> {
return await cache.fetch(userId, async () => await UserKeypairs.findOneOrFail(userId));
}

View File

@ -3,7 +3,7 @@ import { v4 as uuid } from 'uuid';
import { IActivity } from '../type'; import { IActivity } from '../type';
import { LdSignature } from '../misc/ld-signature'; import { LdSignature } from '../misc/ld-signature';
import { ILocalUser } from '../../../models/entities/user'; import { ILocalUser } from '../../../models/entities/user';
import { UserKeypairs } from '../../../models'; import { getUserKeypair } from '../../../misc/keypair-store';
export const renderActivity = (x: any): IActivity | null => { export const renderActivity = (x: any): IActivity | null => {
if (x == null) return null; if (x == null) return null;
@ -23,9 +23,7 @@ export const renderActivity = (x: any): IActivity | null => {
export const attachLdSignature = async (activity: any, user: ILocalUser): Promise<IActivity | null> => { export const attachLdSignature = async (activity: any, user: ILocalUser): Promise<IActivity | null> => {
if (activity == null) return null; if (activity == null) return null;
const keypair = await UserKeypairs.findOneOrFail({ const keypair = await getUserKeypair(user.id);
userId: user.id
});
const obj = { const obj = {
// as non-standards // as non-standards

View File

@ -8,7 +8,8 @@ import { getEmojis } from './note';
import renderEmoji from './emoji'; import renderEmoji from './emoji';
import { IIdentifier } from '../models/identifier'; import { IIdentifier } from '../models/identifier';
import renderHashtag from './hashtag'; import renderHashtag from './hashtag';
import { DriveFiles, UserProfiles, UserKeypairs } from '../../../models'; import { DriveFiles, UserProfiles } from '../../../models';
import { getUserKeypair } from '../../../misc/keypair-store';
export async function renderPerson(user: ILocalUser) { export async function renderPerson(user: ILocalUser) {
const id = `${config.url}/users/${user.id}`; const id = `${config.url}/users/${user.id}`;
@ -49,7 +50,7 @@ export async function renderPerson(user: ILocalUser) {
...hashtagTags, ...hashtagTags,
]; ];
const keypair = await UserKeypairs.findOneOrFail(user.id); const keypair = await getUserKeypair(user.id);
const person = { const person = {
type: isSystem ? 'Application' : user.isBot ? 'Service' : 'Person', type: isSystem ? 'Application' : user.isBot ? 'Service' : 'Person',

View File

@ -5,11 +5,11 @@ import * as crypto from 'crypto';
import config from '../../config'; import config from '../../config';
import { ILocalUser } from '../../models/entities/user'; import { ILocalUser } from '../../models/entities/user';
import { UserKeypairs } from '../../models';
import { getAgentByUrl } from '../../misc/fetch'; import { getAgentByUrl } from '../../misc/fetch';
import { URL } from 'url'; import { URL } from 'url';
import got from 'got'; import got from 'got';
import * as Got from 'got'; import * as Got from 'got';
import { getUserKeypair } from '../../misc/keypair-store';
export default async (user: ILocalUser, url: string, object: any) => { export default async (user: ILocalUser, url: string, object: any) => {
const timeout = 10 * 1000; const timeout = 10 * 1000;
@ -22,9 +22,7 @@ export default async (user: ILocalUser, url: string, object: any) => {
sha256.update(data); sha256.update(data);
const hash = sha256.digest('base64'); const hash = sha256.digest('base64');
const keypair = await UserKeypairs.findOneOrFail({ const keypair = await getUserKeypair(user.id);
userId: user.id
});
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
const req = https.request({ const req = https.request({
@ -74,9 +72,7 @@ export default async (user: ILocalUser, url: string, object: any) => {
export async function signedGet(url: string, user: ILocalUser) { export async function signedGet(url: string, user: ILocalUser) {
const timeout = 10 * 1000; const timeout = 10 * 1000;
const keypair = await UserKeypairs.findOneOrFail({ const keypair = await getUserKeypair(user.id);
userId: user.id
});
const req = got.get<any>(url, { const req = got.get<any>(url, {
headers: { headers: {

View File

@ -13,10 +13,11 @@ import Following from './activitypub/following';
import Featured from './activitypub/featured'; import Featured from './activitypub/featured';
import { inbox as processInbox } from '../queue'; import { inbox as processInbox } from '../queue';
import { isSelfHost } from '../misc/convert-host'; import { isSelfHost } from '../misc/convert-host';
import { Notes, Users, Emojis, UserKeypairs, NoteReactions } from '../models'; import { Notes, Users, Emojis, NoteReactions } from '../models';
import { ILocalUser, User } from '../models/entities/user'; import { ILocalUser, User } from '../models/entities/user';
import { In } from 'typeorm'; import { In } from 'typeorm';
import { renderLike } from '../remote/activitypub/renderer/like'; import { renderLike } from '../remote/activitypub/renderer/like';
import { getUserKeypair } from '../misc/keypair-store';
// Init router // Init router
const router = new Router(); const router = new Router();
@ -135,7 +136,7 @@ router.get('/users/:user/publickey', async ctx => {
return; return;
} }
const keypair = await UserKeypairs.findOneOrFail(user.id); const keypair = await getUserKeypair(user.id);
if (Users.isLocalUser(user)) { if (Users.isLocalUser(user)) {
ctx.body = renderActivity(renderKey(user, keypair)); ctx.body = renderActivity(renderKey(user, keypair));