Use CachedRequest instead of LimitedHTTP

Bleh...
This commit is contained in:
2026-01-24 22:27:39 +01:00
parent 20a936ce0e
commit 5e205ffb51

120
enrich.go
View File

@@ -2,10 +2,9 @@ package main
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"path/filepath"
"sync"
"time"
@@ -149,8 +148,6 @@ func getRegionFromDB(ctx context.Context, db DB, regionID int32) (*models.MapReg
return db.GetRegion(ctx, regionID)
}
var globalHTTPClient = cyutils.LimitedHTTP(10.0, 20)
var globalFlatCache = &FlatCache{
types: NewCache(getTypeFromDB, func(key int32) *logger.Logger {
return logger.Default.WithPrefix("getType").WithPrefix(fmt.Sprintf("type_%d", key))
@@ -651,39 +648,17 @@ func getCharacterName(characterID int64) (string, error) {
return "", fmt.Errorf("failed to create request: %w", err)
}
flog.Debug("Sending HTTP request to proxy")
resp, err := globalHTTPClient.Do(req)
cacheFile := filepath.Join(".cache", fmt.Sprintf("character_%d.json", characterID))
char, err := cyutils.RequestCached[Character](req, cacheFile)
if err != nil {
flog.Error("HTTP request failed: %v", err)
return "", fmt.Errorf("failed to fetch character %d: %w", characterID, err)
}
defer resp.Body.Close()
flog.Debug("Received response: status %d", resp.StatusCode)
if resp.StatusCode == http.StatusNotFound {
flog.Debug("Character not found (404)")
return "", fmt.Errorf("character %d not found", characterID)
}
if resp.StatusCode != http.StatusOK {
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
flog.Error("Failed to read response body: %v", err)
return "", fmt.Errorf("character %d returned status %d and failed to read body: %w", characterID, resp.StatusCode, err)
// Check if it's a 404
if err.Error() == fmt.Sprintf("character %d not found", characterID) ||
err.Error() == fmt.Sprintf("character %d returned status 404", characterID) {
flog.Debug("Character not found (404)")
return "", fmt.Errorf("character %d not found", characterID)
}
bodyStr := string(bodyBytes)
flog.Error("Character request failed with status %d", resp.StatusCode)
flog.Error("Response body: %s", bodyStr)
flog.Error("Response headers: %v", resp.Header)
return "", fmt.Errorf("character %d returned status %d: %s", characterID, resp.StatusCode, bodyStr)
}
var char Character
if err := json.NewDecoder(resp.Body).Decode(&char); err != nil {
flog.Error("Failed to decode JSON response: %v", err)
return "", fmt.Errorf("failed to decode character %d: %w", characterID, err)
flog.Error("Failed to get character: %v", err)
return "", err
}
flog.Debug("Successfully got character name: %s", char.Name)
@@ -706,34 +681,17 @@ func getCorporationName(corporationID int64) (string, error) {
return "", fmt.Errorf("failed to create request: %w", err)
}
flog.Debug("Sending HTTP request to proxy")
resp, err := globalHTTPClient.Do(req)
cacheFile := filepath.Join(".cache", fmt.Sprintf("corporation_%d.json", corporationID))
corp, err := cyutils.RequestCached[Corporation](req, cacheFile)
if err != nil {
flog.Error("HTTP request failed: %v", err)
return "", fmt.Errorf("failed to fetch corporation %d: %w", corporationID, err)
}
defer resp.Body.Close()
flog.Debug("Received response: status %d", resp.StatusCode)
if resp.StatusCode != http.StatusOK {
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
flog.Error("Failed to read response body: %v", err)
return "", fmt.Errorf("corporation %d returned status %d and failed to read body: %w", corporationID, resp.StatusCode, err)
// Check if it's a 404
if err.Error() == fmt.Sprintf("corporation %d not found", corporationID) ||
err.Error() == fmt.Sprintf("corporation %d returned status 404", corporationID) {
flog.Debug("Corporation not found (404)")
return "", fmt.Errorf("corporation %d not found", corporationID)
}
bodyStr := string(bodyBytes)
flog.Error("Corporation request failed with status %d", resp.StatusCode)
flog.Error("Response body: %s", bodyStr)
flog.Error("Response headers: %v", resp.Header)
return "", fmt.Errorf("corporation %d returned status %d: %s", corporationID, resp.StatusCode, bodyStr)
}
var corp Corporation
if err := json.NewDecoder(resp.Body).Decode(&corp); err != nil {
flog.Error("Failed to decode JSON response: %v", err)
return "", fmt.Errorf("failed to decode corporation %d: %w", corporationID, err)
flog.Error("Failed to get corporation: %v", err)
return "", err
}
flog.Debug("Successfully got corporation name: %s", corp.Name)
@@ -756,39 +714,17 @@ func getAllianceName(allianceID int64) (string, error) {
return "", fmt.Errorf("failed to create request: %w", err)
}
flog.Debug("Sending HTTP request to proxy")
resp, err := globalHTTPClient.Do(req)
cacheFile := filepath.Join(".cache", fmt.Sprintf("alliance_%d.json", allianceID))
alliance, err := cyutils.RequestCached[Alliance](req, cacheFile)
if err != nil {
flog.Error("HTTP request failed: %v", err)
return "", fmt.Errorf("failed to fetch alliance %d: %w", allianceID, err)
}
defer resp.Body.Close()
flog.Debug("Received response: status %d", resp.StatusCode)
if resp.StatusCode == http.StatusNotFound {
flog.Debug("Alliance not found (404)")
return "", fmt.Errorf("alliance %d not found", allianceID)
}
if resp.StatusCode != http.StatusOK {
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
flog.Error("Failed to read response body: %v", err)
return "", fmt.Errorf("alliance %d returned status %d and failed to read body: %w", allianceID, resp.StatusCode, err)
// Check if it's a 404
if err.Error() == fmt.Sprintf("alliance %d not found", allianceID) ||
err.Error() == fmt.Sprintf("alliance %d returned status 404", allianceID) {
flog.Debug("Alliance not found (404)")
return "", fmt.Errorf("alliance %d not found", allianceID)
}
bodyStr := string(bodyBytes)
flog.Error("Alliance request failed with status %d", resp.StatusCode)
flog.Error("Response body: %s", bodyStr)
flog.Error("Response headers: %v", resp.Header)
return "", fmt.Errorf("alliance %d returned status %d: %s", allianceID, resp.StatusCode, bodyStr)
}
var alliance Alliance
if err := json.NewDecoder(resp.Body).Decode(&alliance); err != nil {
flog.Error("Failed to decode JSON response: %v", err)
return "", fmt.Errorf("failed to decode alliance %d: %w", allianceID, err)
flog.Error("Failed to get alliance: %v", err)
return "", err
}
flog.Debug("Successfully got alliance name: %s", alliance.Name)