diff --git a/.vscode/launch.json b/.vscode/launch.json index 522db7a..2ae8023 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -23,6 +23,8 @@ "lru", "--log-level", "debug", + "--upstream", + "http://192.168.2.5:80", ], }, { @@ -40,6 +42,8 @@ "hybrid", "--log-level", "debug", + "--upstream", + "http://192.168.2.5:80", ], }, { @@ -55,6 +59,8 @@ "lfu", "--log-level", "debug", + "--upstream", + "http://192.168.2.5:80", ], } ] diff --git a/steamcache/steamcache.go b/steamcache/steamcache.go index 5e2b87a..4f94569 100644 --- a/steamcache/steamcache.go +++ b/steamcache/steamcache.go @@ -238,7 +238,7 @@ func New(address string, memorySize string, diskSize string, diskPath, upstream, client := &http.Client{ Transport: transport, - Timeout: 60 * time.Second, + Timeout: 120 * time.Second, // Increased from 60s } sc := &SteamCache{ @@ -251,10 +251,12 @@ func New(address string, memorySize string, diskSize string, diskPath, upstream, diskgc: dgc, client: client, server: &http.Server{ - Addr: address, - ReadTimeout: 5 * time.Second, - WriteTimeout: 10 * time.Second, - IdleTimeout: 120 * time.Second, + Addr: address, + ReadTimeout: 30 * time.Second, // Increased + WriteTimeout: 60 * time.Second, // Increased + IdleTimeout: 120 * time.Second, // Good for keep-alive + ReadHeaderTimeout: 10 * time.Second, // New, for header attacks + MaxHeaderBytes: 1 << 20, // 1MB, optional }, } @@ -268,7 +270,8 @@ func New(address string, memorySize string, diskSize string, diskPath, upstream, if d != nil { if d.Size() > d.Capacity() { - gc.LRUGC(d, uint(d.Size()-d.Capacity())) + gcHandler := gc.GetGCAlgorithm(gc.GCAlgorithm(diskGC)) + gcHandler(d, uint(d.Size()-d.Capacity())) } } @@ -279,7 +282,7 @@ func (sc *SteamCache) Run() { if sc.upstream != "" { resp, err := sc.client.Get(sc.upstream) if err != nil || resp.StatusCode != http.StatusOK { - logger.Logger.Error().Err(err).Str("upstream", sc.upstream).Msg("Failed to connect to upstream server") + logger.Logger.Error().Err(err).Int("status_code", resp.StatusCode).Str("upstream", sc.upstream).Msg("Failed to connect to upstream server") os.Exit(1) } resp.Body.Close() @@ -312,11 +315,6 @@ func (sc *SteamCache) Shutdown() { } func (sc *SteamCache) ServeHTTP(w http.ResponseWriter, r *http.Request) { - if r.URL.Path == "/metrics" { - promhttp.Handler().ServeHTTP(w, r) - return - } - if r.Method != http.MethodGet { requestsTotal.WithLabelValues(r.Method, "405").Inc() logger.Logger.Warn().Str("method", r.Method).Msg("Only GET method is supported") @@ -324,6 +322,11 @@ func (sc *SteamCache) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } + if r.URL.Path == "/" { + w.WriteHeader(http.StatusFound) // this is used by steamcache2's upstream verification at startup + return + } + if r.URL.String() == "/lancache-heartbeat" { w.Header().Add("X-LanCache-Processed-By", "SteamCache2") w.WriteHeader(http.StatusNoContent) @@ -331,6 +334,11 @@ func (sc *SteamCache) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } + if r.URL.Path == "/metrics" { + promhttp.Handler().ServeHTTP(w, r) + return + } + if strings.HasPrefix(r.URL.String(), "/depot/") { // trim the query parameters from the URL path // this is necessary because the cache key should not include query parameters