refactor: 调整key验证结构
This commit is contained in:
@@ -41,9 +41,6 @@ func BuildContainer() (*dig.Container, error) {
|
||||
if err := container.Provide(services.NewTaskService); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := container.Provide(services.NewKeyValidatorService); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := container.Provide(services.NewKeyValidationPool); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -62,6 +59,9 @@ func BuildContainer() (*dig.Container, error) {
|
||||
if err := container.Provide(keypool.NewProvider); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := container.Provide(keypool.NewKeyValidator); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Handlers
|
||||
if err := container.Provide(handler.NewServer); err != nil {
|
||||
|
@@ -6,6 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
"gpt-load/internal/config"
|
||||
"gpt-load/internal/keypool"
|
||||
"gpt-load/internal/models"
|
||||
"gpt-load/internal/services"
|
||||
"gpt-load/internal/types"
|
||||
@@ -20,7 +21,7 @@ type Server struct {
|
||||
DB *gorm.DB
|
||||
config types.ConfigManager
|
||||
SettingsManager *config.SystemSettingsManager
|
||||
KeyValidatorService *services.KeyValidatorService
|
||||
KeyValidator *keypool.KeyValidator
|
||||
KeyManualValidationService *services.KeyManualValidationService
|
||||
TaskService *services.TaskService
|
||||
KeyService *services.KeyService
|
||||
@@ -33,7 +34,7 @@ type NewServerParams struct {
|
||||
DB *gorm.DB
|
||||
Config types.ConfigManager
|
||||
SettingsManager *config.SystemSettingsManager
|
||||
KeyValidatorService *services.KeyValidatorService
|
||||
KeyValidator *keypool.KeyValidator
|
||||
KeyManualValidationService *services.KeyManualValidationService
|
||||
TaskService *services.TaskService
|
||||
KeyService *services.KeyService
|
||||
@@ -46,7 +47,7 @@ func NewServer(params NewServerParams) *Server {
|
||||
DB: params.DB,
|
||||
config: params.Config,
|
||||
SettingsManager: params.SettingsManager,
|
||||
KeyValidatorService: params.KeyValidatorService,
|
||||
KeyValidator: params.KeyValidator,
|
||||
KeyManualValidationService: params.KeyManualValidationService,
|
||||
TaskService: params.TaskService,
|
||||
KeyService: params.KeyService,
|
||||
|
@@ -182,7 +182,7 @@ func (s *Server) TestMultipleKeys(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
results, err := s.KeyValidatorService.TestMultipleKeys(c.Request.Context(), group, keysToTest)
|
||||
results, err := s.KeyValidator.TestMultipleKeys(c.Request.Context(), group, keysToTest)
|
||||
if err != nil {
|
||||
response.Error(c, app_errors.ParseDBError(err))
|
||||
return
|
||||
|
@@ -1,11 +1,10 @@
|
||||
package services
|
||||
package keypool
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"gpt-load/internal/channel"
|
||||
"gpt-load/internal/config"
|
||||
"gpt-load/internal/keypool"
|
||||
"gpt-load/internal/models"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
@@ -20,25 +19,25 @@ type KeyTestResult struct {
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
// KeyValidatorService provides methods to validate API keys.
|
||||
type KeyValidatorService struct {
|
||||
// KeyValidator provides methods to validate API keys.
|
||||
type KeyValidator struct {
|
||||
DB *gorm.DB
|
||||
channelFactory *channel.Factory
|
||||
SettingsManager *config.SystemSettingsManager
|
||||
keypoolProvider *keypool.KeyProvider
|
||||
keypoolProvider *KeyProvider
|
||||
}
|
||||
|
||||
type KeyValidatorServiceParams struct {
|
||||
type KeyValidatorParams struct {
|
||||
dig.In
|
||||
DB *gorm.DB
|
||||
ChannelFactory *channel.Factory
|
||||
SettingsManager *config.SystemSettingsManager
|
||||
KeypoolProvider *keypool.KeyProvider
|
||||
KeypoolProvider *KeyProvider
|
||||
}
|
||||
|
||||
// NewKeyValidatorService creates a new KeyValidatorService.
|
||||
func NewKeyValidatorService(params KeyValidatorServiceParams) *KeyValidatorService {
|
||||
return &KeyValidatorService{
|
||||
// NewKeyValidator creates a new KeyValidator.
|
||||
func NewKeyValidator(params KeyValidatorParams) *KeyValidator {
|
||||
return &KeyValidator{
|
||||
DB: params.DB,
|
||||
channelFactory: params.ChannelFactory,
|
||||
SettingsManager: params.SettingsManager,
|
||||
@@ -47,7 +46,7 @@ func NewKeyValidatorService(params KeyValidatorServiceParams) *KeyValidatorServi
|
||||
}
|
||||
|
||||
// ValidateSingleKey performs a validation check on a single API key.
|
||||
func (s *KeyValidatorService) ValidateSingleKey(ctx context.Context, key *models.APIKey, group *models.Group) (bool, error) {
|
||||
func (s *KeyValidator) ValidateSingleKey(ctx context.Context, key *models.APIKey, group *models.Group) (bool, error) {
|
||||
if ctx.Err() != nil {
|
||||
return false, fmt.Errorf("context cancelled or timed out: %w", ctx.Err())
|
||||
}
|
||||
@@ -79,7 +78,7 @@ func (s *KeyValidatorService) ValidateSingleKey(ctx context.Context, key *models
|
||||
}
|
||||
|
||||
// TestMultipleKeys performs a synchronous validation for a list of key values within a specific group.
|
||||
func (s *KeyValidatorService) TestMultipleKeys(ctx context.Context, group *models.Group, keyValues []string) ([]KeyTestResult, error) {
|
||||
func (s *KeyValidator) TestMultipleKeys(ctx context.Context, group *models.Group, keyValues []string) ([]KeyTestResult, error) {
|
||||
results := make([]KeyTestResult, len(keyValues))
|
||||
|
||||
// Find which of the provided keys actually exist in the database for this group
|
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"gpt-load/internal/config"
|
||||
"gpt-load/internal/keypool"
|
||||
"gpt-load/internal/models"
|
||||
"gpt-load/internal/types"
|
||||
"sync"
|
||||
@@ -23,14 +24,14 @@ type ManualValidationResult struct {
|
||||
// KeyManualValidationService handles user-initiated key validation for a group.
|
||||
type KeyManualValidationService struct {
|
||||
DB *gorm.DB
|
||||
Validator *KeyValidatorService
|
||||
Validator *keypool.KeyValidator
|
||||
TaskService *TaskService
|
||||
SettingsManager *config.SystemSettingsManager
|
||||
ConfigManager types.ConfigManager
|
||||
}
|
||||
|
||||
// NewKeyManualValidationService creates a new KeyManualValidationService.
|
||||
func NewKeyManualValidationService(db *gorm.DB, validator *KeyValidatorService, taskService *TaskService, settingsManager *config.SystemSettingsManager, configManager types.ConfigManager) *KeyManualValidationService {
|
||||
func NewKeyManualValidationService(db *gorm.DB, validator *keypool.KeyValidator, taskService *TaskService, settingsManager *config.SystemSettingsManager, configManager types.ConfigManager) *KeyManualValidationService {
|
||||
return &KeyManualValidationService{
|
||||
DB: db,
|
||||
Validator: validator,
|
||||
|
@@ -2,6 +2,7 @@ package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"gpt-load/internal/keypool"
|
||||
"gpt-load/internal/models"
|
||||
"gpt-load/internal/types"
|
||||
"sync"
|
||||
@@ -27,7 +28,7 @@ type ValidationResult struct {
|
||||
|
||||
// KeyValidationPool manages a global worker pool for key validation.
|
||||
type KeyValidationPool struct {
|
||||
validator *KeyValidatorService
|
||||
validator *keypool.KeyValidator
|
||||
configManager types.ConfigManager
|
||||
jobs chan ValidationJob
|
||||
results chan ValidationResult // 定时任务结果
|
||||
@@ -36,7 +37,7 @@ type KeyValidationPool struct {
|
||||
}
|
||||
|
||||
// NewKeyValidationPool creates a new KeyValidationPool.
|
||||
func NewKeyValidationPool(validator *KeyValidatorService, configManager types.ConfigManager) *KeyValidationPool {
|
||||
func NewKeyValidationPool(validator *keypool.KeyValidator, configManager types.ConfigManager) *KeyValidationPool {
|
||||
return &KeyValidationPool{
|
||||
validator: validator,
|
||||
configManager: configManager,
|
||||
|
Reference in New Issue
Block a user