PERF: Throttle updates to API key last_used_at (#16390)

Co-authored-by: David Taylor <david@taylorhq.com>
This commit is contained in:
Rafael dos Santos Silva 2022-04-06 11:01:52 -03:00 committed by GitHub
parent 68c74e9b93
commit 1203121ac1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 1 deletions

View File

@ -67,6 +67,13 @@ class ApiKey < ActiveRecord::Base
api_key_scopes.blank? || api_key_scopes.any? { |s| s.permits?(env) }
end
def update_last_used!(now = Time.zone.now)
return if last_used_at && (last_used_at > 1.minute.ago)
# using update_column to avoid the AR transaction
update_column(:last_used_at, now)
end
end
# == Schema Information

View File

@ -385,7 +385,9 @@ class Auth::DefaultCurrentUserProvider
end
if user && can_write?
api_key.update_columns(last_used_at: Time.zone.now)
Scheduler::Defer.later "Updating api_key last_used" do
api_key.update_last_used!
end
end
user