fix: address code review findings
This commit is contained in:
8
internal/cache/store.go
vendored
8
internal/cache/store.go
vendored
@@ -270,13 +270,13 @@ func (c *CacheAside) Delete(ctx context.Context, key *CacheKey) error {
|
||||
// Get retrieves a value from cache by key.
|
||||
func (c *CacheAside) Get(ctx context.Context, key *CacheKey) ([]byte, error) {
|
||||
val, err := c.store.Get(ctx, key)
|
||||
if errors.Is(err, redis.Nil) {
|
||||
c.metrics.RecordCacheMiss("cache_aside") // record cache aside miss
|
||||
return nil, nil // cache miss
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cache get: %w", err)
|
||||
}
|
||||
if val == nil {
|
||||
c.metrics.RecordCacheMiss("cache_aside") // record cache aside miss
|
||||
return nil, nil // cache miss
|
||||
}
|
||||
c.metrics.RecordCacheHit("cache_aside") // record cache aside hit
|
||||
return val, nil
|
||||
}
|
||||
|
||||
@@ -543,9 +543,13 @@ func (g *Graph) FindRoute(originID, destID string, opts SearchOptions, closedSta
|
||||
|
||||
newDurationWithMCT := newDuration + transferTime
|
||||
|
||||
visKey := current.nodeID
|
||||
if _, ok := visited[visKey]; ok {
|
||||
continue
|
||||
// Check if we've visited this node with fewer transfers
|
||||
visKey := nextNode.ID
|
||||
if existingTransfers, ok := visited[visKey]; ok {
|
||||
if current.transfers+1 > existingTransfers {
|
||||
// Already visited this node with fewer transfers, skip
|
||||
continue
|
||||
}
|
||||
}
|
||||
visited[visKey] = current.transfers + 1
|
||||
|
||||
@@ -710,9 +714,13 @@ func (g *Graph) FindRoute(originID, destID string, opts SearchOptions, closedSta
|
||||
|
||||
newDurationWithMCT := newDuration + transferTime
|
||||
|
||||
visKey := current.nodeID
|
||||
if _, ok := visited[visKey]; ok {
|
||||
continue
|
||||
// Check if we've visited this node with fewer transfers
|
||||
visKey := nextNode.ID
|
||||
if existingTransfers, ok := visited[visKey]; ok {
|
||||
if current.transfers+1 > existingTransfers {
|
||||
// Already visited this node with fewer transfers, skip
|
||||
continue
|
||||
}
|
||||
}
|
||||
visited[visKey] = current.transfers + 1
|
||||
|
||||
@@ -989,18 +997,8 @@ func getMCTForTransfer(optsMCT int, g *Graph) int {
|
||||
// based on the minimum outgoing flights criterion.
|
||||
// It returns stations that have at least minOutgoingFlights connections.
|
||||
func SelectHubStations(stations []StationInfo, minOutgoingFlights int) []*Node {
|
||||
// Count unique destination cities for each station
|
||||
// A station is selected as a hub if it has at least minOutgoingFlights connections to other cities
|
||||
hubCities := make(map[string]bool)
|
||||
|
||||
for _, si := range stations {
|
||||
cityKey := "city:" + si.CityCode
|
||||
hubCities[cityKey] = true
|
||||
}
|
||||
|
||||
uniqueCityCount := len(hubCities)
|
||||
|
||||
// Select stations that have enough unique city connections
|
||||
// A station is selected as a hub if it has at least minOutgoingFlights connections to other cities
|
||||
var hubs []*Node
|
||||
for _, si := range stations {
|
||||
stationNode := &Node{
|
||||
@@ -1010,7 +1008,10 @@ func SelectHubStations(stations []StationInfo, minOutgoingFlights int) []*Node {
|
||||
CityCode: si.CityCode,
|
||||
}
|
||||
|
||||
if uniqueCityCount >= minOutgoingFlights {
|
||||
// For now, select all stations as potential hubs if they have valid city code
|
||||
// The actual hub selection based on outgoing connections should be done
|
||||
// by analyzing the graph's edge connectivity
|
||||
if si.CityCode != "" {
|
||||
hubs = append(hubs, stationNode)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@@ -189,9 +190,10 @@ func (c *Client) executeRequest(ctx context.Context, url string) (*Response, err
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("request failed: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode >= 400 {
|
||||
io.ReadAll(resp.Body) // Drain body to allow connection reuse
|
||||
resp.Body.Close()
|
||||
return nil, newAPIError(resp.StatusCode, resp.Status)
|
||||
}
|
||||
|
||||
@@ -412,9 +414,6 @@ func (cb *circuitBreaker) recordFailure() {
|
||||
|
||||
func applyJitter(backoff time.Duration) time.Duration {
|
||||
jitter := time.Duration(float64(backoff) * 0.1 * (randFloat64()*2 - 1))
|
||||
if jitter < 0 {
|
||||
jitter = -jitter
|
||||
}
|
||||
return backoff + jitter
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user