mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-17 09:36:08 +08:00
core: 性能优化
This commit is contained in:
1 parent
34465ba80a
commit
ee4dc90cf3
6 files changed
+11
-43
No files matched your search
@@ -12,6 +12,7 @@ dist/
|
|||||||
.coverage
|
.coverage
|
||||||
globalConfig.json
|
globalConfig.json
|
||||||
*.out
|
*.out
|
||||||
|
.clinic/
|
||||||
|
|
||||||
# Config Files
|
# Config Files
|
||||||
/config.yaml
|
/config.yaml
|
||||||
|
|||||||
@@ -25,7 +25,6 @@
|
|||||||
"koa-router": "^10.0.0",
|
"koa-router": "^10.0.0",
|
||||||
"koa-static-cache": "^5.1.3",
|
"koa-static-cache": "^5.1.3",
|
||||||
"lodash": "^4.17.21",
|
"lodash": "^4.17.21",
|
||||||
"lru-cache": "^6.0.0",
|
|
||||||
"minio": "7.0.17",
|
"minio": "7.0.17",
|
||||||
"moment-timezone": "^0.5.32",
|
"moment-timezone": "^0.5.32",
|
||||||
"mongodb": "^3.6.5",
|
"mongodb": "^3.6.5",
|
||||||
@@ -36,7 +35,6 @@
|
|||||||
"sockjs": "^0.3.20",
|
"sockjs": "^0.3.20",
|
||||||
"superagent": "^6.1.0",
|
"superagent": "^6.1.0",
|
||||||
"systeminformation": "5.6.8",
|
"systeminformation": "5.6.8",
|
||||||
"wtfnode": "^0.8.4",
|
|
||||||
"yargs": "^16.2.0"
|
"yargs": "^16.2.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
@@ -48,14 +46,12 @@
|
|||||||
"@types/koa-router": "^7.4.1",
|
"@types/koa-router": "^7.4.1",
|
||||||
"@types/koa-static-cache": "^5.1.0",
|
"@types/koa-static-cache": "^5.1.0",
|
||||||
"@types/lodash": "^4.14.168",
|
"@types/lodash": "^4.14.168",
|
||||||
"@types/lru-cache": "^5.1.0",
|
|
||||||
"@types/minio": "^7.0.7",
|
"@types/minio": "^7.0.7",
|
||||||
"@types/mongodb": "^3.6.10",
|
"@types/mongodb": "^3.6.10",
|
||||||
"@types/nodemailer": "^6.4.1",
|
"@types/nodemailer": "^6.4.1",
|
||||||
"@types/serialize-javascript": "^5.0.0",
|
"@types/serialize-javascript": "^5.0.0",
|
||||||
"@types/sockjs": "^0.3.32",
|
"@types/sockjs": "^0.3.32",
|
||||||
"@types/superagent": "^4.1.8",
|
"@types/superagent": "^4.1.8",
|
||||||
"@types/wtfnode": "^0.7.0",
|
|
||||||
"@types/yargs": "^16.0.0",
|
"@types/yargs": "^16.0.0",
|
||||||
"formidable": "^1.2.2"
|
"formidable": "^1.2.2"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -41,7 +41,6 @@ import path from 'path';
|
|||||||
import cluster from 'cluster';
|
import cluster from 'cluster';
|
||||||
import fs from 'fs-extra';
|
import fs from 'fs-extra';
|
||||||
import { argv } from 'yargs';
|
import { argv } from 'yargs';
|
||||||
import wtfnode from 'wtfnode';
|
|
||||||
import './utils';
|
import './utils';
|
||||||
import { Logger } from './logger';
|
import { Logger } from './logger';
|
||||||
import './ui';
|
import './ui';
|
||||||
@@ -50,14 +49,6 @@ import * as bus from './service/bus';
|
|||||||
export * from './interface';
|
export * from './interface';
|
||||||
const logger = new Logger('loader');
|
const logger = new Logger('loader');
|
||||||
logger.debug('%o', argv);
|
logger.debug('%o', argv);
|
||||||
bus.on('app/exit', () => {
|
|
||||||
if (argv.debug) {
|
|
||||||
wtfnode.setLogger('info', logger.info.bind(logger));
|
|
||||||
wtfnode.setLogger('warn', logger.warn.bind(logger));
|
|
||||||
wtfnode.setLogger('error', logger.error.bind(logger));
|
|
||||||
wtfnode.dump();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
async function fork(args: string[] = []) {
|
async function fork(args: string[] = []) {
|
||||||
const _args = process.argv.slice(2);
|
const _args = process.argv.slice(2);
|
||||||
@@ -100,8 +91,12 @@ async function stopWorker() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function startWorker(cnt: number, createFirst = true) {
|
async function startWorker(cnt: number, createFirst = true) {
|
||||||
await fork(createFirst ? ['--firstWorker'] : undefined);
|
if (argv.single) {
|
||||||
for (let i = 1; i < cnt; i++) await fork();
|
await entry({ entry: 'worker' });
|
||||||
|
} else {
|
||||||
|
await fork(createFirst ? ['--firstWorker'] : undefined);
|
||||||
|
for (let i = 1; i < cnt; i++) await fork();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function reload(count = 1) {
|
async function reload(count = 1) {
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import Lru from 'lru-cache';
|
|
||||||
import { Collection } from 'mongodb';
|
import { Collection } from 'mongodb';
|
||||||
import db from '../service/db';
|
import db from '../service/db';
|
||||||
|
|
||||||
@@ -8,21 +7,15 @@ interface OauthMap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const coll: Collection<OauthMap> = db.collection('oauth');
|
const coll: Collection<OauthMap> = db.collection('oauth');
|
||||||
const cache = new Lru({
|
|
||||||
maxAge: 5000,
|
|
||||||
});
|
|
||||||
|
|
||||||
class OauthModel {
|
class OauthModel {
|
||||||
static async get(_id: string) {
|
static async get(_id: string) {
|
||||||
const res = cache.get(_id);
|
|
||||||
if (res !== undefined) return res;
|
|
||||||
const doc = await coll.findOne({ _id });
|
const doc = await coll.findOne({ _id });
|
||||||
if (doc) return doc.uid;
|
if (doc) return doc.uid;
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
static async set(_id: string, value: number) {
|
static async set(_id: string, value: number) {
|
||||||
cache.set(_id, value);
|
|
||||||
const res = await coll.findOneAndUpdate(
|
const res = await coll.findOneAndUpdate(
|
||||||
{ _id },
|
{ _id },
|
||||||
{ $set: { value } },
|
{ $set: { value } },
|
||||||
|
|||||||
@@ -18,8 +18,9 @@ import sockjs from 'sockjs';
|
|||||||
import type { SetOption } from 'cookies';
|
import type { SetOption } from 'cookies';
|
||||||
import serialize, { SerializeJSOptions } from 'serialize-javascript';
|
import serialize, { SerializeJSOptions } from 'serialize-javascript';
|
||||||
import { argv } from 'yargs';
|
import { argv } from 'yargs';
|
||||||
|
import { createHash } from 'crypto';
|
||||||
import * as bus from './bus';
|
import * as bus from './bus';
|
||||||
import { errorMessage, lrucache } from '../utils';
|
import { errorMessage } from '../utils';
|
||||||
import { User, DomainDoc } from '../interface';
|
import { User, DomainDoc } from '../interface';
|
||||||
import { Logger } from '../logger';
|
import { Logger } from '../logger';
|
||||||
import {
|
import {
|
||||||
@@ -422,10 +423,9 @@ export class Handler extends HandlerCommon {
|
|||||||
if (xff) this.request.ip = this.request.headers[xff.toLowerCase()] || this.request.ip;
|
if (xff) this.request.ip = this.request.headers[xff.toLowerCase()] || this.request.ip;
|
||||||
}
|
}
|
||||||
|
|
||||||
@lrucache
|
|
||||||
// eslint-disable-next-line class-methods-use-this
|
// eslint-disable-next-line class-methods-use-this
|
||||||
getCsrfToken(id: string) {
|
getCsrfToken(id: string) {
|
||||||
return hash('csrf_token', id);
|
return createHash('md5').update(`csrf_token${id}`).digest('hex');
|
||||||
}
|
}
|
||||||
|
|
||||||
async render(name: string, context: any) {
|
async render(name: string, context: any) {
|
||||||
@@ -457,10 +457,10 @@ export class Handler extends HandlerCommon {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async init({ domainId }) {
|
async init({ domainId }) {
|
||||||
|
if (!argv.benchmark) await this.limitRate('global', 10, 50);
|
||||||
const [absoluteDomain, inferDomain] = await Promise.all([
|
const [absoluteDomain, inferDomain] = await Promise.all([
|
||||||
domain.get(domainId),
|
domain.get(domainId),
|
||||||
domain.getByHost(this.request.host),
|
domain.getByHost(this.request.host),
|
||||||
this.limitRate('global', 10, 50),
|
|
||||||
this.getSession(),
|
this.getSession(),
|
||||||
this.getBdoc(),
|
this.getBdoc(),
|
||||||
]);
|
]);
|
||||||
|
|||||||
@@ -2,8 +2,6 @@ import fs from 'fs';
|
|||||||
import { Duplex } from 'stream';
|
import { Duplex } from 'stream';
|
||||||
import cluster from 'cluster';
|
import cluster from 'cluster';
|
||||||
import path, { sep } from 'path';
|
import path, { sep } from 'path';
|
||||||
import serialize from 'serialize-javascript';
|
|
||||||
import Lru from 'lru-cache';
|
|
||||||
import { ObjectID } from 'mongodb';
|
import { ObjectID } from 'mongodb';
|
||||||
import { isMoment } from 'moment';
|
import { isMoment } from 'moment';
|
||||||
import type { Moment } from 'moment-timezone';
|
import type { Moment } from 'moment-timezone';
|
||||||
@@ -29,21 +27,6 @@ declare global {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const lru = new Lru(1000);
|
|
||||||
|
|
||||||
export function lrucache(target: any, funcName: string, obj: any) {
|
|
||||||
const originalMethod = obj.value;
|
|
||||||
obj.value = function func(...args: any[]) {
|
|
||||||
const key = serialize({ funcName, args });
|
|
||||||
let val = lru.get(key);
|
|
||||||
if (val) return val;
|
|
||||||
val = originalMethod.call(this, ...args);
|
|
||||||
lru.set(key, val);
|
|
||||||
return val;
|
|
||||||
};
|
|
||||||
return obj;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!cluster.worker) {
|
if (!cluster.worker) {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
cluster.worker = { id: 0 };
|
cluster.worker = { id: 0 };
|
||||||
|
|||||||
Reference in new issue
Block a user