MCPcopy Create free account
hub / github.com/tbphp/gpt-load / RestoreMultipleKeys

Method RestoreMultipleKeys

internal/keypool/provider.go:408–462  ·  view source on GitHub ↗

RestoreMultipleKeys 恢复指定的 Key。

(groupID uint, keyValues []string)

Source from the content-addressed store, hash-verified

406
407// RestoreMultipleKeys 恢复指定的 Key。
408func (p *KeyProvider) RestoreMultipleKeys(groupID uint, keyValues []string) (int64, error) {
409 if len(keyValues) == 0 {
410 return 0, nil
411 }
412
413 var keysToRestore []models.APIKey
414 var restoredCount int64
415
416 err := p.db.Transaction(func(tx *gorm.DB) error {
417 var keyHashes []string
418 for _, keyValue := range keyValues {
419 keyHash := p.encryptionSvc.Hash(keyValue)
420 if keyHash != "" {
421 keyHashes = append(keyHashes, keyHash)
422 }
423 }
424
425 if len(keyHashes) == 0 {
426 return nil
427 }
428
429 if err := tx.Where("group_id = ? AND key_hash IN ? AND status = ?", groupID, keyHashes, models.KeyStatusInvalid).Find(&keysToRestore).Error; err != nil {
430 return err
431 }
432
433 if len(keysToRestore) == 0 {
434 return nil
435 }
436
437 keyIDsToRestore := pluckIDs(keysToRestore)
438
439 updates := map[string]any{
440 "status": models.KeyStatusActive,
441 "failure_count": 0,
442 }
443 result := tx.Model(&models.APIKey{}).Where("id IN ?", keyIDsToRestore).Updates(updates)
444 if result.Error != nil {
445 return result.Error
446 }
447 restoredCount = result.RowsAffected
448
449 for _, key := range keysToRestore {
450 key.Status = models.KeyStatusActive
451 key.FailureCount = 0
452 if err := p.addKeyToStore(&key); err != nil {
453 logrus.WithFields(logrus.Fields{"keyID": key.ID, "error": err}).Error("Failed to restore key in store after DB update")
454 return err
455 }
456 }
457
458 return nil
459 })
460
461 return restoredCount, err
462}
463
464// RemoveInvalidKeys 移除组内所有无效的 Key。
465func (p *KeyProvider) RemoveInvalidKeys(groupID uint) (int64, error) {

Callers

nothing calls this directly

Calls 4

addKeyToStoreMethod · 0.95
pluckIDsFunction · 0.85
HashMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected