From fc312b76d23d100d9349ae777e1ab7497ffa6354 Mon Sep 17 00:00:00 2001 From: Sendya <18x@loacg.com> Date: Wed, 4 Feb 2026 21:17:58 +0800 Subject: [PATCH] feat: Introduce flag constants and enable conditional caching of error responses using `FlagOn`. --- internal/constants/global.go | 6 ++++++ server/middleware/caching/caching.go | 22 +++++++++++++++------- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/internal/constants/global.go b/internal/constants/global.go index cc9fcf5..4babfc8 100644 --- a/internal/constants/global.go +++ b/internal/constants/global.go @@ -16,3 +16,9 @@ const ( InternalCacheErrCode = "i-x-ct-code" InternalUpstreamAddr = "i-x-ups-addr" ) + +// define flag constants +const ( + FlagOn = "1" // gateway control flag ON + FlagOff = "0" // gateway control flag OFF +) diff --git a/server/middleware/caching/caching.go b/server/middleware/caching/caching.go index f7359d0..09d88c7 100644 --- a/server/middleware/caching/caching.go +++ b/server/middleware/caching/caching.go @@ -387,13 +387,21 @@ func (c *Caching) doProxy(req *http.Request, subRequest bool) (*http.Response, e c.md.Size = respRange.ObjSize // error code cache feature. - if statusCode >= http.StatusBadRequest && - resp.Header.Get(constants.InternalCacheErrCode) != "1" { - c.cacheable = false // 禁止缓存错误码缓存 - - copiedHeaders := make(http.Header) - xhttp.CopyHeader(copiedHeaders, resp.Header) - c.md.Headers = copiedHeaders + if statusCode >= http.StatusBadRequest { + + // Caching is disabled + // restoring the default behavior for error codes. + if resp.Header.Get(constants.InternalCacheErrCode) != constants.FlagOn { + c.cacheable = false + + copiedHeaders := make(http.Header) + xhttp.CopyHeader(copiedHeaders, resp.Header) + c.md.Headers = copiedHeaders + } else { + // Caching is allowed (or rather, not disabled), + // and the proxy error is suppressed + proxyErr = nil + } } // `cacheable` means can write to cache storage