Skip to content

Commit afd055d

Browse files
committed
feat: Introduce flag constants and enable conditional caching of error responses using FlagOn.
1 parent ce4d992 commit afd055d

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

internal/constants/global.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,9 @@ const (
1616
InternalCacheErrCode = "i-x-ct-code"
1717
InternalUpstreamAddr = "i-x-ups-addr"
1818
)
19+
20+
// define flag constants
21+
const (
22+
FlagOn = "1" // gateway control flag ON
23+
FlagOff = "0" // gateway control flag OFF
24+
)

server/middleware/caching/caching.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -387,13 +387,21 @@ func (c *Caching) doProxy(req *http.Request, subRequest bool) (*http.Response, e
387387
c.md.Size = respRange.ObjSize
388388

389389
// error code cache feature.
390-
if statusCode >= http.StatusBadRequest &&
391-
resp.Header.Get(constants.InternalCacheErrCode) != "1" {
392-
c.cacheable = false // 禁止缓存错误码缓存
393-
394-
copiedHeaders := make(http.Header)
395-
xhttp.CopyHeader(copiedHeaders, resp.Header)
396-
c.md.Headers = copiedHeaders
390+
if statusCode >= http.StatusBadRequest {
391+
392+
// Caching is disabled
393+
// restoring the default behavior for error codes.
394+
if resp.Header.Get(constants.InternalCacheErrCode) != constants.FlagOn {
395+
c.cacheable = false
396+
397+
copiedHeaders := make(http.Header)
398+
xhttp.CopyHeader(copiedHeaders, resp.Header)
399+
c.md.Headers = copiedHeaders
400+
} else {
401+
// Caching is allowed (or rather, not disabled),
402+
// and the proxy error is suppressed
403+
proxyErr = nil
404+
}
397405
}
398406

399407
// `cacheable` means can write to cache storage

0 commit comments

Comments
 (0)