mirror of
https://codeberg.org/yeentown/barkey.git
synced 2025-06-06 21:16:57 +00:00
* 1. ed25519キーペアを発行・Personとして公開鍵を送受信 * validate additionalPublicKeys * getAuthUserFromApIdはmainを選ぶ * ✌️ * fix * signatureAlgorithm * set publicKeyCache lifetime * refresh * httpMessageSignatureAcceptable * ED25519_SIGNED_ALGORITHM * ED25519_PUBLIC_KEY_SIGNATURE_ALGORITHM * remove sign additionalPublicKeys signature requirements * httpMessageSignaturesSupported * httpMessageSignaturesImplementationLevel * httpMessageSignaturesImplementationLevel: '01' * perf(federation): Use hint for getAuthUserFromApId (#13470) * Hint for getAuthUserFromApId * とどのつまりこれでいいのか? * use @misskey-dev/node-http-message-signatures * fix * signedPost, signedGet * ap-request.tsを復活させる * remove digest prerender * fix test? * fix test * add httpMessageSignaturesImplementationLevel to FederationInstance * ManyToOne * fetchPersonWithRenewal * exactKey * ✌️ * use const * use gen-key-pair fn. from '@misskey-dev/node-http-message-signatures' * update node-http-message-signatures * fix * @misskey-dev/node-http-message-signatures@0.0.0-alpha.11 * getAuthUserFromApIdでupdatePersonの頻度を増やす * cacheRaw.date * use requiredInputs https://github.com/misskey-dev/misskey/pull/13464#discussion_r1509964359 * update @misskey-dev/node-http-message-signatures * clean up * err msg * fix(backend): fetchInstanceMetadataのLockが永遠に解除されない問題を修正 Co-authored-by: まっちゃとーにゅ <17376330+u1-liquid@users.noreply.github.com> * fix httpMessageSignaturesImplementationLevel validation * fix test * fix * comment * comment * improve test * fix * use Promise.all in genRSAAndEd25519KeyPair * refreshAndprepareEd25519KeyPair * refreshAndfindKey * commetn * refactor public keys add * digestプリレンダを復活させる RFC実装時にどうするか考える * fix, async * fix * !== true * use save * Deliver update person when new key generated (not tested) https://github.com/misskey-dev/misskey/pull/13464#issuecomment-1977049061 * 循環参照で落ちるのを解消? * fix? * Revert "fix?" This reverts commit 0082f6f8e8c5d5febd14933ba9a1ac643f70ca92. * a * logger * log * change logger * 秘密鍵の変更は、フラグではなく鍵を引き回すようにする * addAllKnowingSharedInboxRecipe * nanka meccha kaeta * delivre * キャッシュ有効チェックはロック取得前に行う * @misskey-dev/node-http-message-signatures@0.0.3 * PrivateKeyPem * getLocalUserPrivateKey * fix test * if * fix ap-request * update node-http-message-signatures * fix type error * update package * fix type * update package * retry no key * @misskey-dev/node-http-message-signatures@0.0.8 * fix type error * log keyid * logger * db-resolver * JSON.stringify * HTTP Signatureがなかったり使えなかったりしそうな場合にLD Signatureを活用するように * inbox-delayed use actor if no signature * ユーザーとキーの同一性チェックはhostの一致にする * log signature parse err * save array * とりあえずtryで囲っておく * fetchPersonWithRenewalでエラーが起きたら古いデータを返す * use transactionalEntityManager * fix spdx * @misskey-dev/node-http-message-signatures@0.0.10 * add comment * fix * publicKeyに配列が入ってもいいようにする https://github.com/misskey-dev/misskey/pull/13950 * define additionalPublicKeys * fix * merge fix * refreshAndprepareEd25519KeyPair → refreshAndPrepareEd25519KeyPair * remove gen-key-pair.ts * defaultMaxListeners = 512 * Revert "defaultMaxListeners = 512" This reverts commit f2c412c18057a9300540794ccbe4dfbf6d259ed6. * genRSAAndEd25519KeyPairではキーを直列に生成する? * maxConcurrency: 8 * maxConcurrency: 16 * maxConcurrency: 8 * Revert "genRSAAndEd25519KeyPairではキーを直列に生成する?" This reverts commit d0aada55c1ed5aa98f18731ec82f3ac5eb5a6c16. * maxWorkers: '90%' * Revert "maxWorkers: '90%'" This reverts commit 9e0a93f110456320d6485a871f014f7cdab29b33. * e2e/timelines.tsで個々のテストに対するtimeoutを削除, maxConcurrency: 32 * better error handling of this.userPublickeysRepository.delete * better comment * set result to keypairEntityCache * deliverJobConcurrency: 16, deliverJobPerSec: 1024, inboxJobConcurrency: 4 * inboxJobPerSec: 64 * delete request.headers['host']; * fix * // node-fetch will generate this for us. if we keep 'Host', it won't change with redirects! * move delete host * modify comment * modify comment * fix correct → collect * refreshAndfindKey → refreshAndFindKey * modify comment * modify attachLdSignature * getApId, InboxProcessorService * TODO * [skip ci] add CHANGELOG --------- Co-authored-by: MeiMei <30769358+mei23@users.noreply.github.com> Co-authored-by: まっちゃとーにゅ <17376330+u1-liquid@users.noreply.github.com>
389 lines
10 KiB
TypeScript
389 lines
10 KiB
TypeScript
/*
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
import * as Redis from 'ioredis';
|
|
import { bindThis } from '@/decorators.js';
|
|
|
|
export class RedisKVCache<T> {
|
|
private redisClient: Redis.Redis;
|
|
private name: string;
|
|
private lifetime: number;
|
|
private memoryCache: MemoryKVCache<T>;
|
|
private fetcher: (key: string) => Promise<T>;
|
|
private toRedisConverter: (value: T) => string;
|
|
private fromRedisConverter: (value: string) => T | undefined;
|
|
|
|
constructor(redisClient: RedisKVCache<T>['redisClient'], name: RedisKVCache<T>['name'], opts: {
|
|
lifetime: RedisKVCache<T>['lifetime'];
|
|
memoryCacheLifetime: number;
|
|
fetcher: RedisKVCache<T>['fetcher'];
|
|
toRedisConverter: RedisKVCache<T>['toRedisConverter'];
|
|
fromRedisConverter: RedisKVCache<T>['fromRedisConverter'];
|
|
}) {
|
|
this.redisClient = redisClient;
|
|
this.name = name;
|
|
this.lifetime = opts.lifetime;
|
|
this.memoryCache = new MemoryKVCache(opts.memoryCacheLifetime);
|
|
this.fetcher = opts.fetcher;
|
|
this.toRedisConverter = opts.toRedisConverter;
|
|
this.fromRedisConverter = opts.fromRedisConverter;
|
|
}
|
|
|
|
@bindThis
|
|
public async set(key: string, value: T): Promise<void> {
|
|
this.memoryCache.set(key, value);
|
|
if (this.lifetime === Infinity) {
|
|
await this.redisClient.set(
|
|
`kvcache:${this.name}:${key}`,
|
|
this.toRedisConverter(value),
|
|
);
|
|
} else {
|
|
await this.redisClient.set(
|
|
`kvcache:${this.name}:${key}`,
|
|
this.toRedisConverter(value),
|
|
'EX', Math.round(this.lifetime / 1000),
|
|
);
|
|
}
|
|
}
|
|
|
|
@bindThis
|
|
public async get(key: string): Promise<T | undefined> {
|
|
const memoryCached = this.memoryCache.get(key);
|
|
if (memoryCached !== undefined) return memoryCached;
|
|
|
|
const cached = await this.redisClient.get(`kvcache:${this.name}:${key}`);
|
|
if (cached == null) return undefined;
|
|
return this.fromRedisConverter(cached);
|
|
}
|
|
|
|
@bindThis
|
|
public async delete(key: string): Promise<void> {
|
|
this.memoryCache.delete(key);
|
|
await this.redisClient.del(`kvcache:${this.name}:${key}`);
|
|
}
|
|
|
|
/**
|
|
* キャッシュがあればそれを返し、無ければfetcherを呼び出して結果をキャッシュ&返します
|
|
*/
|
|
@bindThis
|
|
public async fetch(key: string): Promise<T> {
|
|
const cachedValue = await this.get(key);
|
|
if (cachedValue !== undefined) {
|
|
// Cache HIT
|
|
return cachedValue;
|
|
}
|
|
|
|
// Cache MISS
|
|
const value = await this.fetcher(key);
|
|
this.set(key, value);
|
|
return value;
|
|
}
|
|
|
|
@bindThis
|
|
public async refresh(key: string) {
|
|
const value = await this.fetcher(key);
|
|
this.set(key, value);
|
|
|
|
// TODO: イベント発行して他プロセスのメモリキャッシュも更新できるようにする
|
|
}
|
|
|
|
@bindThis
|
|
public gc() {
|
|
this.memoryCache.gc();
|
|
}
|
|
|
|
@bindThis
|
|
public dispose() {
|
|
this.memoryCache.dispose();
|
|
}
|
|
}
|
|
|
|
export class RedisSingleCache<T> {
|
|
private redisClient: Redis.Redis;
|
|
private name: string;
|
|
private lifetime: number;
|
|
private memoryCache: MemorySingleCache<T>;
|
|
private fetcher: () => Promise<T>;
|
|
private toRedisConverter: (value: T) => string;
|
|
private fromRedisConverter: (value: string) => T | undefined;
|
|
|
|
constructor(redisClient: RedisSingleCache<T>['redisClient'], name: RedisSingleCache<T>['name'], opts: {
|
|
lifetime: RedisSingleCache<T>['lifetime'];
|
|
memoryCacheLifetime: number;
|
|
fetcher: RedisSingleCache<T>['fetcher'];
|
|
toRedisConverter: RedisSingleCache<T>['toRedisConverter'];
|
|
fromRedisConverter: RedisSingleCache<T>['fromRedisConverter'];
|
|
}) {
|
|
this.redisClient = redisClient;
|
|
this.name = name;
|
|
this.lifetime = opts.lifetime;
|
|
this.memoryCache = new MemorySingleCache(opts.memoryCacheLifetime);
|
|
this.fetcher = opts.fetcher;
|
|
this.toRedisConverter = opts.toRedisConverter;
|
|
this.fromRedisConverter = opts.fromRedisConverter;
|
|
}
|
|
|
|
@bindThis
|
|
public async set(value: T): Promise<void> {
|
|
this.memoryCache.set(value);
|
|
if (this.lifetime === Infinity) {
|
|
await this.redisClient.set(
|
|
`singlecache:${this.name}`,
|
|
this.toRedisConverter(value),
|
|
);
|
|
} else {
|
|
await this.redisClient.set(
|
|
`singlecache:${this.name}`,
|
|
this.toRedisConverter(value),
|
|
'EX', Math.round(this.lifetime / 1000),
|
|
);
|
|
}
|
|
}
|
|
|
|
@bindThis
|
|
public async get(): Promise<T | undefined> {
|
|
const memoryCached = this.memoryCache.get();
|
|
if (memoryCached !== undefined) return memoryCached;
|
|
|
|
const cached = await this.redisClient.get(`singlecache:${this.name}`);
|
|
if (cached == null) return undefined;
|
|
return this.fromRedisConverter(cached);
|
|
}
|
|
|
|
@bindThis
|
|
public async delete(): Promise<void> {
|
|
this.memoryCache.delete();
|
|
await this.redisClient.del(`singlecache:${this.name}`);
|
|
}
|
|
|
|
/**
|
|
* キャッシュがあればそれを返し、無ければfetcherを呼び出して結果をキャッシュ&返します
|
|
*/
|
|
@bindThis
|
|
public async fetch(): Promise<T> {
|
|
const cachedValue = await this.get();
|
|
if (cachedValue !== undefined) {
|
|
// Cache HIT
|
|
return cachedValue;
|
|
}
|
|
|
|
// Cache MISS
|
|
const value = await this.fetcher();
|
|
this.set(value);
|
|
return value;
|
|
}
|
|
|
|
@bindThis
|
|
public async refresh() {
|
|
const value = await this.fetcher();
|
|
this.set(value);
|
|
|
|
// TODO: イベント発行して他プロセスのメモリキャッシュも更新できるようにする
|
|
}
|
|
}
|
|
|
|
// TODO: メモリ節約のためあまり参照されないキーを定期的に削除できるようにする?
|
|
|
|
export class MemoryKVCache<T> {
|
|
/**
|
|
* データを持つマップ
|
|
* @deprecated これを直接操作するべきではない
|
|
*/
|
|
public cache: Map<string, { date: number; value: T; }>;
|
|
private lifetime: number;
|
|
private gcIntervalHandle: NodeJS.Timeout;
|
|
|
|
/**
|
|
* @param lifetime キャッシュの生存期間 (ms)
|
|
*/
|
|
constructor(lifetime: MemoryKVCache<never>['lifetime']) {
|
|
this.cache = new Map();
|
|
this.lifetime = lifetime;
|
|
|
|
this.gcIntervalHandle = setInterval(() => {
|
|
this.gc();
|
|
}, 1000 * 60 * 3);
|
|
}
|
|
|
|
@bindThis
|
|
/**
|
|
* Mapにキャッシュをセットします
|
|
* @deprecated これを直接呼び出すべきではない。InternalEventなどで変更を全てのプロセス/マシンに通知するべき
|
|
*/
|
|
public set(key: string, value: T): void {
|
|
this.cache.set(key, {
|
|
date: Date.now(),
|
|
value,
|
|
});
|
|
}
|
|
|
|
@bindThis
|
|
public get(key: string): T | undefined {
|
|
const cached = this.cache.get(key);
|
|
if (cached == null) return undefined;
|
|
if ((Date.now() - cached.date) > this.lifetime) {
|
|
this.cache.delete(key);
|
|
return undefined;
|
|
}
|
|
return cached.value;
|
|
}
|
|
|
|
@bindThis
|
|
public delete(key: string): void {
|
|
this.cache.delete(key);
|
|
}
|
|
|
|
/**
|
|
* キャッシュがあればそれを返し、無ければfetcherを呼び出して結果をキャッシュ&返します
|
|
* optional: キャッシュが存在してもvalidatorでfalseを返すとキャッシュ無効扱いにします
|
|
*/
|
|
@bindThis
|
|
public async fetch(key: string, fetcher: () => Promise<T>, validator?: (cachedValue: T) => boolean): Promise<T> {
|
|
const cachedValue = this.get(key);
|
|
if (cachedValue !== undefined) {
|
|
if (validator) {
|
|
if (validator(cachedValue)) {
|
|
// Cache HIT
|
|
return cachedValue;
|
|
}
|
|
} else {
|
|
// Cache HIT
|
|
return cachedValue;
|
|
}
|
|
}
|
|
|
|
// Cache MISS
|
|
const value = await fetcher();
|
|
this.set(key, value);
|
|
return value;
|
|
}
|
|
|
|
/**
|
|
* キャッシュがあればそれを返し、無ければfetcherを呼び出して結果をキャッシュ&返します
|
|
* optional: キャッシュが存在してもvalidatorでfalseを返すとキャッシュ無効扱いにします
|
|
*/
|
|
@bindThis
|
|
public async fetchMaybe(key: string, fetcher: () => Promise<T | undefined>, validator?: (cachedValue: T) => boolean): Promise<T | undefined> {
|
|
const cachedValue = this.get(key);
|
|
if (cachedValue !== undefined) {
|
|
if (validator) {
|
|
if (validator(cachedValue)) {
|
|
// Cache HIT
|
|
return cachedValue;
|
|
}
|
|
} else {
|
|
// Cache HIT
|
|
return cachedValue;
|
|
}
|
|
}
|
|
|
|
// Cache MISS
|
|
const value = await fetcher();
|
|
if (value !== undefined) {
|
|
this.set(key, value);
|
|
}
|
|
return value;
|
|
}
|
|
|
|
@bindThis
|
|
public gc(): void {
|
|
const now = Date.now();
|
|
for (const [key, { date }] of this.cache.entries()) {
|
|
if ((now - date) > this.lifetime) {
|
|
this.cache.delete(key);
|
|
}
|
|
}
|
|
}
|
|
|
|
@bindThis
|
|
public dispose(): void {
|
|
clearInterval(this.gcIntervalHandle);
|
|
}
|
|
}
|
|
|
|
export class MemorySingleCache<T> {
|
|
private cachedAt: number | null = null;
|
|
private value: T | undefined;
|
|
private lifetime: number;
|
|
|
|
constructor(lifetime: MemorySingleCache<never>['lifetime']) {
|
|
this.lifetime = lifetime;
|
|
}
|
|
|
|
@bindThis
|
|
public set(value: T): void {
|
|
this.cachedAt = Date.now();
|
|
this.value = value;
|
|
}
|
|
|
|
@bindThis
|
|
public get(): T | undefined {
|
|
if (this.cachedAt == null) return undefined;
|
|
if ((Date.now() - this.cachedAt) > this.lifetime) {
|
|
this.value = undefined;
|
|
this.cachedAt = null;
|
|
return undefined;
|
|
}
|
|
return this.value;
|
|
}
|
|
|
|
@bindThis
|
|
public delete() {
|
|
this.value = undefined;
|
|
this.cachedAt = null;
|
|
}
|
|
|
|
/**
|
|
* キャッシュがあればそれを返し、無ければfetcherを呼び出して結果をキャッシュ&返します
|
|
* optional: キャッシュが存在してもvalidatorでfalseを返すとキャッシュ無効扱いにします
|
|
*/
|
|
@bindThis
|
|
public async fetch(fetcher: () => Promise<T>, validator?: (cachedValue: T) => boolean): Promise<T> {
|
|
const cachedValue = this.get();
|
|
if (cachedValue !== undefined) {
|
|
if (validator) {
|
|
if (validator(cachedValue)) {
|
|
// Cache HIT
|
|
return cachedValue;
|
|
}
|
|
} else {
|
|
// Cache HIT
|
|
return cachedValue;
|
|
}
|
|
}
|
|
|
|
// Cache MISS
|
|
const value = await fetcher();
|
|
this.set(value);
|
|
return value;
|
|
}
|
|
|
|
/**
|
|
* キャッシュがあればそれを返し、無ければfetcherを呼び出して結果をキャッシュ&返します
|
|
* optional: キャッシュが存在してもvalidatorでfalseを返すとキャッシュ無効扱いにします
|
|
*/
|
|
@bindThis
|
|
public async fetchMaybe(fetcher: () => Promise<T | undefined>, validator?: (cachedValue: T) => boolean): Promise<T | undefined> {
|
|
const cachedValue = this.get();
|
|
if (cachedValue !== undefined) {
|
|
if (validator) {
|
|
if (validator(cachedValue)) {
|
|
// Cache HIT
|
|
return cachedValue;
|
|
}
|
|
} else {
|
|
// Cache HIT
|
|
return cachedValue;
|
|
}
|
|
}
|
|
|
|
// Cache MISS
|
|
const value = await fetcher();
|
|
if (value !== undefined) {
|
|
this.set(value);
|
|
}
|
|
return value;
|
|
}
|
|
}
|