diff --git a/backend/internal/pkg/websearch/brave.go b/backend/internal/pkg/websearch/brave.go index 5620ca8d68..707e70293e 100644 --- a/backend/internal/pkg/websearch/brave.go +++ b/backend/internal/pkg/websearch/brave.go @@ -62,7 +62,7 @@ func (b *BraveProvider) Search(ctx context.Context, req SearchRequest) (*SearchR if err != nil { return nil, fmt.Errorf("brave: request failed: %w", err) } - defer resp.Body.Close() + defer func() { _ = resp.Body.Close() }() body, err := io.ReadAll(io.LimitReader(resp.Body, maxResponseSize)) if err != nil { diff --git a/backend/internal/pkg/websearch/brave_test.go b/backend/internal/pkg/websearch/brave_test.go index 3fe350209a..4dc5b219d5 100644 --- a/backend/internal/pkg/websearch/brave_test.go +++ b/backend/internal/pkg/websearch/brave_test.go @@ -29,7 +29,7 @@ func TestBraveProvider_Search_Success(t *testing.T) { {URL: "https://tour.go.dev", Title: "Tour", Description: "A Tour of Go", Age: "3 days"}, } w.Header().Set("Content-Type", "application/json") - json.NewEncoder(w).Encode(resp) + _ = json.NewEncoder(w).Encode(resp) })) defer srv.Close() @@ -53,7 +53,7 @@ func TestBraveProvider_Search_DefaultMaxResults(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { receivedCount = r.URL.Query().Get("count") resp := braveResponse{} - json.NewEncoder(w).Encode(resp) + _ = json.NewEncoder(w).Encode(resp) })) defer srv.Close() @@ -70,7 +70,7 @@ func TestBraveProvider_Search_DefaultMaxResults(t *testing.T) { func TestBraveProvider_Search_HTTPError(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(429) - w.Write([]byte("rate limited")) + _, _ = w.Write([]byte("rate limited")) })) defer srv.Close() @@ -86,7 +86,7 @@ func TestBraveProvider_Search_HTTPError(t *testing.T) { func TestBraveProvider_Search_InvalidJSON(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { - w.Write([]byte("not json")) + _, _ = w.Write([]byte("not json")) })) defer srv.Close() @@ -103,7 +103,7 @@ func TestBraveProvider_Search_InvalidJSON(t *testing.T) { func TestBraveProvider_Search_EmptyResults(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { resp := braveResponse{} - json.NewEncoder(w).Encode(resp) + _ = json.NewEncoder(w).Encode(resp) })) defer srv.Close() diff --git a/backend/internal/pkg/websearch/tavily.go b/backend/internal/pkg/websearch/tavily.go index 6ac09edfa0..ac4928a6f8 100644 --- a/backend/internal/pkg/websearch/tavily.go +++ b/backend/internal/pkg/websearch/tavily.go @@ -60,7 +60,7 @@ func (t *TavilyProvider) Search(ctx context.Context, req SearchRequest) (*Search if err != nil { return nil, fmt.Errorf("tavily: request failed: %w", err) } - defer resp.Body.Close() + defer func() { _ = resp.Body.Close() }() body, err := io.ReadAll(io.LimitReader(resp.Body, maxResponseSize)) if err != nil { diff --git a/backend/internal/service/setting_service.go b/backend/internal/service/setting_service.go index 9fcf57de39..b7e886934c 100644 --- a/backend/internal/service/setting_service.go +++ b/backend/internal/service/setting_service.go @@ -105,8 +105,8 @@ type SettingService struct { settingRepo SettingRepository defaultSubGroupReader DefaultSubscriptionGroupReader cfg *config.Config - onUpdate func() // Callback when settings are updated (for cache invalidation) - version string // Application version + onUpdate func() // Callback when settings are updated (for cache invalidation) + version string // Application version webSearchRedis *redis.Client // optional: Redis client for web search quota tracking } diff --git a/backend/internal/service/websearch_config.go b/backend/internal/service/websearch_config.go index 15ec1f9dcc..d59a5a7555 100644 --- a/backend/internal/service/websearch_config.go +++ b/backend/internal/service/websearch_config.go @@ -96,8 +96,7 @@ const ( // GetWebSearchEmulationConfig returns the configuration with in-process cache + singleflight. func (s *SettingService) GetWebSearchEmulationConfig(ctx context.Context) (*WebSearchEmulationConfig, error) { if cached := webSearchEmulationCache.Load(); cached != nil { - c := cached.(*cachedWebSearchEmulationConfig) - if time.Now().UnixNano() < c.expiresAt { + if c, ok := cached.(*cachedWebSearchEmulationConfig); ok && time.Now().UnixNano() < c.expiresAt { return c.config, nil } } @@ -107,7 +106,10 @@ func (s *SettingService) GetWebSearchEmulationConfig(ctx context.Context) (*WebS if err != nil { return &WebSearchEmulationConfig{}, err } - return result.(*WebSearchEmulationConfig), nil + if cfg, ok := result.(*WebSearchEmulationConfig); ok { + return cfg, nil + } + return &WebSearchEmulationConfig{}, nil } func (s *SettingService) loadWebSearchConfigFromDB() (*WebSearchEmulationConfig, error) {