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