fix: 删除group时内存数据一致
This commit is contained in:
@@ -447,31 +447,30 @@ func (s *Server) DeleteGroup(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := tx.Commit().Error; err != nil {
|
||||
tx.Rollback()
|
||||
response.Error(c, app_errors.ErrDatabase)
|
||||
return
|
||||
}
|
||||
|
||||
// Clean up memory store (Redis) - this is done after successful DB transaction
|
||||
// to maintain consistency. If this fails, the keys will be cleaned up during
|
||||
// the next key pool reload.
|
||||
// Clean up memory store (Redis) within the transaction to ensure atomicity
|
||||
// If Redis cleanup fails, the entire transaction will be rolled back
|
||||
if len(keyIDs) > 0 {
|
||||
if err := s.KeyService.KeyProvider.RemoveKeysFromStore(uint(id), keyIDs); err != nil {
|
||||
tx.Rollback()
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"groupID": id,
|
||||
"keyCount": len(keyIDs),
|
||||
"error": err,
|
||||
}).Error("Failed to remove keys from memory store")
|
||||
}).Error("Failed to remove keys from memory store, rolling back transaction")
|
||||
|
||||
response.Success(c, gin.H{
|
||||
"message": "Group and associated keys deleted successfully",
|
||||
"warning": "Some keys may remain in memory cache and will be cleaned up during next restart",
|
||||
})
|
||||
response.Error(c, app_errors.NewAPIError(app_errors.ErrDatabase,
|
||||
"Failed to delete group: unable to clean up cache"))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Commit the transaction only if both DB and Redis operations succeed
|
||||
if err := tx.Commit().Error; err != nil {
|
||||
tx.Rollback()
|
||||
response.Error(c, app_errors.ErrDatabase)
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, gin.H{"message": "Group and associated keys deleted successfully"})
|
||||
}
|
||||
|
||||
|
@@ -474,7 +474,7 @@ func (p *KeyProvider) RemoveKeysFromStore(groupID uint, keyIDs []uint) error {
|
||||
"groupID": groupID,
|
||||
"error": err,
|
||||
}).Error("Failed to delete active keys list")
|
||||
// 继续执行hash删除,因为即使列表删除失败,hash仍然需要清理
|
||||
return err
|
||||
}
|
||||
|
||||
// 第二步:批量删除所有相关的key hash
|
||||
@@ -485,7 +485,6 @@ func (p *KeyProvider) RemoveKeysFromStore(groupID uint, keyIDs []uint) error {
|
||||
"keyID": keyID,
|
||||
"error": err,
|
||||
}).Error("Failed to delete key hash")
|
||||
// 继续删除其他keys,不因单个失败而中断
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user