From 655c35d4953865a9c556f1f654e3dba42e98220e Mon Sep 17 00:00:00 2001 From: ZacharyZcR Date: Tue, 28 Apr 2026 12:30:19 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=87=AD=E6=8D=AE=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E8=BF=9E=E7=BB=AD=E7=BD=91=E7=BB=9C=E9=94=99=E8=AF=AF=E7=9F=AD?= =?UTF-8?q?=E8=B7=AF=E3=80=81resultChan=20=E7=BC=93=E5=86=B2=E9=98=B2?= =?UTF-8?q?=E9=98=BB=E5=A1=9E=E3=80=81timer=20=E6=B3=84=E6=BC=8F=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/services/credential_tester.go | 34 +++++++++++++++++++++------ 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/plugins/services/credential_tester.go b/plugins/services/credential_tester.go index e7e3394..97698fd 100644 --- a/plugins/services/credential_tester.go +++ b/plugins/services/credential_tester.go @@ -118,9 +118,10 @@ func DefaultConcurrentTestConfig(config *common.Config) ConcurrentTestConfig { concurrency = 10 } return ConcurrentTestConfig{ - Concurrency: concurrency, - MaxRetries: 3, - RetryDelay: time.Second, + Concurrency: concurrency, + MaxRetries: 3, + RetryDelay: time.Second, + MaxConsecutiveNetErrors: 5, } } @@ -151,9 +152,9 @@ func TestCredentialsConcurrently( cancelCtx, cancel := context.WithCancel(ctx) defer cancel() - // 通道 + // 通道(buffer 设为 concurrency+1 避免 worker 阻塞在发送上) credChan := make(chan Credential, len(credentials)) - resultChan := make(chan *ScanResult, concurrency) + resultChan := make(chan *ScanResult, concurrency+1) // 发送所有凭据 for _, cred := range credentials { @@ -211,6 +212,12 @@ func workerTestCredentials( serviceName string, testConfig ConcurrentTestConfig, ) { + consecutiveNetErrors := 0 + maxNetErrors := testConfig.MaxConsecutiveNetErrors + if maxNetErrors <= 0 { + maxNetErrors = 5 + } + for cred := range credChan { // 检查是否应该停止 select { @@ -219,12 +226,24 @@ func workerTestCredentials( default: } + // 连续网络错误达到阈值,目标可能不可达,提前退出 + if consecutiveNetErrors >= maxNetErrors { + return + } + // 带重试的凭据测试 result := testCredentialWithRetry(ctx, cred, authFn, serviceName, testConfig) if result != nil && result.Success { resultChan <- result return } + + // 跟踪连续网络错误 + if result != nil && result.Error != nil { + consecutiveNetErrors++ + } else { + consecutiveNetErrors = 0 + } } } @@ -267,11 +286,12 @@ func testCredentialWithRetry( case ErrorTypeNetwork, ErrorTypeUnknown: // 网络错误或未知错误,可以重试(可能是服务端限流等临时问题) if attempt < testConfig.MaxRetries-1 { + timer := time.NewTimer(testConfig.RetryDelay) select { case <-ctx.Done(): + timer.Stop() return nil - case <-time.After(testConfig.RetryDelay): - // 继续重试 + case <-timer.C: } } }