FIX: in readonly mode don't double count pages

This commit is contained in:
Sam
2017-10-25 13:19:43 +11:00
parent d9a9ad3edb
commit 877b7be579
4 changed files with 53 additions and 12 deletions

View File

@ -1,3 +1,4 @@
# frozen_string_literal: true
class ApplicationRequest < ActiveRecord::Base
enum req_type: %i(http_total
http_2xx
@ -36,6 +37,12 @@ class ApplicationRequest < ActiveRecord::Base
end
end
GET_AND_RESET = <<~LUA
local val = redis.call('get', KEYS[1])
redis.call('set', KEYS[1], '0')
return val
LUA
def self.write_cache!(date = nil)
if date.nil?
write_cache!(Time.now.utc)
@ -51,20 +58,12 @@ class ApplicationRequest < ActiveRecord::Base
# for concurrent calls without double counting
req_types.each do |req_type, _|
key = redis_key(req_type, date)
val = $redis.get(key).to_i
namespaced_key = $redis.namespace_key(key)
val = $redis.eval(GET_AND_RESET, keys: [namespaced_key]).to_i
next if val == 0
new_val = $redis.incrby(key, -val).to_i
if new_val < 0
# undo and flush next time
$redis.incrby(key, val)
next
end
id = req_id(date, req_type)
where(id: id).update_all(["count = count + ?", val])
end
end