From 682f546c0e743718934ac3f0de5abc132ec6ae0e Mon Sep 17 00:00:00 2001 From: yangjianbo Date: Wed, 31 Dec 2025 14:51:58 +0800 Subject: [PATCH] =?UTF-8?q?fix(lint):=20=E4=BF=AE=E5=A4=8D=20golangci-lint?= =?UTF-8?q?=20=E6=8A=A5=E5=91=8A=E7=9A=84=E4=BB=A3=E7=A0=81=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - errcheck: 修复类型断言未检查返回值的问题 - pool.go: 添加 sync.Map 类型断言安全检查 - req_client_pool.go: 添加 sync.Map 类型断言安全检查 - concurrency_cache_benchmark_test.go: 显式忽略断言返回值 - gateway_service.go: 显式忽略 WriteString 返回值 - gofmt: 修复代码格式问题 - redis.go: 注释对齐 - api_key_repo.go: 结构体字段对齐 - concurrency_cache.go: 字段对齐 - http_upstream.go: 注释对齐 - unused: 删除未使用的代码 - user_repo.go: 删除未使用的 sql 字段 - usage_service.go: 删除未使用的 calculateStats 函数 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- backend/internal/infrastructure/redis.go | 4 +-- backend/internal/pkg/httpclient/pool.go | 9 +++++-- backend/internal/repository/api_key_repo.go | 14 +++++----- .../internal/repository/concurrency_cache.go | 4 +-- .../concurrency_cache_benchmark_test.go | 2 +- backend/internal/repository/http_upstream.go | 2 +- .../internal/repository/req_client_pool.go | 9 +++++-- backend/internal/repository/user_repo.go | 5 ++-- backend/internal/service/gateway_service.go | 2 +- backend/internal/service/usage_service.go | 26 ------------------- 10 files changed, 30 insertions(+), 47 deletions(-) diff --git a/backend/internal/infrastructure/redis.go b/backend/internal/infrastructure/redis.go index 5bb92d19..9f4c8770 100644 --- a/backend/internal/infrastructure/redis.go +++ b/backend/internal/infrastructure/redis.go @@ -33,7 +33,7 @@ func buildRedisOptions(cfg *config.Config) *redis.Options { DialTimeout: time.Duration(cfg.Redis.DialTimeoutSeconds) * time.Second, // 建连超时 ReadTimeout: time.Duration(cfg.Redis.ReadTimeoutSeconds) * time.Second, // 读取超时 WriteTimeout: time.Duration(cfg.Redis.WriteTimeoutSeconds) * time.Second, // 写入超时 - PoolSize: cfg.Redis.PoolSize, // 连接池大小 - MinIdleConns: cfg.Redis.MinIdleConns, // 最小空闲连接 + PoolSize: cfg.Redis.PoolSize, // 连接池大小 + MinIdleConns: cfg.Redis.MinIdleConns, // 最小空闲连接 } } diff --git a/backend/internal/pkg/httpclient/pool.go b/backend/internal/pkg/httpclient/pool.go index f68d50a5..1028fb84 100644 --- a/backend/internal/pkg/httpclient/pool.go +++ b/backend/internal/pkg/httpclient/pool.go @@ -58,7 +58,9 @@ var sharedClients sync.Map func GetClient(opts Options) (*http.Client, error) { key := buildClientKey(opts) if cached, ok := sharedClients.Load(key); ok { - return cached.(*http.Client), nil + if client, ok := cached.(*http.Client); ok { + return client, nil + } } client, err := buildClient(opts) @@ -72,7 +74,10 @@ func GetClient(opts Options) (*http.Client, error) { } actual, _ := sharedClients.LoadOrStore(key, client) - return actual.(*http.Client), nil + if c, ok := actual.(*http.Client); ok { + return c, nil + } + return client, nil } func buildClient(opts Options) (*http.Client, error) { diff --git a/backend/internal/repository/api_key_repo.go b/backend/internal/repository/api_key_repo.go index a3a52333..3ba2fd85 100644 --- a/backend/internal/repository/api_key_repo.go +++ b/backend/internal/repository/api_key_repo.go @@ -311,13 +311,13 @@ func groupEntityToService(g *dbent.Group) *service.Group { return nil } return &service.Group{ - ID: g.ID, - Name: g.Name, - Description: derefString(g.Description), - Platform: g.Platform, - RateMultiplier: g.RateMultiplier, - IsExclusive: g.IsExclusive, - Status: g.Status, + ID: g.ID, + Name: g.Name, + Description: derefString(g.Description), + Platform: g.Platform, + RateMultiplier: g.RateMultiplier, + IsExclusive: g.IsExclusive, + Status: g.Status, SubscriptionType: g.SubscriptionType, DailyLimitUSD: g.DailyLimitUsd, WeeklyLimitUSD: g.WeeklyLimitUsd, diff --git a/backend/internal/repository/concurrency_cache.go b/backend/internal/repository/concurrency_cache.go index 31527f22..9205230b 100644 --- a/backend/internal/repository/concurrency_cache.go +++ b/backend/internal/repository/concurrency_cache.go @@ -126,7 +126,7 @@ var ( ) type concurrencyCache struct { - rdb *redis.Client + rdb *redis.Client slotTTLSeconds int // 槽位过期时间(秒) } @@ -137,7 +137,7 @@ func NewConcurrencyCache(rdb *redis.Client, slotTTLMinutes int) service.Concurre slotTTLMinutes = defaultSlotTTLMinutes } return &concurrencyCache{ - rdb: rdb, + rdb: rdb, slotTTLSeconds: slotTTLMinutes * 60, } } diff --git a/backend/internal/repository/concurrency_cache_benchmark_test.go b/backend/internal/repository/concurrency_cache_benchmark_test.go index 29cc7fbc..cafab9cb 100644 --- a/backend/internal/repository/concurrency_cache_benchmark_test.go +++ b/backend/internal/repository/concurrency_cache_benchmark_test.go @@ -22,7 +22,7 @@ func BenchmarkAccountConcurrency(b *testing.B) { _ = rdb.Close() }() - cache := NewConcurrencyCache(rdb, benchSlotTTLMinutes).(*concurrencyCache) + cache, _ := NewConcurrencyCache(rdb, benchSlotTTLMinutes).(*concurrencyCache) ctx := context.Background() for _, size := range []int{10, 100, 1000} { diff --git a/backend/internal/repository/http_upstream.go b/backend/internal/repository/http_upstream.go index 061866b1..180844b5 100644 --- a/backend/internal/repository/http_upstream.go +++ b/backend/internal/repository/http_upstream.go @@ -572,7 +572,7 @@ func buildUpstreamTransport(settings poolSettings, proxyURL *url.URL) *http.Tran // trackedBody 带跟踪功能的响应体包装器 // 在 Close 时执行回调,用于更新请求计数 type trackedBody struct { - io.ReadCloser // 原始响应体 + io.ReadCloser // 原始响应体 once sync.Once onClose func() // 关闭时的回调函数 } diff --git a/backend/internal/repository/req_client_pool.go b/backend/internal/repository/req_client_pool.go index bfe0ccd2..b23462a4 100644 --- a/backend/internal/repository/req_client_pool.go +++ b/backend/internal/repository/req_client_pool.go @@ -35,7 +35,9 @@ var sharedReqClients sync.Map func getSharedReqClient(opts reqClientOptions) *req.Client { key := buildReqClientKey(opts) if cached, ok := sharedReqClients.Load(key); ok { - return cached.(*req.Client) + if c, ok := cached.(*req.Client); ok { + return c + } } client := req.C().SetTimeout(opts.Timeout) @@ -47,7 +49,10 @@ func getSharedReqClient(opts reqClientOptions) *req.Client { } actual, _ := sharedReqClients.LoadOrStore(key, client) - return actual.(*req.Client) + if c, ok := actual.(*req.Client); ok { + return c + } + return client } func buildReqClientKey(opts reqClientOptions) string { diff --git a/backend/internal/repository/user_repo.go b/backend/internal/repository/user_repo.go index 7294fadc..8393ae7c 100644 --- a/backend/internal/repository/user_repo.go +++ b/backend/internal/repository/user_repo.go @@ -16,15 +16,14 @@ import ( type userRepository struct { client *dbent.Client - sql sqlExecutor } func NewUserRepository(client *dbent.Client, sqlDB *sql.DB) service.UserRepository { return newUserRepositoryWithSQL(client, sqlDB) } -func newUserRepositoryWithSQL(client *dbent.Client, sqlq sqlExecutor) *userRepository { - return &userRepository{client: client, sql: sqlq} +func newUserRepositoryWithSQL(client *dbent.Client, _ sqlExecutor) *userRepository { + return &userRepository{client: client} } func (r *userRepository) Create(ctx context.Context, userIn *service.User) error { diff --git a/backend/internal/service/gateway_service.go b/backend/internal/service/gateway_service.go index dd879da2..d542e9c2 100644 --- a/backend/internal/service/gateway_service.go +++ b/backend/internal/service/gateway_service.go @@ -197,7 +197,7 @@ func (s *GatewayService) extractCacheableContent(parsed *ParsedRequest) string { if cc, ok := partMap["cache_control"].(map[string]any); ok { if cc["type"] == "ephemeral" { if text, ok := partMap["text"].(string); ok { - builder.WriteString(text) + _, _ = builder.WriteString(text) } } } diff --git a/backend/internal/service/usage_service.go b/backend/internal/service/usage_service.go index f57e90eb..f653ddfe 100644 --- a/backend/internal/service/usage_service.go +++ b/backend/internal/service/usage_service.go @@ -235,32 +235,6 @@ func (s *UsageService) GetDailyStats(ctx context.Context, userID int64, days int return stats, nil } -// calculateStats 计算统计数据 -func (s *UsageService) calculateStats(logs []UsageLog) *UsageStats { - stats := &UsageStats{} - - for _, log := range logs { - stats.TotalRequests++ - stats.TotalInputTokens += int64(log.InputTokens) - stats.TotalOutputTokens += int64(log.OutputTokens) - stats.TotalCacheTokens += int64(log.CacheCreationTokens + log.CacheReadTokens) - stats.TotalTokens += int64(log.TotalTokens()) - stats.TotalCost += log.TotalCost - stats.TotalActualCost += log.ActualCost - - if log.DurationMs != nil { - stats.AverageDurationMs += float64(*log.DurationMs) - } - } - - // 计算平均持续时间 - if stats.TotalRequests > 0 { - stats.AverageDurationMs /= float64(stats.TotalRequests) - } - - return stats -} - // Delete 删除使用日志(管理员功能,谨慎使用) func (s *UsageService) Delete(ctx context.Context, id int64) error { if err := s.usageRepo.Delete(ctx, id); err != nil {