Skip to content

Commit

Permalink
fix(backend): キャッシュが溜まり続けないように
Browse files Browse the repository at this point in the history
Related #10984
  • Loading branch information
syuilo committed Jun 10, 2023
1 parent 6182a1c commit e8420ad
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- エラー時や項目が存在しないときなどのアイコン画像をサーバー管理者が設定できるようになりました

### Server
- Fix: キャッシュが溜まり続けないように
- Fix: api/metaで`TypeError: JSON5.parse is not a function`エラーが発生する問題を修正

## 13.13.0
Expand Down
11 changes: 11 additions & 0 deletions packages/backend/src/core/CacheService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,17 @@ export class CacheService implements OnApplicationShutdown {
@bindThis
public dispose(): void {
this.redisForSub.off('message', this.onMessage);
this.userByIdCache.dispose();
this.localUserByNativeTokenCache.dispose();
this.localUserByIdCache.dispose();
this.uriPersonCache.dispose();
this.userProfileCache.dispose();
this.userMutingsCache.dispose();
this.userBlockingCache.dispose();
this.userBlockedCache.dispose();
this.renoteMutingsCache.dispose();
this.userFollowingsCache.dispose();
this.userFollowingChannelsCache.dispose();
}

@bindThis
Expand Down
14 changes: 12 additions & 2 deletions packages/backend/src/core/CustomEmojiService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Inject, Injectable } from '@nestjs/common';
import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
import { DataSource, In, IsNull } from 'typeorm';
import * as Redis from 'ioredis';
import { DI } from '@/di-symbols.js';
Expand All @@ -18,7 +18,7 @@ import type { Serialized } from '@/server/api/stream/types.js';
const parseEmojiStrRegexp = /^(\w+)(?:@([\w.-]+))?$/;

@Injectable()
export class CustomEmojiService {
export class CustomEmojiService implements OnApplicationShutdown {
private cache: MemoryKVCache<Emoji | null>;
public localEmojisCache: RedisSingleCache<Map<string, Emoji>>;

Expand Down Expand Up @@ -349,4 +349,14 @@ export class CustomEmojiService {
this.cache.set(`${emoji.name} ${emoji.host}`, emoji);
}
}

@bindThis
public dispose(): void {
this.cache.dispose();
}

@bindThis
public onApplicationShutdown(signal?: string | undefined): void {
this.dispose();
}
}
14 changes: 12 additions & 2 deletions packages/backend/src/core/FederatedInstanceService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Inject, Injectable } from '@nestjs/common';
import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
import * as Redis from 'ioredis';
import type { InstancesRepository } from '@/models/index.js';
import type { Instance } from '@/models/entities/Instance.js';
Expand All @@ -9,7 +9,7 @@ import { UtilityService } from '@/core/UtilityService.js';
import { bindThis } from '@/decorators.js';

@Injectable()
export class FederatedInstanceService {
export class FederatedInstanceService implements OnApplicationShutdown {
public federatedInstanceCache: RedisKVCache<Instance | null>;

constructor(
Expand Down Expand Up @@ -77,4 +77,14 @@ export class FederatedInstanceService {

this.federatedInstanceCache.set(result.host, result);
}

@bindThis
public dispose(): void {
this.federatedInstanceCache.dispose();
}

@bindThis
public onApplicationShutdown(signal?: string | undefined): void {
this.dispose();
}
}
14 changes: 12 additions & 2 deletions packages/backend/src/core/PushNotificationService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Inject, Injectable } from '@nestjs/common';
import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
import push from 'web-push';
import * as Redis from 'ioredis';
import { DI } from '@/di-symbols.js';
Expand Down Expand Up @@ -42,7 +42,7 @@ function truncateBody<T extends keyof PushNotificationsTypes>(type: T, body: Pus
}

@Injectable()
export class PushNotificationService {
export class PushNotificationService implements OnApplicationShutdown {
private subscriptionsCache: RedisKVCache<SwSubscription[]>;

constructor(
Expand Down Expand Up @@ -115,4 +115,14 @@ export class PushNotificationService {
});
}
}

@bindThis
public dispose(): void {
this.subscriptionsCache.dispose();
}

@bindThis
public onApplicationShutdown(signal?: string | undefined): void {
this.dispose();
}
}
1 change: 1 addition & 0 deletions packages/backend/src/core/RoleService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ export class RoleService implements OnApplicationShutdown {
@bindThis
public dispose(): void {
this.redisForSub.off('message', this.onMessage);
this.roleAssignmentByUserIdCache.dispose();
}

@bindThis
Expand Down
14 changes: 12 additions & 2 deletions packages/backend/src/core/UserKeypairService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Inject, Injectable } from '@nestjs/common';
import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
import * as Redis from 'ioredis';
import type { User } from '@/models/entities/User.js';
import type { UserKeypairsRepository } from '@/models/index.js';
Expand All @@ -8,7 +8,7 @@ import { DI } from '@/di-symbols.js';
import { bindThis } from '@/decorators.js';

@Injectable()
export class UserKeypairService {
export class UserKeypairService implements OnApplicationShutdown {
private cache: RedisKVCache<UserKeypair>;

constructor(
Expand All @@ -31,4 +31,14 @@ export class UserKeypairService {
public async getUserKeypair(userId: User['id']): Promise<UserKeypair> {
return await this.cache.fetch(userId);
}

@bindThis
public dispose(): void {
this.cache.dispose();
}

@bindThis
public onApplicationShutdown(signal?: string | undefined): void {
this.dispose();
}
}
15 changes: 13 additions & 2 deletions packages/backend/src/core/activitypub/ApDbResolverService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Inject, Injectable } from '@nestjs/common';
import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
import escapeRegexp from 'escape-regexp';
import { DI } from '@/di-symbols.js';
import type { NotesRepository, UserPublickeysRepository, UsersRepository } from '@/models/index.js';
Expand Down Expand Up @@ -30,7 +30,7 @@ export type UriParseResult = {
};

@Injectable()
export class ApDbResolverService {
export class ApDbResolverService implements OnApplicationShutdown {
private publicKeyCache: MemoryKVCache<UserPublickey | null>;
private publicKeyByUserIdCache: MemoryKVCache<UserPublickey | null>;

Expand Down Expand Up @@ -162,4 +162,15 @@ export class ApDbResolverService {
key,
};
}

@bindThis
public dispose(): void {
this.publicKeyCache.dispose();
this.publicKeyByUserIdCache.dispose();
}

@bindThis
public onApplicationShutdown(signal?: string | undefined): void {
this.dispose();
}
}
32 changes: 31 additions & 1 deletion packages/backend/src/misc/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ export class RedisKVCache<T> {

// TODO: イベント発行して他プロセスのメモリキャッシュも更新できるようにする
}

@bindThis
public gc() {
this.memoryCache.gc();
}

@bindThis
public dispose() {
this.memoryCache.dispose();
}
}

export class RedisSingleCache<T> {
Expand Down Expand Up @@ -174,10 +184,15 @@ export class RedisSingleCache<T> {
export class MemoryKVCache<T> {
public cache: Map<string, { date: number; value: T; }>;
private lifetime: number;
private gcIntervalHandle: NodeJS.Timer;

constructor(lifetime: MemoryKVCache<never>['lifetime']) {
this.cache = new Map();
this.lifetime = lifetime;

this.gcIntervalHandle = setInterval(() => {
this.gc();
}, 1000 * 60 * 3);
}

@bindThis
Expand All @@ -200,7 +215,7 @@ export class MemoryKVCache<T> {
}

@bindThis
public delete(key: string) {
public delete(key: string): void {
this.cache.delete(key);
}

Expand Down Expand Up @@ -255,6 +270,21 @@ export class MemoryKVCache<T> {
}
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> {
Expand Down
14 changes: 12 additions & 2 deletions packages/backend/src/server/api/AuthenticateService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Inject, Injectable } from '@nestjs/common';
import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
import { DI } from '@/di-symbols.js';
import type { AccessTokensRepository, AppsRepository, UsersRepository } from '@/models/index.js';
import type { LocalUser } from '@/models/entities/User.js';
Expand All @@ -17,7 +17,7 @@ export class AuthenticationError extends Error {
}

@Injectable()
export class AuthenticateService {
export class AuthenticateService implements OnApplicationShutdown {
private appCache: MemoryKVCache<App>;

constructor(
Expand Down Expand Up @@ -85,4 +85,14 @@ export class AuthenticateService {
}
}
}

@bindThis
public dispose(): void {
this.appCache.dispose();
}

@bindThis
public onApplicationShutdown(signal?: string | undefined): void {
this.dispose();
}
}

0 comments on commit e8420ad

Please sign in to comment.