Add configurable timeouts and cache validity to ESI and SSO components

This commit is contained in:
2025-10-10 22:38:09 +02:00
parent fe47d3f8dd
commit 0f0adac82a
5 changed files with 78 additions and 50 deletions

View File

@@ -8,6 +8,7 @@ import (
"fmt"
"time"
"go-eve-pi/options"
"go-eve-pi/types"
logger "git.site.quack-lab.dev/dave/cylogger"
@@ -22,6 +23,7 @@ type CachedESI struct {
GetCacheEntry(urlHash string) (*types.CacheEntry, error)
SaveCacheEntry(entry *types.CacheEntry) error
}
cacheValidity time.Duration
}
// NewCachedESI creates a new CachedESI instance
@@ -29,44 +31,43 @@ func NewCachedESI(direct ESIInterface, db interface {
GetCacheEntry(urlHash string) (*types.CacheEntry, error)
SaveCacheEntry(entry *types.CacheEntry) error
}) *CachedESI {
// Parse cache validity ONCE at initialization
cacheValidity, err := time.ParseDuration(options.GlobalOptions.CacheValidity)
if err != nil {
logger.Warning("Invalid cache validity duration %s, using 10m default: %v", options.GlobalOptions.CacheValidity, err)
cacheValidity = 10 * time.Minute
}
return &CachedESI{
direct: direct,
db: db,
direct: direct,
db: db,
cacheValidity: cacheValidity,
}
}
// GetCharacterPlanets retrieves a list of planets for a character with caching
func (c *CachedESI) GetCharacterPlanets(ctx context.Context, characterID int, accessToken string) ([]Planet, error) {
url := fmt.Sprintf("/v1/characters/%d/planets/", characterID)
result, err := func() (interface{}, error) {
var fetchFunc func() (interface{}, error) = func() (interface{}, error) {
return c.direct.GetCharacterPlanets(ctx, characterID, accessToken)
}
return c.getCachedResponse(url, fetchFunc)
}()
if err != nil {
return nil, err
fetchFunc := func() ([]Planet, error) {
return c.direct.GetCharacterPlanets(ctx, characterID, accessToken)
}
return result.([]Planet), nil
return getCachedResponse(c, url, fetchFunc)
}
// GetPlanetDetails retrieves detailed information about a specific planet with caching
func (c *CachedESI) GetPlanetDetails(ctx context.Context, characterID, planetID int, accessToken string) (*PlanetDetail, error) {
url := fmt.Sprintf("/v3/characters/%d/planets/%d/", characterID, planetID)
result, err := func() (interface{}, error) {
var fetchFunc func() (interface{}, error) = func() (interface{}, error) {
return c.direct.GetPlanetDetails(ctx, characterID, planetID, accessToken)
}
return c.getCachedResponse(url, fetchFunc)
}()
if err != nil {
return nil, err
fetchFunc := func() (*PlanetDetail, error) {
return c.direct.GetPlanetDetails(ctx, characterID, planetID, accessToken)
}
return result.(*PlanetDetail), nil
return getCachedResponse(c, url, fetchFunc)
}
// getCachedResponse handles caching logic
func (c *CachedESI) getCachedResponse(url string, fetchFunc func() (interface{}, error)) (interface{}, error) {
// getCachedResponse handles caching logic with generics
func getCachedResponse[T any](c *CachedESI, url string, fetchFunc func() (T, error)) (T, error) {
// Generate cache key
hash := sha256.Sum256([]byte(url))
urlHash := hex.EncodeToString(hash[:])
@@ -75,22 +76,12 @@ func (c *CachedESI) getCachedResponse(url string, fetchFunc func() (interface{},
cacheEntry, err := c.db.GetCacheEntry(urlHash)
if err == nil {
// Check if cache is still valid
cacheValidity, _ := time.ParseDuration("10m") // Default 10 minutes
if time.Since(cacheEntry.CachedAt) < cacheValidity {
if time.Since(cacheEntry.CachedAt) < c.cacheValidity {
logger.Debug("Cache hit for URL: %s", url)
// Parse cached response based on URL pattern
if url[len(url)-1:] == "/" {
// Planets endpoint
var planets []Planet
if err := json.Unmarshal([]byte(cacheEntry.Response), &planets); err == nil {
return planets, nil
}
} else {
// Planet details endpoint
var planetDetail PlanetDetail
if err := json.Unmarshal([]byte(cacheEntry.Response), &planetDetail); err == nil {
return &planetDetail, nil
}
// Parse cached response
var result T
if err := json.Unmarshal([]byte(cacheEntry.Response), &result); err == nil {
return result, nil
}
}
}
@@ -99,7 +90,8 @@ func (c *CachedESI) getCachedResponse(url string, fetchFunc func() (interface{},
logger.Debug("Cache miss for URL: %s", url)
result, err := fetchFunc()
if err != nil {
return nil, err
var zero T
return zero, err
}
// Store in cache