Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(proxy-cache): follow request Cache-Control: no-cache header field #14139

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

oowl
Copy link
Member

@oowl oowl commented Jan 13, 2025

Summary

Previously, when the header field Cache-Control contained the directive no-cache, the proxy-cache plugin was not caching at all, which is not the behavior described by RFC-7234.

Checklist

  • The Pull Request has tests
  • A changelog file has been created under changelog/unreleased/kong or skip-changelog label added on PR if changelog is unnecessary. README.md
  • There is a user-facing docs PR against https://github.com/Kong/docs.konghq.com - PUT DOCS PR HERE

Issue reference

Fix #14120

KAG-6165

@github-actions github-actions bot added plugins/proxy-cache cherry-pick kong-ee schedule this PR for cherry-picking to kong/kong-ee labels Jan 13, 2025
@xianghai2 xianghai2 requested a review from locao January 13, 2025 08:38
@oowl oowl changed the title fix(proxy-cache): let request no-cache arguement functionality follow… fix(proxy-cache): let cache action follow request cache-control header no-cache field base on rfc7234 Jan 17, 2025
Copy link
Contributor

@locao locao left a comment

Choose a reason for hiding this comment

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

This fix would change the cached object format instead of only refreshing it. We keep the request body in the cache and the new code is returning before storing it.

My suggestion is to skip the fetch() call when Cache-Control: no-cache and setting the correct cache status in the signal_cache_req() call.

set_header(conf, "X-Cache-Key", cache_key)
if conf.cache_control then
if cc["no-cache"] then
return signal_cache_req(ctx, conf, cache_key, "Bypass")
Copy link
Contributor

Choose a reason for hiding this comment

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

If we return here, we're not caching the request body.

Choose a reason for hiding this comment

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

@locao ,
Just out of curiosity, why are we caching the request body? Used somewhere else?

Copy link
Contributor

Choose a reason for hiding this comment

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

@khaled4vokalz yes, it is not used directly by this plugin, but exposed in the request ctx variable so logging plugins can use it to log full request details.

Choose a reason for hiding this comment

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

I understand sharing it via the ctx but as we're using ngx.shared memory space to cache things, caching some object which is not used for anything seems a bit ambitious to me. There can be large request bodies from clients. That's the reason I asked. I was wondering if it'll be a good idea to remove the request_body from the cached object to save some space.

One more thing here is that the ctx we're using in the plugin itself is the plugin context that's not shared between plugins for use, rather it's only available in this plugin's lifecycle.

Copy link
Contributor

Choose a reason for hiding this comment

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

I was wondering if it'll be a good idea to remove the request_body from the cached object to save some space.

Removing it now is not expected by custom plugins which can use this field. It can go from just changing the behavior to break someone's logging process completely.

But if you're using Kong Enterprise Edition, you can select redis as a caching strategy :)

the ctx we're using in the plugin itself is the plugin context

Actually, we copy the cached data to the shared context when cache is hit.

@locao locao changed the title fix(proxy-cache): let cache action follow request cache-control header no-cache field base on rfc7234 fix(proxy-cache): follow request Cache-Control: no-cache header Jan 17, 2025
@locao locao changed the title fix(proxy-cache): follow request Cache-Control: no-cache header fix(proxy-cache): follow request Cache-Control: no-cache header field Jan 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cherry-pick kong-ee schedule this PR for cherry-picking to kong/kong-ee plugins/proxy-cache size/S
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cache-Control: no-cache from client request makes the response itself non-cacheable
4 participants