Skip to content

Commit

Permalink
Merge branch 'develop' into use-interval
Browse files Browse the repository at this point in the history
  • Loading branch information
tamaina authored Jul 4, 2023
2 parents edb06d3 + aa92df4 commit a417fc5
Show file tree
Hide file tree
Showing 20 changed files with 164 additions and 38 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,16 @@

## 13.x.x (unreleased)

### General
- identicon生成を無効にしてパフォーマンスを向上させることができるようになりました
- サーバーのマシン情報の公開を無効にしてパフォーマンスを向上させることができるようになりました

### Client
- Fix: サーバーメトリクスが90度傾いている
- Fix: sparkle内にリンクを入れるとクリック不能になる問題の修正

### Server
- JSON.parse の回数を削減することで、ストリーミングのパフォーマンスを向上しました

## 13.13.2

Expand Down
2 changes: 2 additions & 0 deletions locales/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,8 @@ export interface Locale {
"additionalEmojiDictionary": string;
"installed": string;
"branding": string;
"enableServerMachineStats": string;
"enableIdenticonGeneration": string;
"_initialAccountSetting": {
"accountCreated": string;
"letsStartAccountSetup": string;
Expand Down
2 changes: 2 additions & 0 deletions locales/ja-JP.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,8 @@ goToMisskey: "Misskeyへ"
additionalEmojiDictionary: "絵文字の追加辞書"
installed: "インストール済み"
branding: "ブランディング"
enableServerMachineStats: "サーバーのマシン情報を公開する"
enableIdenticonGeneration: "ユーザーごとのIdenticon生成を有効にする"

_initialAccountSetting:
accountCreated: "アカウントの作成が完了しました!"
Expand Down
Binary file added packages/backend/assets/avatar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions packages/backend/migration/1688280713783-add-meta-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export class AddMetaOptions1688280713783 {
name = 'AddMetaOptions1688280713783'

async up(queryRunner) {
await queryRunner.query(`ALTER TABLE "meta" ADD "enableServerMachineStats" boolean NOT NULL DEFAULT false`);
await queryRunner.query(`ALTER TABLE "meta" ADD "enableIdenticonGeneration" boolean NOT NULL DEFAULT true`);
}

async down(queryRunner) {
await queryRunner.query(`ALTER TABLE "meta" DROP COLUMN "enableIdenticonGeneration"`);
await queryRunner.query(`ALTER TABLE "meta" DROP COLUMN "enableServerMachineStats"`);
}
}
12 changes: 9 additions & 3 deletions packages/backend/src/daemons/ServerStatsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import si from 'systeminformation';
import Xev from 'xev';
import * as osUtils from 'os-utils';
import { bindThis } from '@/decorators.js';
import { MetaService } from '@/core/MetaService.js';
import type { OnApplicationShutdown } from '@nestjs/common';

const ev = new Xev();
Expand All @@ -14,17 +15,20 @@ const round = (num: number) => Math.round(num * 10) / 10;

@Injectable()
export class ServerStatsService implements OnApplicationShutdown {
private intervalId: NodeJS.Timer;
private intervalId: NodeJS.Timer | null = null;

constructor(
private metaService: MetaService,
) {
}

/**
* Report server stats regularly
*/
@bindThis
public start(): void {
public async start(): Promise<void> {
if (!(await this.metaService.fetch(true)).enableServerMachineStats) return;

const log = [] as any[];

ev.on('requestServerStatsLog', x => {
Expand Down Expand Up @@ -64,7 +68,9 @@ export class ServerStatsService implements OnApplicationShutdown {

@bindThis
public dispose(): void {
clearInterval(this.intervalId);
if (this.intervalId) {
clearInterval(this.intervalId);
}
}

@bindThis
Expand Down
10 changes: 10 additions & 0 deletions packages/backend/src/models/entities/Meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,16 @@ export class Meta {
})
public enableChartsForFederatedInstances: boolean;

@Column('boolean', {
default: false,
})
public enableServerMachineStats: boolean;

@Column('boolean', {
default: true,
})
public enableIdenticonGeneration: boolean;

@Column('jsonb', {
default: { },
})
Expand Down
15 changes: 11 additions & 4 deletions packages/backend/src/server/ServerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { createTemp } from '@/misc/create-temp.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js';
import { LoggerService } from '@/core/LoggerService.js';
import { bindThis } from '@/decorators.js';
import { MetaService } from '@/core/MetaService.js';
import { ActivityPubServerService } from './ActivityPubServerService.js';
import { NodeinfoServerService } from './NodeinfoServerService.js';
import { ApiServerService } from './api/ApiServerService.js';
Expand Down Expand Up @@ -45,6 +46,7 @@ export class ServerService implements OnApplicationShutdown {
@Inject(DI.emojisRepository)
private emojisRepository: EmojisRepository,

private metaService: MetaService,
private userEntityService: UserEntityService,
private apiServerService: ApiServerService,
private openApiServerService: OpenApiServerService,
Expand Down Expand Up @@ -161,11 +163,16 @@ export class ServerService implements OnApplicationShutdown {
});

fastify.get<{ Params: { x: string } }>('/identicon/:x', async (request, reply) => {
const [temp, cleanup] = await createTemp();
await genIdenticon(request.params.x, fs.createWriteStream(temp));
reply.header('Content-Type', 'image/png');
reply.header('Cache-Control', 'public, max-age=86400');
return fs.createReadStream(temp).on('close', () => cleanup());

if ((await this.metaService.fetch()).enableIdenticonGeneration) {
const [temp, cleanup] = await createTemp();
await genIdenticon(request.params.x, fs.createWriteStream(temp));
return fs.createReadStream(temp).on('close', () => cleanup());
} else {
return reply.redirect('/static-assets/avatar.png');
}
});

fastify.get<{ Params: { code: string } }>('/verify-email/:code', async (request, reply) => {
Expand Down Expand Up @@ -224,7 +231,7 @@ export class ServerService implements OnApplicationShutdown {

@bindThis
public async dispose(): Promise<void> {
await this.streamingApiServerService.detach();
await this.streamingApiServerService.detach();
await this.#fastify.close();
}

Expand Down
16 changes: 11 additions & 5 deletions packages/backend/src/server/api/StreamingApiServerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ export class StreamingApiServerService {
});
});

const globalEv = new EventEmitter();

this.redisForSub.on('message', (_: string, data: string) => {
const parsed = JSON.parse(data);
globalEv.emit('message', parsed);
});

this.#wss.on('connection', async (connection: WebSocket.WebSocket, request: http.IncomingMessage, ctx: {
stream: MainStreamConnection,
user: LocalUser | null;
Expand All @@ -112,12 +119,11 @@ export class StreamingApiServerService {

const ev = new EventEmitter();

async function onRedisMessage(_: string, data: string): Promise<void> {
const parsed = JSON.parse(data);
ev.emit(parsed.channel, parsed.message);
function onRedisMessage(data: any): void {
ev.emit(data.channel, data.message);
}

this.redisForSub.on('message', onRedisMessage);
globalEv.on('message', onRedisMessage);

await stream.listen(ev, connection);

Expand All @@ -137,7 +143,7 @@ export class StreamingApiServerService {
connection.once('close', () => {
ev.removeAllListeners();
stream.dispose();
this.redisForSub.off('message', onRedisMessage);
globalEv.off('message', onRedisMessage);
this.#connections.delete(connection);
if (userUpdateIntervalId) clearInterval(userUpdateIntervalId);
});
Expand Down
10 changes: 10 additions & 0 deletions packages/backend/src/server/api/endpoints/admin/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,14 @@ export const meta = {
type: 'boolean',
optional: false, nullable: false,
},
enableServerMachineStats: {
type: 'boolean',
optional: false, nullable: false,
},
enableIdenticonGeneration: {
type: 'boolean',
optional: false, nullable: false,
},
policies: {
type: 'object',
optional: false, nullable: false,
Expand Down Expand Up @@ -364,6 +372,8 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
enableActiveEmailValidation: instance.enableActiveEmailValidation,
enableChartsForRemoteUser: instance.enableChartsForRemoteUser,
enableChartsForFederatedInstances: instance.enableChartsForFederatedInstances,
enableServerMachineStats: instance.enableServerMachineStats,
enableIdenticonGeneration: instance.enableIdenticonGeneration,
policies: { ...DEFAULT_POLICIES, ...instance.policies },
};
});
Expand Down
10 changes: 10 additions & 0 deletions packages/backend/src/server/api/endpoints/admin/update-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ export const paramDef = {
enableActiveEmailValidation: { type: 'boolean' },
enableChartsForRemoteUser: { type: 'boolean' },
enableChartsForFederatedInstances: { type: 'boolean' },
enableServerMachineStats: { type: 'boolean' },
enableIdenticonGeneration: { type: 'boolean' },
serverRules: { type: 'array', items: { type: 'string' } },
preservedUsernames: { type: 'array', items: { type: 'string' } },
},
Expand Down Expand Up @@ -399,6 +401,14 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
set.enableChartsForFederatedInstances = ps.enableChartsForFederatedInstances;
}

if (ps.enableServerMachineStats !== undefined) {
set.enableServerMachineStats = ps.enableServerMachineStats;
}

if (ps.enableIdenticonGeneration !== undefined) {
set.enableIdenticonGeneration = ps.enableIdenticonGeneration;
}

if (ps.serverRules !== undefined) {
set.serverRules = ps.serverRules;
}
Expand Down
19 changes: 19 additions & 0 deletions packages/backend/src/server/api/endpoints/server-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import * as os from 'node:os';
import si from 'systeminformation';
import { Injectable } from '@nestjs/common';
import { Endpoint } from '@/server/api/endpoint-base.js';
import { MetaService } from '@/core/MetaService.js';

export const meta = {
requireCredential: false,
allowGet: true,
cacheSec: 60 * 1,

tags: ['meta'],
} as const;
Expand All @@ -19,8 +22,24 @@ export const paramDef = {
@Injectable()
export default class extends Endpoint<typeof meta, typeof paramDef> {
constructor(
private metaService: MetaService,
) {
super(meta, paramDef, async () => {
if (!(await this.metaService.fetch()).enableServerMachineStats) return {
machine: '?',
cpu: {
model: '?',
cores: 0,
},
mem: {
total: 0,
},
fs: {
total: 0,
used: 0,
},
};

const memStats = await si.mem();
const fsStats = await si.fsSize();

Expand Down
51 changes: 32 additions & 19 deletions packages/frontend/src/components/MkImgWithBlurhash.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ import TestWebGL2 from '@/workers/test-webgl2?worker';
import { WorkerMultiDispatch } from '@/scripts/worker-multi-dispatch';
import { extractAvgColorFromBlurhash } from '@/scripts/extract-avg-color-from-blurhash';
const workerPromise = new Promise<WorkerMultiDispatch | null>(resolve => {
const canvasPromise = new Promise<WorkerMultiDispatch | HTMLCanvasElement>(resolve => {
// テスト環境で Web Worker インスタンスは作成できない
if (import.meta.env.MODE === 'test') {
resolve(null);
const canvas = document.createElement('canvas');
canvas.width = 64;
canvas.height = 64;
resolve(canvas);
return;
}
const testWorker = new TestWebGL2();
Expand All @@ -38,7 +41,10 @@ const workerPromise = new Promise<WorkerMultiDispatch | null>(resolve => {
resolve(workers);
if (_DEV_) console.log('WebGL2 in worker is supported!');
} else {
resolve(null);
const canvas = document.createElement('canvas');
canvas.width = 64;
canvas.height = 64;
resolve(canvas);
if (_DEV_) console.log('WebGL2 in worker is not supported...');
}
testWorker.terminate();
Expand Down Expand Up @@ -70,6 +76,7 @@ const props = withDefaults(defineProps<{
width?: number;
cover?: boolean;
forceBlurhash?: boolean;
onlyAvgColor?: boolean; // 軽量化のためにBlurhashを使わずに平均色だけを描画
}>(), {
transition: null,
src: null,
Expand All @@ -79,6 +86,7 @@ const props = withDefaults(defineProps<{
width: 64,
cover: true,
forceBlurhash: false,
onlyAvgColor: false,
});
const viewId = uuid();
Expand Down Expand Up @@ -139,8 +147,8 @@ function drawImage(bitmap: CanvasImageSource) {
ctx.drawImage(bitmap, 0, 0, canvasWidth, canvasHeight);
}
async function draw() {
if (!canvas.value || props.hash == null) return;
function drawAvg() {
if (!canvas.value || !props.hash) return;
const ctx = canvas.value.getContext('2d');
if (!ctx) return;
Expand All @@ -149,25 +157,28 @@ async function draw() {
ctx.beginPath();
ctx.fillStyle = extractAvgColorFromBlurhash(props.hash) ?? '#888';
ctx.fillRect(0, 0, canvasWidth, canvasHeight);
}
async function draw() {
if (props.hash == null) return;
const workers = await workerPromise;
if (workers) {
workers.postMessage(
drawAvg();
if (props.onlyAvgColor) return;
const work = await canvasPromise;
if (work instanceof WorkerMultiDispatch) {
work.postMessage(
{
id: viewId,
hash: props.hash,
width: canvasWidth,
height: canvasHeight,
},
undefined,
);
} else {
try {
const work = document.createElement('canvas');
work.width = canvasWidth;
work.height = canvasHeight;
render(props.hash, work);
ctx.drawImage(work, 0, 0, canvasWidth, canvasHeight);
drawImage(work);
} catch (error) {
console.error('Error occured during drawing blurhash', error);
}
Expand All @@ -179,9 +190,9 @@ function workerOnMessage(event: MessageEvent) {
drawImage(event.data.bitmap as ImageBitmap);
}
workerPromise.then(worker => {
if (worker) {
worker.addListener(workerOnMessage);
canvasPromise.then(work => {
if (work instanceof WorkerMultiDispatch) {
work.addListener(workerOnMessage);
}
draw();
Expand All @@ -204,8 +215,10 @@ onMounted(() => {
});
onUnmounted(() => {
workerPromise.then(worker => {
worker?.removeListener(workerOnMessage);
canvasPromise.then(work => {
if (work instanceof WorkerMultiDispatch) {
work.removeListener(workerOnMessage);
}
});
});
</script>
Expand Down
Loading

0 comments on commit a417fc5

Please sign in to comment.