Class: ChatName

Inherits:
ApplicationRecord show all
Defined in:
app/models/chat_name.rb

Constant Summary collapse

LAST_USED_AT_INTERVAL =
1.hour

Instance Method Summary collapse

Methods inherited from ApplicationRecord

at_most, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, underscore, without_order

Instance Method Details

#update_last_used_atObject

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.


22
23
24
25
26
27
28
29
30
# File 'app/models/chat_name.rb', line 22

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

#update_last_used_at?Boolean

Returns:

  • (Boolean)

32
33
34
# File 'app/models/chat_name.rb', line 32

def update_last_used_at?
  last_used_at.nil? || last_used_at > LAST_USED_AT_INTERVAL.ago
end