mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 04:01:18 +08:00
FIX: in readonly mode don't double count pages
This commit is contained in:
@ -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
|
||||
|
Reference in New Issue
Block a user