don't reserve capacity for blacklist

Change-Id: I90f224a03228977cd5a3ea1b130536f7fb24ad3d
Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/246242
Reviewed-by: Cody Cutrer <cody@instructure.com>
QA-Review: Cody Cutrer <cody@instructure.com>
Product-Review: Cody Cutrer <cody@instructure.com>
Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com>
This commit is contained in:
Ethan Vizitei 2020-08-27 13:15:39 -05:00 committed by Cody Cutrer
parent 05e03dbe93
commit c79f946e83
2 changed files with 7 additions and 6 deletions

View File

@ -55,7 +55,8 @@ class RequestThrottle
bucket = LeakyBucket.new(client_identifier(request))
up_front_cost = bucket.get_up_front_cost_for_path(path)
cost = bucket.reserve_capacity(up_front_cost, request_whitelisted: whitelisted?(request)) do
pre_judged = (whitelisted?(request) || blacklisted?(request))
cost = bucket.reserve_capacity(up_front_cost, request_prejudged: pre_judged) do
status, headers, response = if !allowed?(request, bucket)
throttled = true
rate_limit_exceeded
@ -315,11 +316,11 @@ class RequestThrottle
# data out of redis at the same time. It then yields to the block,
# expecting the block to return the final cost. It then increments again,
# subtracting the initial up_front_cost from the final cost to erase it.
def reserve_capacity(up_front_cost = self.up_front_cost, request_whitelisted: false)
increment(0, up_front_cost) unless request_whitelisted
def reserve_capacity(up_front_cost = self.up_front_cost, request_prejudged: false)
increment(0, up_front_cost) unless request_prejudged
cost = yield
ensure
increment(cost || 0, -up_front_cost) unless request_whitelisted
increment(cost || 0, -up_front_cost) unless request_prejudged
end
def full?

View File

@ -375,10 +375,10 @@ describe 'RequestThrottle' do
end
end
it "does no reserving if whitelisted" do
it "does no reserving if status decided" do
Timecop.freeze('2012-01-29 12:00:00 UTC') do
@bucket.increment(0, 0, @current_time)
@bucket.reserve_capacity(20, request_whitelisted: true) do
@bucket.reserve_capacity(20, request_prejudged: true) do
expect(@bucket.redis.hget(@bucket.cache_key, 'count').to_f).to be_within(0.1).of(0)
end
expect(@bucket.redis.hget(@bucket.cache_key, 'count').to_f).to be_within(0.1).of(0)