summaryrefslogtreecommitdiff
path: root/app/models/chat_name.rb
diff options
context:
space:
mode:
authorClement Ho <clemmakesapps@gmail.com>2018-03-01 22:33:29 +0000
committerClement Ho <clemmakesapps@gmail.com>2018-03-01 22:33:29 +0000
commitb112a33b98c657b1d2838c14d598a291a14565e0 (patch)
treea37cc22491baf419a15b81918ee07c2fd0d8f2da /app/models/chat_name.rb
parent4441ca4ba7bf6c4a68574d018d2bf48e45326654 (diff)
parent5c4eace67f188da436b3b380a0125d053b29422a (diff)
downloadgitlab-ce-sentiment-analysis.tar.gz
Merge branch 'master' into 'sentiment-analysis'sentiment-analysis
# Conflicts: # app/assets/javascripts/notes/components/comment_form.vue
Diffstat (limited to 'app/models/chat_name.rb')
-rw-r--r--app/models/chat_name.rb21
1 files changed, 21 insertions, 0 deletions
diff --git a/app/models/chat_name.rb b/app/models/chat_name.rb
index f321db75eeb..fbd0f123341 100644
--- a/app/models/chat_name.rb
+++ b/app/models/chat_name.rb
@@ -1,4 +1,6 @@
class ChatName < ActiveRecord::Base
+ LAST_USED_AT_INTERVAL = 1.hour
+
belongs_to :service
belongs_to :user
@@ -9,4 +11,23 @@ class ChatName < ActiveRecord::Base
validates :user_id, uniqueness: { scope: [:service_id] }
validates :chat_id, uniqueness: { scope: [:service_id, :team_id] }
+
+ # Updates the "last_used_timestamp" but only if it wasn't already updated
+ # recently.
+ #
+ # The throttling this method uses is put in place to ensure that high chat
+ # traffic doesn't result in many UPDATE queries being performed.
+ def update_last_used_at
+ return unless update_last_used_at?
+
+ obtained = Gitlab::ExclusiveLease
+ .new("chat_name/last_used_at/#{id}", timeout: LAST_USED_AT_INTERVAL.to_i)
+ .try_obtain
+
+ touch(:last_used_at) if obtained
+ end
+
+ def update_last_used_at?
+ last_used_at.nil? || last_used_at > LAST_USED_AT_INTERVAL.ago
+ end
end