🐛 fix(redis,mongodb): 修复Redis集群数据显示不全和MongoDB指定数据库连接失败

- Redis集群ScanKeys改用ForEachMaster逐master节点扫描合并去重
- MongoDB authSource未显式设置时始终默认admin而非Database值
- 同步修复mongodb_impl.go和mongodb_impl_v1.go
- refs #246
This commit is contained in:
Syngnat
2026-03-18 15:51:19 +08:00
parent fb70f1420c
commit 5c5e1fc68f
3 changed files with 59 additions and 6 deletions

View File

@@ -237,9 +237,6 @@ func (m *MongoDB) getURI(config connection.ConnectionConfig) string {
params.Set("serverSelectionTimeoutMS", strconv.Itoa(timeout*1000))
authSource := strings.TrimSpace(config.AuthSource)
if authSource == "" && strings.TrimSpace(config.Database) != "" {
authSource = strings.TrimSpace(config.Database)
}
if authSource == "" {
authSource = "admin"
}

View File

@@ -238,9 +238,6 @@ func (m *MongoDBV1) getURI(config connection.ConnectionConfig) string {
params.Set("serverSelectionTimeoutMS", strconv.Itoa(timeout*1000))
authSource := strings.TrimSpace(config.AuthSource)
if authSource == "" && strings.TrimSpace(config.Database) != "" {
authSource = strings.TrimSpace(config.Database)
}
if authSource == "" {
authSource = "admin"
}

View File

@@ -388,6 +388,65 @@ func (r *RedisClientImpl) ScanKeys(pattern string, cursor uint64, count int64) (
ctx, cancel := context.WithTimeout(context.Background(), maxDuration+5*time.Second)
defer cancel()
// 集群模式:逐 master 节点 SCAN 后合并去重
if r.isCluster && r.clusterClient != nil {
keys := make([]string, 0, int(targetCount))
seen := make(map[string]struct{}, int(targetCount))
var mu sync.Mutex
err := r.clusterClient.ForEachMaster(ctx, func(nodeCtx context.Context, node *redis.Client) error {
var nodeCursor uint64
round := 0
scanStartedAt := time.Now()
for {
if time.Since(scanStartedAt) >= maxDuration {
break
}
mu.Lock()
enough := len(keys) >= int(targetCount)
mu.Unlock()
if enough {
break
}
batch, nextCursor, err := node.Scan(nodeCtx, nodeCursor, physicalPattern, scanStepCount).Result()
if err != nil {
return err
}
mu.Lock()
for _, key := range batch {
if _, ok := seen[key]; ok {
continue
}
seen[key] = struct{}{}
keys = append(keys, key)
if len(keys) >= int(targetCount) {
break
}
}
mu.Unlock()
nodeCursor = nextCursor
round++
if nodeCursor == 0 || round >= maxRounds {
break
}
}
return nil
})
if err != nil {
return nil, err
}
// 集群模式 cursor 无意义,始终返回 "0" 表示扫描完成
return &RedisScanResult{
Keys: r.loadRedisKeyInfos(ctx, keys),
Cursor: "0",
}, nil
}
// 非集群模式:原逻辑
currentCursor := cursor
round := 0
scanStartedAt := time.Now()