-
-
Notifications
You must be signed in to change notification settings - Fork 181
fix: 优化供应商测试和日志系统 #186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: 优化供应商测试和日志系统 #186
Changes from all commits
07267a5
93d4b7b
cc3e30c
a178d13
22062f4
25cf1c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,13 +61,9 @@ class ErrorRuleDetector { | |
| private exactPatterns: Map<string, ExactPattern> = new Map(); | ||
| private lastReloadTime: number = 0; | ||
| private isLoading: boolean = false; | ||
| private isInitialized: boolean = false; // 跟踪初始化状态 | ||
|
|
||
| constructor() { | ||
| // 初始化时立即加载缓存(异步,不阻塞构造函数) | ||
| this.reload().catch((error) => { | ||
| logger.error("[ErrorRuleDetector] Failed to initialize cache:", error); | ||
| }); | ||
|
|
||
| // 监听数据库变更事件,自动刷新缓存 | ||
| eventEmitter.on("errorRulesUpdated", () => { | ||
| this.reload().catch((error) => { | ||
|
|
@@ -76,6 +72,17 @@ class ErrorRuleDetector { | |
| }); | ||
| } | ||
|
|
||
| /** | ||
| * 确保规则已加载(懒加载,首次使用时或显式 reload 时调用) | ||
| * 避免在数据库未准备好时过早加载 | ||
| */ | ||
| private async ensureInitialized(): Promise<void> { | ||
| if (this.isInitialized || this.isLoading) { | ||
| return; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium: Race condition in Why this is a problem: If multiple requests call Suggested fix: private initializationPromise: Promise<void> | null = null;
private async ensureInitialized(): Promise<void> {
if (this.isInitialized) {
return;
}
if (!this.initializationPromise) {
this.initializationPromise = this.reload().finally(() => {
this.initializationPromise = null;
});
}
await this.initializationPromise;
} |
||
| } | ||
| await this.reload(); | ||
| } | ||
|
|
||
| /** | ||
| * 从数据库重新加载错误规则 | ||
| */ | ||
|
|
@@ -169,6 +176,7 @@ class ErrorRuleDetector { | |
| } | ||
|
|
||
| this.lastReloadTime = Date.now(); | ||
| this.isInitialized = true; // 标记为已初始化 | ||
|
|
||
| logger.info( | ||
| `[ErrorRuleDetector] Loaded ${rules.length} error rules: ` + | ||
|
|
@@ -184,7 +192,22 @@ class ErrorRuleDetector { | |
| } | ||
|
|
||
| /** | ||
| * 检测错误消息是否匹配任何规则 | ||
| * 异步检测错误消息(推荐使用) | ||
| * 确保规则已加载后再进行检测 | ||
| * | ||
| * @param errorMessage - 错误消息 | ||
| * @returns 检测结果 | ||
| */ | ||
| async detectAsync(errorMessage: string): Promise<ErrorDetectionResult> { | ||
| await this.ensureInitialized(); | ||
| return this.detect(errorMessage); | ||
| } | ||
|
|
||
| /** | ||
| * 检测错误消息是否匹配任何规则(同步版本) | ||
| * | ||
| * 注意:如果规则未初始化,会记录警告并返回 false | ||
| * 推荐使用 detectAsync() 以确保规则已加载 | ||
| * | ||
| * 检测顺序(性能优先): | ||
| * 1. 包含匹配(最快,O(n*m)) | ||
|
|
@@ -199,6 +222,13 @@ class ErrorRuleDetector { | |
| return { matched: false }; | ||
| } | ||
|
|
||
| // 如果未初始化,记录警告 | ||
| if (!this.isInitialized && !this.isLoading) { | ||
| logger.warn( | ||
| "[ErrorRuleDetector] detect() called before initialization, results may be incomplete. Consider using detectAsync() instead." | ||
| ); | ||
| } | ||
|
|
||
| const lowerMessage = errorMessage.toLowerCase(); | ||
| const trimmedMessage = lowerMessage.trim(); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟢 Low: Fallback message "No error details available" could be misleading
Why this is a problem: When both
errorDetailanderrorTextare empty/undefined, the fallback "No error details available" is technically correct but doesn't help users understand what went wrong. This can happen with network timeouts or connection refusals where the response body is empty.Suggested fix: