- 新增 RoutingCache 组件,使用 sync.Map 缓存 Provider 和 Model - 新增 StatsBuffer 组件,使用 sync.Map + atomic.Int64 缓冲统计数据 - 扩展 StatsRepository.BatchUpdate 支持批量增量更新 - 改造 RoutingService/StatsService/ProviderService/ModelService 集成缓存 - 更新 usage-statistics spec,新增 routing-cache 和 stats-buffer spec - 新增单元测试覆盖缓存命中/失效/并发场景
41 lines
811 B
Go
41 lines
811 B
Go
package service
|
|
|
|
import (
|
|
appErrors "nex/backend/pkg/errors"
|
|
|
|
"nex/backend/internal/domain"
|
|
)
|
|
|
|
type routingService struct {
|
|
cache *RoutingCache
|
|
}
|
|
|
|
func NewRoutingService(cache *RoutingCache) RoutingService {
|
|
return &routingService{cache: cache}
|
|
}
|
|
|
|
func (s *routingService) RouteByModelName(providerID, modelName string) (*domain.RouteResult, error) {
|
|
model, err := s.cache.GetModel(providerID, modelName)
|
|
if err != nil {
|
|
return nil, appErrors.ErrModelNotFound
|
|
}
|
|
|
|
if !model.Enabled {
|
|
return nil, appErrors.ErrModelDisabled
|
|
}
|
|
|
|
provider, err := s.cache.GetProvider(model.ProviderID)
|
|
if err != nil {
|
|
return nil, appErrors.ErrProviderNotFound
|
|
}
|
|
|
|
if !provider.Enabled {
|
|
return nil, appErrors.ErrProviderDisabled
|
|
}
|
|
|
|
return &domain.RouteResult{
|
|
Provider: provider,
|
|
Model: model,
|
|
}, nil
|
|
}
|