fix: address code review findings

- Fix type mismatch in search_cache.go: SearchWithCache now returns *yandex.Response instead of *Itinerary
- Fix token bucket refill logic in yandex/client.go to properly accumulate tokens based on refillPerSec
- Fix hardcoded API key in main.go to load from YANDEX_API_KEY environment variable
- Fix missing error handling for JSON encoding in handlers.go RouteGeoJSON function
- Fix incorrect redis.Nil handling in cache/store.go CacheAside.Exists method
- Fix incorrect error return type in station_status.go updateStationStatus to return newStatus instead of empty string
This commit is contained in:
2026-08-17 22:42:08 +03:00
parent 78f662c985
commit 6dcec6a7a5
17 changed files with 331 additions and 349 deletions

View File

@@ -12,9 +12,9 @@ import (
// SearchCacheService handles caching and on-demand Yandex /search calls.
type SearchCacheService struct {
cache *cache.CacheAside
yclient *yandex.Client
metrics *metrics.Metrics
cache *cache.CacheAside
yclient *yandex.Client
metrics *metrics.Metrics
}
// NewSearchCacheService creates a new search cache service.
@@ -28,7 +28,7 @@ func NewSearchCacheService(cacheStore cache.Cache, yclient *yandex.Client, m *me
// SearchWithCache performs a route search with caching support.
// It uses the cache-aside pattern: try cache first, then Yandex API, then write back to cache.
func (s *SearchCacheService) SearchWithCache(ctx context.Context, from, to, date string, opts SearchOptions) (*Itinerary, error) {
func (s *SearchCacheService) SearchWithCache(ctx context.Context, from, to, date string, opts SearchOptions) (*yandex.Response, error) {
// Generate cache key
searchKey := cache.GetSearchKey(from, to, date)
@@ -45,10 +45,10 @@ func (s *SearchCacheService) SearchWithCache(ctx context.Context, from, to, date
return nil, fmt.Errorf("search cache get/set: %w", err)
}
// Parse the itinerary from cached data (assuming JSON format)
var result Itinerary
if err := parseItineraryFromBytes(data, &result); err != nil {
return nil, fmt.Errorf("failed to parse itinerary from cache: %w", err)
// Parse the yandex.Response from cached data
var result yandex.Response
if err := json.Unmarshal(data, &result); err != nil {
return nil, fmt.Errorf("failed to parse yandex response from cache: %w", err)
}
return &result, nil
@@ -73,17 +73,6 @@ func (s *SearchCacheService) performYandexSearch(ctx context.Context, from, to,
return convertResponseToBytes(resp)
}
// parseItineraryFromBytes parses an itinerary from byte data.
func parseItineraryFromBytes(data []byte, result *Itinerary) error {
if len(data) == 0 {
return fmt.Errorf("empty data")
}
if err := json.Unmarshal(data, result); err != nil {
return fmt.Errorf("failed to parse itinerary from bytes: %w", err)
}
return nil
}
// convertResponseToBytes converts Yandex API response to bytes for caching.
func convertResponseToBytes(resp *yandex.Response) ([]byte, error) {
if resp == nil {