feat: optimize event slow exec cache

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2022-11-28 17:14:29 +08:00
parent 597bdb721a
commit 72673b2a67
7 changed files with 38 additions and 20 deletions

View File

@ -13,9 +13,9 @@
"ug": "yarn upgrade-interactive --latest", "ug": "yarn upgrade-interactive --latest",
"np": "./script/push.sh", "np": "./script/push.sh",
"lsp": "npm login -scope=@ccms", "lsp": "npm login -scope=@ccms",
"lp": "lerna publish --verify-access", "lp": "lerna publish --verify-access --force-publish",
"lpb": "lerna publish --preid beta --dist-tag beta --verify-access", "lpb": "lerna publish --preid beta --dist-tag beta --verify-access --force-publish",
"lpc": "lerna publish --canary --preid beta --pre-dist-tag beta --verify-access", "lpc": "lerna publish --canary --preid beta --pre-dist-tag beta --verify-access --force-publish",
"lpf": "lerna publish from-package --yes" "lpf": "lerna publish from-package --yes"
}, },
"workspaces": [ "workspaces": [

View File

@ -26,6 +26,7 @@ export namespace event {
private mapEvent = []; private mapEvent = [];
private listenerMap = []; private listenerMap = [];
private cacheSlowEventKey = {};
protected baseEventDir = ''; protected baseEventDir = '';
@ -92,18 +93,24 @@ export namespace event {
return eventCls return eventCls
} }
execute(name, exec, eventCls) { /**
*
* @param name
* @param exec
* @param eventCls
* @returns
*/
createExecute(name, exec, eventCls) {
return (...args: any[]) => { return (...args: any[]) => {
try {
let event = args[args.length - 1] let event = args[args.length - 1]
if (eventCls.isAssignableFrom(event.getClass())) { try {
let time = Date.now() if (!eventCls.isAssignableFrom(event.getClass())) { return }
exec(event) let time = Date.now(); exec(event); let cost = Date.now() - time
let cost = Date.now() - time
if (cost > global.ScriptSlowExecuteTime) { if (cost > global.ScriptSlowExecuteTime) {
let eventKey = `${name}-${this.class2Name(eventCls)}`
if (!this.cacheSlowEventKey[eventKey]) { return this.cacheSlowEventKey[eventKey] = cost }
console.i18n("ms.api.event.execute.slow", { name, event: this.class2Name(eventCls), cost }) console.i18n("ms.api.event.execute.slow", { name, event: this.class2Name(eventCls), cost })
} }
}
} catch (ex: any) { } catch (ex: any) {
console.i18n("ms.api.event.execute.error", { name, event: this.class2Name(eventCls), ex }) console.i18n("ms.api.event.execute.error", { name, event: this.class2Name(eventCls), ex })
console.ex(ex) console.ex(ex)
@ -135,7 +142,7 @@ export namespace event {
// noinspection JSUnusedGlobalSymbols // noinspection JSUnusedGlobalSymbols
var listener = this.register( var listener = this.register(
eventCls, eventCls,
this.execute(name, exec, eventCls), this.createExecute(name, exec, eventCls),
priority, priority,
ignoreCancel ignoreCancel
) )

View File

@ -185,15 +185,25 @@ export namespace server {
} }
} }
} }
if (this.rootLogger.class.name.indexOf('slf4j') !== -1) {
try {
let LogManager = Java.type('org.apache.logging.log4j.LogManager')
this.rootLogger = LogManager.getLogger('ROOT')
} catch (error: any) {
if (global.debug) {
console.ex(error)
}
}
}
if (this.rootLogger && this.rootLogger.class.name.indexOf('Logger') === -1) { if (this.rootLogger && this.rootLogger.class.name.indexOf('Logger') === -1) {
console.error('Error Logger Class: ' + this.rootLogger.class.name) console.error('Error Logger Class: ' + this.rootLogger.class.name)
this.rootLogger = undefined this.rootLogger = undefined
} }
if (!this.rootLogger) { console.error("Can't found rootLogger!") }
// get root logger // get root logger
for (let index = 0; index < 5 && this.rootLogger.parent; index++) { for (let index = 0; index < 5 && this.rootLogger.parent; index++) {
this.rootLogger = this.rootLogger.parent this.rootLogger = this.rootLogger.parent
} }
if (!this.rootLogger) { console.error("Can't found rootLogger!") }
this.container.bind(constants.ServiceIdentifier.RootLogger).toConstantValue(this.rootLogger) this.container.bind(constants.ServiceIdentifier.RootLogger).toConstantValue(this.rootLogger)
} }
} }

View File

@ -78,7 +78,7 @@ export class PluginCommandManager {
permission = `${pluginInstance.description.name.toLocaleLowerCase()}.${command}.${subcommand || 'main'}` permission = `${pluginInstance.description.name.toLocaleLowerCase()}.${command}.${subcommand || 'main'}`
} }
if (!sender.hasPermission(permission)) { if (!sender.hasPermission(permission)) {
return pluginInstance.logger.sender(sender, `§c你需要 ${permission} 权限 才可执行此命令.`) return pluginInstance.logger.sender(sender, `§c你需要 §4${permission} §c权限 才可执行此命令.`)
} }
} }
args.shift() args.shift()
@ -98,7 +98,7 @@ export class PluginCommandManager {
return (args.length == 1 ? cmdSubCache : []).concat(originCompleter?.apply(pluginInstance, [sender, command, args]) || []) return (args.length == 1 ? cmdSubCache : []).concat(originCompleter?.apply(pluginInstance, [sender, command, args]) || [])
} }
} }
if (!cmdCompleter) { console.warn(`[${pluginInstance.description.name}] command ${cmd.name} is not registry tabCompleter`) } if (!cmdCompleter) { console.debug(`[${pluginInstance.description.name}] command ${cmd.name} is not registry tabCompleter`) }
return [cmdExecutor, cmdCompleter] return [cmdExecutor, cmdCompleter]
} }
} }

View File

@ -7,8 +7,7 @@ import { interfaces } from './interfaces'
import { getPluginConfigMetadata } from './utils' import { getPluginConfigMetadata } from './utils'
import { PluginConfigLoader } from './config/interfaces' import { PluginConfigLoader } from './config/interfaces'
import './config/loader/json-loader' import './config/loader'
import './config/loader/yaml-loader'
@provideSingleton(PluginConfigManager) @provideSingleton(PluginConfigManager)
export class PluginConfigManager { export class PluginConfigManager {

View File

@ -98,6 +98,7 @@ export class PluginManagerImpl implements plugin.PluginManager {
try { try {
this.loadAndRequirePlugin(loadMetadata) this.loadAndRequirePlugin(loadMetadata)
} catch (error: any) { } catch (error: any) {
console.console(`§c扫描器 §4${scanner.type} §c文件 §4${loadMetadata.file.toString().replace(root, '')} §c编译失败 请提供下列错误给开发者`)
console.ex(error) console.ex(error)
} }
} }

View File

@ -2,7 +2,7 @@ import { EventEmitter } from "events"
import * as parser_v4 from "../engine.io-parser" import * as parser_v4 from "../engine.io-parser"
import * as parser_v3 from "./parser-v3" import * as parser_v3 from "./parser-v3"
// import debugModule from "debug" // import debugModule from "debug"
import { IncomingMessage } from "http" // import { IncomingMessage } from "http"
import { Packet } from "../engine.io-parser" import { Packet } from "../engine.io-parser"
// const debug = debugModule("engine:transport") // const debug = debugModule("engine:transport")
@ -24,7 +24,8 @@ export abstract class Transport extends EventEmitter {
protected _readyState: string protected _readyState: string
protected discarded: boolean protected discarded: boolean
protected parser: any protected parser: any
protected req: IncomingMessage & { cleanup: Function } // protected req: IncomingMessage & { cleanup: Function }
protected req: { cleanup: Function }
protected supportsBinary: boolean protected supportsBinary: boolean
get readyState() { get readyState() {