feat: update dependencies and improve caching mechanism
Some checks failed
PR Check / check-and-test (pull_request) Failing after 2m11s
Some checks failed
PR Check / check-and-test (pull_request) Failing after 2m11s
- Added Prometheus client library for metrics collection. - Refactored garbage collection strategy from random deletion to LRU (Least Recently Used) deletion. - Introduced per-key locking in cache to prevent race conditions. - Enhanced logging with structured log messages for cache hits and misses. - Implemented a retry mechanism for upstream requests with exponential backoff. - Updated Go modules and indirect dependencies for better compatibility and performance. - Removed unused sync filesystem implementation. - Added version initialization to ensure a default version string.
This commit is contained in:
@@ -1,29 +1,45 @@
|
||||
package steamcache
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"runtime"
|
||||
"s1d3sw1ped/SteamCache2/steamcache/avgcachestate"
|
||||
"s1d3sw1ped/SteamCache2/steamcache/logger"
|
||||
"s1d3sw1ped/SteamCache2/version"
|
||||
"s1d3sw1ped/SteamCache2/vfs"
|
||||
"s1d3sw1ped/SteamCache2/vfs/cache"
|
||||
"s1d3sw1ped/SteamCache2/vfs/cachestate"
|
||||
"s1d3sw1ped/SteamCache2/vfs/disk"
|
||||
"s1d3sw1ped/SteamCache2/vfs/gc"
|
||||
"s1d3sw1ped/SteamCache2/vfs/memory"
|
||||
syncfs "s1d3sw1ped/SteamCache2/vfs/sync"
|
||||
|
||||
// syncfs "s1d3sw1ped/SteamCache2/vfs/sync"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
pprof "net/http/pprof"
|
||||
|
||||
"github.com/docker/go-units"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
)
|
||||
|
||||
var (
|
||||
requestsTotal = promauto.NewCounterVec(
|
||||
prometheus.CounterOpts{
|
||||
Name: "http_requests_total",
|
||||
Help: "Total number of HTTP requests",
|
||||
},
|
||||
[]string{"method", "status"},
|
||||
)
|
||||
cacheHitRate = promauto.NewGauge(
|
||||
prometheus.GaugeOpts{
|
||||
Name: "cache_hit_rate",
|
||||
Help: "Cache hit rate",
|
||||
},
|
||||
)
|
||||
)
|
||||
|
||||
type SteamCache struct {
|
||||
@@ -40,9 +56,6 @@ type SteamCache struct {
|
||||
diskgc *gc.GCFS
|
||||
|
||||
hits *avgcachestate.AvgCacheState
|
||||
|
||||
dirty bool
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func New(address string, memorySize string, memoryMultiplier int, diskSize string, diskMultiplier int, diskPath, upstream string, pprof bool) *SteamCache {
|
||||
@@ -64,14 +77,14 @@ func New(address string, memorySize string, memoryMultiplier int, diskSize strin
|
||||
var mgc *gc.GCFS
|
||||
if memorysize > 0 {
|
||||
m = memory.New(memorysize)
|
||||
mgc = gc.New(m, memoryMultiplier, randomgc)
|
||||
mgc = gc.New(m, memoryMultiplier, lruGC)
|
||||
}
|
||||
|
||||
var d *disk.DiskFS
|
||||
var dgc *gc.GCFS
|
||||
if disksize > 0 {
|
||||
d = disk.New(diskPath, disksize)
|
||||
dgc = gc.New(d, diskMultiplier, randomgc)
|
||||
dgc = gc.New(d, diskMultiplier, lruGC)
|
||||
}
|
||||
|
||||
// configure the cache to match the specified mode (memory only, disk only, or memory and disk) based on the provided sizes
|
||||
@@ -98,7 +111,8 @@ func New(address string, memorySize string, memoryMultiplier int, diskSize strin
|
||||
pprof: pprof,
|
||||
upstream: upstream,
|
||||
address: address,
|
||||
vfs: syncfs.New(c),
|
||||
// vfs: syncfs.New(c),
|
||||
vfs: c,
|
||||
|
||||
memory: m,
|
||||
disk: d,
|
||||
@@ -111,7 +125,7 @@ func New(address string, memorySize string, memoryMultiplier int, diskSize strin
|
||||
|
||||
if d != nil {
|
||||
if d.Size() > d.Capacity() {
|
||||
randomgc(d, uint(d.Size()-d.Capacity()))
|
||||
lruGC(d, uint(d.Size()-d.Capacity()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,18 +141,6 @@ func (sc *SteamCache) Run() {
|
||||
}
|
||||
}
|
||||
|
||||
sc.mu.Lock()
|
||||
sc.dirty = true
|
||||
sc.mu.Unlock()
|
||||
|
||||
sc.LogStats()
|
||||
t := time.NewTicker(1 * time.Second)
|
||||
go func() {
|
||||
for range t.C {
|
||||
sc.LogStats()
|
||||
}
|
||||
}()
|
||||
|
||||
err := http.ListenAndServe(sc.address, sc)
|
||||
if err != nil {
|
||||
if err == http.ErrServerClosed {
|
||||
@@ -150,71 +152,6 @@ func (sc *SteamCache) Run() {
|
||||
}
|
||||
}
|
||||
|
||||
func (sc *SteamCache) LogStats() {
|
||||
sc.mu.Lock()
|
||||
defer sc.mu.Unlock()
|
||||
if sc.dirty {
|
||||
|
||||
up := sc.upstream
|
||||
if up == "" {
|
||||
up = "{host in request}"
|
||||
}
|
||||
|
||||
logger.Logger.Info().Str("address", sc.address).Str("version", version.Version).Str("upstream", up).Msg("listening")
|
||||
if sc.memory != nil { // only log memory if memory is enabled
|
||||
lifetimeBytes, lifetimeFiles, reclaimedBytes, deletedFiles, gcTime := sc.memorygc.Stats()
|
||||
|
||||
logger.Logger.Info().
|
||||
Str("size", units.HumanSize(float64(sc.memory.Size()))).
|
||||
Str("capacity", units.HumanSize(float64(sc.memory.Capacity()))).
|
||||
Str("files", fmt.Sprintf("%d", len(sc.memory.StatAll()))).
|
||||
Msg("memory")
|
||||
|
||||
logger.Logger.Info().
|
||||
Str("data_total", units.HumanSize(float64(lifetimeBytes))).
|
||||
Uint("files_total", lifetimeFiles).
|
||||
Str("data", units.HumanSize(float64(reclaimedBytes))).
|
||||
Uint("files", deletedFiles).
|
||||
Str("gc_time", gcTime.String()).
|
||||
Msg("memory_gc")
|
||||
}
|
||||
|
||||
if sc.disk != nil { // only log disk if disk is enabled
|
||||
lifetimeBytes, lifetimeFiles, reclaimedBytes, deletedFiles, gcTime := sc.diskgc.Stats()
|
||||
|
||||
logger.Logger.Info().
|
||||
Str("size", units.HumanSize(float64(sc.disk.Size()))).
|
||||
Str("capacity", units.HumanSize(float64(sc.disk.Capacity()))).
|
||||
Str("files", fmt.Sprintf("%d", len(sc.disk.StatAll()))).
|
||||
Msg("disk")
|
||||
|
||||
logger.Logger.Info().
|
||||
Str("data_total", units.HumanSize(float64(lifetimeBytes))).
|
||||
Uint("files_total", lifetimeFiles).
|
||||
Str("data", units.HumanSize(float64(reclaimedBytes))).
|
||||
Uint("files", deletedFiles).
|
||||
Str("gc_time", gcTime.String()).
|
||||
Msg("disk_gc")
|
||||
}
|
||||
|
||||
// log golang Garbage Collection stats
|
||||
var m runtime.MemStats
|
||||
runtime.ReadMemStats(&m)
|
||||
|
||||
logger.Logger.Info().
|
||||
Str("alloc", units.HumanSize(float64(m.Alloc))).
|
||||
Str("sys", units.HumanSize(float64(m.Sys))).
|
||||
Msg("app_gc")
|
||||
|
||||
logger.Logger.Info().
|
||||
Str("hitrate", fmt.Sprintf("%.2f%%", sc.hits.Avg()*100)).
|
||||
Msg("cache")
|
||||
|
||||
logger.Logger.Info().Msg("") // empty line to separate log entries for better readability
|
||||
sc.dirty = false
|
||||
}
|
||||
}
|
||||
|
||||
func (sc *SteamCache) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if sc.pprof && r.URL.Path == "/debug/pprof/" {
|
||||
pprof.Index(w, r)
|
||||
@@ -223,8 +160,13 @@ func (sc *SteamCache) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
pprof.Handler(strings.TrimPrefix(r.URL.Path, "/debug/pprof/")).ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
if r.URL.Path == "/metrics" {
|
||||
promhttp.Handler().ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
if r.Method != http.MethodGet {
|
||||
requestsTotal.WithLabelValues(r.Method, "405").Inc()
|
||||
http.Error(w, "Only GET method is supported", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
@@ -236,24 +178,33 @@ func (sc *SteamCache) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
sc.mu.Lock()
|
||||
sc.dirty = true
|
||||
sc.mu.Unlock()
|
||||
|
||||
w.Header().Add("X-LanCache-Processed-By", "SteamCache2") // SteamPrefill uses this header to determine if the request was processed by the cache maybe steam uses it too
|
||||
tstart := time.Now()
|
||||
|
||||
cacheKey := strings.ReplaceAll(r.URL.String()[1:], "\\", "/") // replace all backslashes with forward slashes shouldn't be necessary but just in case
|
||||
if cacheKey == "" {
|
||||
requestsTotal.WithLabelValues(r.Method, "400").Inc()
|
||||
http.Error(w, "Invalid URL", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Add("X-LanCache-Processed-By", "SteamCache2") // SteamPrefill uses this header to determine if the request was processed by the cache maybe steam uses it too
|
||||
|
||||
data, err := sc.vfs.Get(cacheKey)
|
||||
if err == nil {
|
||||
sc.hits.Add(cachestate.CacheStateHit)
|
||||
w.Header().Add("X-LanCache-Status", "HIT")
|
||||
requestsTotal.WithLabelValues(r.Method, "200").Inc()
|
||||
cacheHitRate.Set(sc.hits.Avg())
|
||||
|
||||
w.Write(data)
|
||||
logger.Logger.Debug().Str("key", r.URL.String()).Msg("cache")
|
||||
|
||||
logger.Logger.Info().
|
||||
Str("key", cacheKey).
|
||||
Str("host", r.Host).
|
||||
Str("status", "HIT").
|
||||
Int64("size", int64(len(data))).
|
||||
Dur("duration", time.Since(tstart)).
|
||||
Msg("request")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -261,17 +212,18 @@ func (sc *SteamCache) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if sc.upstream != "" { // if an upstream server is configured, proxy the request to the upstream server
|
||||
ur, err := url.JoinPath(sc.upstream, r.URL.String())
|
||||
if err != nil {
|
||||
requestsTotal.WithLabelValues(r.Method, "500").Inc()
|
||||
http.Error(w, "Failed to join URL path", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
req, err = http.NewRequest(http.MethodGet, ur, nil)
|
||||
if err != nil {
|
||||
requestsTotal.WithLabelValues(r.Method, "500").Inc()
|
||||
http.Error(w, "Failed to create request", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
req.Host = r.Host
|
||||
logger.Logger.Debug().Str("key", cacheKey).Str("host", sc.upstream).Msg("upstream")
|
||||
} else { // if no upstream server is configured, proxy the request to the host specified in the request
|
||||
host := r.Host
|
||||
if r.Header.Get("X-Sls-Https") == "enable" {
|
||||
@@ -282,35 +234,51 @@ func (sc *SteamCache) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
ur, err := url.JoinPath(host, r.URL.String())
|
||||
if err != nil {
|
||||
requestsTotal.WithLabelValues(r.Method, "500").Inc()
|
||||
http.Error(w, "Failed to join URL path", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
req, err = http.NewRequest(http.MethodGet, ur, nil)
|
||||
if err != nil {
|
||||
requestsTotal.WithLabelValues(r.Method, "500").Inc()
|
||||
http.Error(w, "Failed to create request", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
logger.Logger.Debug().Str("key", cacheKey).Str("host", host).Msg("forward")
|
||||
}
|
||||
|
||||
req.Header.Add("X-Sls-Https", r.Header.Get("X-Sls-Https"))
|
||||
req.Header.Add("User-Agent", r.Header.Get("User-Agent"))
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
// Copy headers from the original request to the new request
|
||||
for key, values := range r.Header {
|
||||
for _, value := range values {
|
||||
req.Header.Add(key, value)
|
||||
}
|
||||
}
|
||||
|
||||
// req.Header.Add("X-Sls-Https", r.Header.Get("X-Sls-Https"))
|
||||
// req.Header.Add("User-Agent", r.Header.Get("User-Agent"))
|
||||
|
||||
// Retry logic
|
||||
backoffSchedule := []time.Duration{1 * time.Second, 3 * time.Second, 10 * time.Second}
|
||||
var resp *http.Response
|
||||
for i, backoff := range backoffSchedule {
|
||||
resp, err = http.DefaultClient.Do(req)
|
||||
if err == nil && resp.StatusCode == http.StatusOK {
|
||||
break
|
||||
}
|
||||
if i < len(backoffSchedule)-1 {
|
||||
time.Sleep(backoff)
|
||||
}
|
||||
}
|
||||
if err != nil || resp.StatusCode != http.StatusOK {
|
||||
requestsTotal.WithLabelValues(r.Method, "500").Inc()
|
||||
http.Error(w, "Failed to fetch the requested URL", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
http.Error(w, "Failed to fetch the requested URL", resp.StatusCode)
|
||||
return
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
requestsTotal.WithLabelValues(r.Method, "500").Inc()
|
||||
http.Error(w, "Failed to read response body", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@@ -318,5 +286,16 @@ func (sc *SteamCache) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
sc.vfs.Set(cacheKey, body)
|
||||
sc.hits.Add(cachestate.CacheStateMiss)
|
||||
w.Header().Add("X-LanCache-Status", "MISS")
|
||||
requestsTotal.WithLabelValues(r.Method, "200").Inc()
|
||||
cacheHitRate.Set(sc.hits.Avg())
|
||||
|
||||
w.Write(body)
|
||||
|
||||
logger.Logger.Info().
|
||||
Str("key", cacheKey).
|
||||
Str("host", r.Host).
|
||||
Str("status", "MISS").
|
||||
Int64("size", int64(len(body))).
|
||||
Dur("duration", time.Since(tstart)).
|
||||
Msg("request")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user