Skip to content

feat: Introduce flag constants and enable conditional caching of erro…#39

Merged
sendya merged 1 commit intomainfrom
fix/cache-errcode-with-refresh
Feb 4, 2026
Merged

feat: Introduce flag constants and enable conditional caching of erro…#39
sendya merged 1 commit intomainfrom
fix/cache-errcode-with-refresh

Conversation

@sendya
Copy link
Member

@sendya sendya commented Feb 4, 2026

This pull request introduces a new mechanism for controlling error code caching behavior in the gateway by defining explicit flag constants and updating the error code caching logic to use these flags. The main focus is to make the enabling or disabling of error code caching more explicit and maintainable.

Key changes include:

Gateway control flags

  • Added new flag constants FlagOn and FlagOff in internal/constants/global.go to explicitly represent gateway control states for features such as error code caching.

Error code caching logic

  • Updated the error code caching logic in server/middleware/caching/caching.go to use the new FlagOn constant for checking if error code caching is enabled, improving clarity and making the feature toggle more maintainable. The logic now also allows error responses to be cached if the header is set to FlagOn, and suppresses proxy errors in that scenario.

Copilot AI review requested due to automatic review settings February 4, 2026 13:21
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces explicit flag constants to represent gateway control states and updates the gateway’s error-response caching logic to use those flags, making the toggle more explicit and maintainable.

Changes:

  • Added FlagOn / FlagOff constants in internal/constants/global.go.
  • Updated error status (4xx/5xx) caching behavior in server/middleware/caching/caching.go to check InternalCacheErrCode against FlagOn and suppress proxyErr when enabled.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
server/middleware/caching/caching.go Switches error-code caching toggle to constants.FlagOn and adds logic to suppress proxy errors when error caching is enabled.
internal/constants/global.go Introduces shared on/off flag constants intended for gateway feature toggles.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

} else {
// Caching is allowed (or rather, not disabled),
// and the proxy error is suppressed
proxyErr = nil
Copy link

Copilot AI Feb 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When InternalCacheErrCode == FlagOn, this branch suppresses proxyErr but does not update c.md.Headers. For subRequest == true and c.fileChanged == false, the later header sync (if c.fileChanged || !subRequest) will be skipped, so the metadata can keep stale/empty headers while c.md.Code is set to an error. That can cause cached error responses to be served with incorrect headers/content-length.

Consider always copying resp.Header into c.md.Headers when caching an error response is allowed (ideally replacing the map to avoid stale keys), even for sub-requests.

Suggested change
proxyErr = nil
proxyErr = nil
// Ensure metadata headers are updated for cached error responses.
copiedHeaders := make(http.Header)
xhttp.CopyHeader(copiedHeaders, resp.Header)
c.md.Headers = copiedHeaders

Copilot uses AI. Check for mistakes.
Comment on lines +22 to +23
FlagOn = "1" // gateway control flag ON
FlagOff = "0" // gateway control flag OFF
Copy link

Copilot AI Feb 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FlagOn / FlagOff are very generic exported names in a shared constants package, which makes it unclear what they apply to and increases the chance of future collisions/ambiguity. Consider using more specific names (e.g., GatewayFlagOn/GatewayFlagOff or ControlFlagOn/ControlFlagOff, or even feature-scoped constants like CacheErrCodeFlagOn).

Suggested change
FlagOn = "1" // gateway control flag ON
FlagOff = "0" // gateway control flag OFF
// GatewayControlFlagOn represents the "ON" value for gateway control flags.
GatewayControlFlagOn = "1"
// GatewayControlFlagOff represents the "OFF" value for gateway control flags.
GatewayControlFlagOff = "0"
// Deprecated: use GatewayControlFlagOn instead.
FlagOn = GatewayControlFlagOn
// Deprecated: use GatewayControlFlagOff instead.
FlagOff = GatewayControlFlagOff

Copilot uses AI. Check for mistakes.
@sendya sendya merged commit afd055d into main Feb 4, 2026
7 checks passed
@sendya sendya deleted the fix/cache-errcode-with-refresh branch February 12, 2026 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant