fix: 凭据测试连续网络错误短路、resultChan 缓冲防阻塞、timer 泄漏修复

This commit is contained in:
ZacharyZcR
2026-04-28 12:30:19 +08:00
parent d41a595195
commit 655c35d495
+24 -4
View File
@@ -121,6 +121,7 @@ func DefaultConcurrentTestConfig(config *common.Config) ConcurrentTestConfig {
Concurrency: concurrency, Concurrency: concurrency,
MaxRetries: 3, MaxRetries: 3,
RetryDelay: time.Second, RetryDelay: time.Second,
MaxConsecutiveNetErrors: 5,
} }
} }
@@ -151,9 +152,9 @@ func TestCredentialsConcurrently(
cancelCtx, cancel := context.WithCancel(ctx) cancelCtx, cancel := context.WithCancel(ctx)
defer cancel() defer cancel()
// 通道 // 通道(buffer 设为 concurrency+1 避免 worker 阻塞在发送上)
credChan := make(chan Credential, len(credentials)) credChan := make(chan Credential, len(credentials))
resultChan := make(chan *ScanResult, concurrency) resultChan := make(chan *ScanResult, concurrency+1)
// 发送所有凭据 // 发送所有凭据
for _, cred := range credentials { for _, cred := range credentials {
@@ -211,6 +212,12 @@ func workerTestCredentials(
serviceName string, serviceName string,
testConfig ConcurrentTestConfig, testConfig ConcurrentTestConfig,
) { ) {
consecutiveNetErrors := 0
maxNetErrors := testConfig.MaxConsecutiveNetErrors
if maxNetErrors <= 0 {
maxNetErrors = 5
}
for cred := range credChan { for cred := range credChan {
// 检查是否应该停止 // 检查是否应该停止
select { select {
@@ -219,12 +226,24 @@ func workerTestCredentials(
default: default:
} }
// 连续网络错误达到阈值,目标可能不可达,提前退出
if consecutiveNetErrors >= maxNetErrors {
return
}
// 带重试的凭据测试 // 带重试的凭据测试
result := testCredentialWithRetry(ctx, cred, authFn, serviceName, testConfig) result := testCredentialWithRetry(ctx, cred, authFn, serviceName, testConfig)
if result != nil && result.Success { if result != nil && result.Success {
resultChan <- result resultChan <- result
return return
} }
// 跟踪连续网络错误
if result != nil && result.Error != nil {
consecutiveNetErrors++
} else {
consecutiveNetErrors = 0
}
} }
} }
@@ -267,11 +286,12 @@ func testCredentialWithRetry(
case ErrorTypeNetwork, ErrorTypeUnknown: case ErrorTypeNetwork, ErrorTypeUnknown:
// 网络错误或未知错误,可以重试(可能是服务端限流等临时问题) // 网络错误或未知错误,可以重试(可能是服务端限流等临时问题)
if attempt < testConfig.MaxRetries-1 { if attempt < testConfig.MaxRetries-1 {
timer := time.NewTimer(testConfig.RetryDelay)
select { select {
case <-ctx.Done(): case <-ctx.Done():
timer.Stop()
return nil return nil
case <-time.After(testConfig.RetryDelay): case <-timer.C:
// 继续重试
} }
} }
} }