-
Notifications
You must be signed in to change notification settings - Fork 2.7k
feat: ai rate limiting redis support #12751
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
Open
beardnick
wants to merge
22
commits into
apache:master
Choose a base branch
from
beardnick:feature/ai-rate-limiting-redis-support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+809
−84
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
2194930
refactor: align limit-count redis implementation with limit-conn
beardnick 680af73
feat: stable tests
beardnick 75b0aa7
feat: redis test util
beardnick 14e6fce
feat: review
beardnick d5552d8
feat: review
beardnick 82323ae
feat: review
beardnick 3b8e2ad
Merge branch 'apache:master' into feature/ai-rate-limiting-redis-support
beardnick 5204105
Merge branch 'apache:master' into feature/ai-rate-limiting-redis-support
beardnick c857713
feat: delete useless file
beardnick 73737c1
feat: inline code
beardnick b303401
feat: set keepalive_pool to 0 by default in test_redis.lua
beardnick 0b76863
feat: add keepalive_pool to test_redis.lua
beardnick f86b98f
test: flush redis before each test
beardnick 706bcb7
doc: update documentation
beardnick a10b0e7
fix: lint
beardnick ac70f00
feat: rename
beardnick f9ceaeb
fix: limit in log phase
beardnick 5276e0c
feat: add license header
beardnick 7f02e0e
feat: simplify the code
beardnick a6b71af
feat: simplify the code
beardnick a716a34
feat: simplify the code
beardnick 5c409c6
feat: simplify the code
beardnick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| -- | ||
beardnick marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| -- Licensed to the Apache Software Foundation (ASF) under one or more | ||
| -- contributor license agreements. See the NOTICE file distributed with | ||
| -- this work for additional information regarding copyright ownership. | ||
| -- The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| -- (the "License"); you may not use this file except in compliance with | ||
| -- the License. You may obtain a copy of the License at | ||
| -- | ||
| -- http://www.apache.org/licenses/LICENSE-2.0 | ||
| -- | ||
| -- Unless required by applicable law or agreed to in writing, software | ||
| -- distributed under the License is distributed on an "AS IS" BASIS, | ||
| -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| -- See the License for the specific language governing permissions and | ||
| -- limitations under the License. | ||
| -- | ||
| local core = require("apisix.core") | ||
| local tostring = tostring | ||
| local tonumber = tonumber | ||
| local _M = {version = 0.1} | ||
|
|
||
| local commit_script = core.string.compress_script([=[ | ||
| assert(tonumber(ARGV[3]) >= 0, "cost must be at least 0") | ||
| local ttl = redis.call('ttl', KEYS[1]) | ||
| if ttl < 0 then | ||
| redis.call('set', KEYS[1], ARGV[1] - ARGV[3], 'EX', ARGV[2]) | ||
| return {ARGV[1] - ARGV[3], ARGV[2]} | ||
| end | ||
| return {redis.call('incrby', KEYS[1], 0 - ARGV[3]), ttl} | ||
| ]=]) | ||
|
|
||
| function _M.redis_incoming(self, red, key, commit, cost) | ||
| local limit = self.limit | ||
| local window = self.window | ||
| key = self.plugin_name .. tostring(key) | ||
|
|
||
| local requested_cost = cost or 1 | ||
| local script_cost = commit and requested_cost or 0 | ||
| local res, err = red:eval(commit_script, 1, key, limit, window, script_cost) | ||
|
|
||
| if err then | ||
| return nil, err, 0 | ||
| end | ||
|
|
||
| local stored_remaining = tonumber(res[1]) | ||
| if stored_remaining == nil then | ||
| stored_remaining = limit - script_cost | ||
| end | ||
| local ttl = tonumber(res[2]) or window | ||
|
|
||
| local remaining | ||
| if commit then | ||
| remaining = stored_remaining | ||
| else | ||
| remaining = stored_remaining - requested_cost | ||
| end | ||
|
|
||
| if remaining < 0 then | ||
| return nil, "rejected", ttl | ||
| end | ||
|
|
||
| return 0, remaining, ttl | ||
| end | ||
|
|
||
| function _M.redis_log_phase_incoming(self, red, key, cost) | ||
| local limit = self.limit | ||
| local window = self.window | ||
| key = self.plugin_name .. tostring(key) | ||
|
|
||
| local res, err = red:eval(commit_script, 1, key, limit, window, cost or 1) | ||
| if err then | ||
| return nil, err | ||
| end | ||
|
|
||
| return res[1] | ||
| end | ||
|
|
||
| return _M | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.