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:
@@ -325,11 +325,15 @@ func (tb *tokenBucket) acquire() error {
|
||||
func (tb *tokenBucket) refill(now time.Time) {
|
||||
elapsed := now.Sub(tb.lastRefill)
|
||||
if elapsed >= time.Second {
|
||||
// Refill tokens based on elapsed time and rate
|
||||
tb.tokens = tb.capacity
|
||||
tokensToAdd := int(elapsed.Seconds()) * tb.refillPerSec
|
||||
if tb.tokens+tokensToAdd > tb.capacity {
|
||||
tb.tokens = tb.capacity
|
||||
} else {
|
||||
tb.tokens += tokensToAdd
|
||||
}
|
||||
tb.lastRefill = now
|
||||
}
|
||||
// else: keep current tokens, will fully refill on next second boundary
|
||||
// else: keep current tokens, will add on next refill
|
||||
}
|
||||
|
||||
// --- Circuit Breaker ---
|
||||
@@ -342,7 +346,6 @@ func newCircuitBreaker() *circuitBreaker {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func (cb *circuitBreaker) ResetCircuitBreaker() {
|
||||
cb.mu.Lock()
|
||||
defer cb.mu.Unlock()
|
||||
@@ -420,4 +423,4 @@ func applyJitter(backoff time.Duration) time.Duration {
|
||||
func randFloat64() float64 {
|
||||
// Use math/rand with a seed based on function call index for variability
|
||||
return rand.Float64()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user