Repository navigation
fix: batch add key backend deduplication - #2581
Conversation
WalkthroughThe Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
controller/channel.go (1)
983-1006: Deduplication logic is correct; consider normalizing all keys consistently.The deduplication logic successfully prevents duplicate keys from being added. The use of a
seenmap with normalized keys (trimmed) ensures that duplicates are caught regardless of whitespace differences.However, there's a minor inconsistency:
dedupedNewKeysare trimmed (line 1002), butexistingKeys(line 1005) retain their original formatting, which may include leading/trailing whitespace. This means the final key list could have mixed normalization states.🔎 Suggested improvements
1. Normalize all keys consistently (recommended):
Apply trimming to existing keys before joining to ensure consistent formatting throughout the key list:
seen := make(map[string]struct{}, len(existingKeys)+len(newKeys)) + normalizedExistingKeys := make([]string, 0, len(existingKeys)) for _, key := range existingKeys { normalized := strings.TrimSpace(key) if normalized == "" { continue } seen[normalized] = struct{}{} + normalizedExistingKeys = append(normalizedExistingKeys, normalized) } dedupedNewKeys := make([]string, 0, len(newKeys)) for _, key := range newKeys { normalized := strings.TrimSpace(key) if normalized == "" { continue } if _, ok := seen[normalized]; ok { continue } seen[normalized] = struct{}{} dedupedNewKeys = append(dedupedNewKeys, normalized) } - allKeys := append(existingKeys, dedupedNewKeys...) + allKeys := append(normalizedExistingKeys, dedupedNewKeys...) channel.Key = strings.Join(allKeys, "\n")2. Provide user feedback (optional):
Consider logging or returning information about how many duplicate keys were skipped, which would improve the user experience by making the deduplication behavior transparent.
📜 Review details
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
controller/channel.go
🧰 Additional context used
🧠 Learnings (1)
📓 Common learnings
Learnt from: RedwindA
Repo: QuantumNous/new-api PR: 1517
File: controller/channel.go:196-196
Timestamp: 2025-08-06T17:09:08.221Z
Learning: In the channel management system, channel keys are validated as required during channel creation, so they are guaranteed to be non-empty in functions like FetchUpstreamModels. Additional validation for empty keys in downstream functions is not needed.
fix #2519
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.