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

Add 'allow' mode in the caching policy #558

Merged
merged 5 commits into from
Jan 22, 2018
Merged

Conversation

davidor
Copy link
Contributor

@davidor davidor commented Jan 19, 2018

This mode caches authorized and denied calls. When backend is unavailable, it will cache an authorization. In practice, this means that when backend is down any request will be authorized unless last call to backend for that request returned 'deny' (status code = 4xx).

Closes #126

@davidor davidor requested a review from mikz January 19, 2018 13:50
@davidor davidor force-pushed the cache-policy-allow-mode branch from 5d1c046 to 0a70362 Compare January 19, 2018 13:55
ngx.log(ngx.WARN, 'Backend seems to be unavailable. "Allow" mode is ',
'enabled in the cache policy, so next request will be ',
'authorized')
cache:set(cached_key, 200, ttl)
Copy link
Contributor

Choose a reason for hiding this comment

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

What about the race condition between read and write?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. Unfortunately, I don't see any 'compare-and-set' operation in the API of ngx.shared.dict: https://github.com/openresty/lua-nginx-module#ngxshareddict

I see that there's this library: https://github.com/openresty/lua-resty-lock but that would mean introducing a dependency just for this policy.

Copy link
Contributor

Choose a reason for hiding this comment

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

@davidor lua-resty-lock is part of the openresty bundle, so it is part of stdlib.

I think we could take some time and think about how to do this without the lock anyway. Possibly using the add operation. If the stdict value would be nil, we could add 200.
Not sure how exactly that should work, but I see it as the only option without using the lock because it is the only compare-and-set atomic (compare to nil) operation on shdict.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that add would work in this case. There are only 3 possibilities:

  • If there was a 2XX, we don't need to overwrite it with the same thing.
  • If there was a 4XX, we don't need to write anything.
  • If it was nil, we need to write a 200.

Let me try this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Solved in the new commit @mikz

Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@davidor davidor force-pushed the cache-policy-allow-mode branch from b983013 to a1837ff Compare January 22, 2018 10:57
@@ -38,13 +44,37 @@ local function resilient_handler(cache, cached_key, response, ttl)
end
end

local function handle_500_allow_mode(cache, cached_key, ttl)
Copy link
Contributor

Choose a reason for hiding this comment

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

Does it warrant own function now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't have a strong preference. I'm inclined to say yes because of the long comment.

@@ -5,7 +5,7 @@
"properties": {
"exit": {
"type": "caching_type",
"enum": ["resilient", "strict", "none"]
"enum": ["resilient", "strict", "allow", "none"]
Copy link
Contributor

Choose a reason for hiding this comment

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

No need to change anything, but I wonder if there is a way to describe each individual enum value so we can show it nicely in the form.

Copy link
Contributor

@mikz mikz left a comment

Choose a reason for hiding this comment

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

👍

@mikz mikz merged commit 6049ab3 into master Jan 22, 2018
@mikz mikz deleted the cache-policy-allow-mode branch January 22, 2018 11:39
@mayorova
Copy link
Contributor

mayorova commented Feb 2, 2018

Does this replace APICAST_BACKEND_CACHE_HANDLER?

@davidor
Copy link
Contributor Author

davidor commented Feb 2, 2018

@mayorova this does not break compatibility. If this cache policy is not included, users can still use APICAST_BACKEND_CACHE_HANDLER.

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.

3 participants