Skip to content

fix: batch add key backend deduplication - #2581

Merged
seefs001 merged 1 commit into
QuantumNous:mainfrom
seefs001:fix/batch-add-key-deduplicate
Jan 5, 2026
Merged

seefs001 merged 1 commit into
QuantumNous:mainfrom
seefs001:fix/batch-add-key-deduplicate

Conversation

@seefs001

@seefs001 seefs001 commented Jan 5, 2026 •

Copy link
Copy Markdown
Collaborator

fix #2519

Summary by CodeRabbit

  • Bug Fixes
    • Improved API key handling during channel updates by eliminating duplicate entries and normalizing whitespace, ensuring more reliable channel configuration.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai

coderabbitai Bot commented Jan 5, 2026 •

Copy link
Copy Markdown
Contributor

Walkthrough

The UpdateChannel function in controller/channel.go now normalizes and deduplicates keys when merging new keys with existing ones. The updated logic trims whitespace, filters empty keys, tracks seen keys, and prevents duplicate entries from being added to the existing key set.

Changes

Cohort / File(s) Summary
Key merge deduplication
controller/channel.go
Modified UpdateChannel to normalize keys by trimming whitespace and removing empty entries. Added deduplication logic that checks against existing keys before appending new ones. Changes applied to both Vertex AI special-case and general merge paths; replace path unchanged.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 Hippity-hop, the keys align,
Duplicates trimmed with care so fine,
Empty spaces swept away,
Clean and tidy, hip-hooray!
No more clutter in the field,
Deduplication's now sealed! 🔑✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly relates to the main change: adding deduplication logic for batch key operations in the backend, which is exactly what the code changes implement.
✨ Finishing touches
  • 📝 Generate docstrings

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 seen map with normalized keys (trimmed) ensures that duplicates are caught regardless of whitespace differences.

However, there's a minor inconsistency: dedupedNewKeys are trimmed (line 1002), but existingKeys (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

📥 Commits

Reviewing files that changed from the base of the PR and between 177553a and a8f7c06.

📒 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.

@seefs001
seefs001 merged commit 9addf1b into QuantumNous:main Jan 5, 2026
@seefs001
seefs001 deleted the fix/batch-add-key-deduplicate branch January 5, 2026 10:52
ennnnny pushed a commit to ennnnny/new-api that referenced this pull request Mar 17, 2026
salem-2007 added a commit to salem-2007/new-api that referenced this pull request Sep 10, 2026
junjundesk pushed a commit to junjundesk/new-api that referenced this pull request Oct 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

多密钥渠道追加秘钥时应与数据库中的秘钥去重

2 participants