Module: Legion::Extensions::Tick::Helpers::Jitter

Defined in:
lib/legion/extensions/tick/helpers/jitter.rb

Constant Summary collapse

MAX_JITTER_CAP =

seconds (15 minutes)

900

Class Method Summary collapse

Class Method Details

.deterministic_jitter(task_name, interval_seconds) ⇒ Object

Returns a deterministic integer jitter offset (seconds) for the given task name and interval. The offset is in the range [0, max_jitter) where max_jitter is 10% of the interval, capped at MAX_JITTER_CAP (15 minutes).

The same task_name always produces the same offset, so all nodes handling the same named task will sleep the same initial amount — preventing thundering herd while keeping execution predictable.



19
20
21
22
23
24
# File 'lib/legion/extensions/tick/helpers/jitter.rb', line 19

def deterministic_jitter(task_name, interval_seconds)
  max_jitter = [interval_seconds * 0.1, MAX_JITTER_CAP].min.to_i
  return 0 if max_jitter < 1

  task_name.to_s.hash.abs % max_jitter
end

.jitter_enabled?Boolean

Returns true when jitter is enabled via settings (default: true).

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/legion/extensions/tick/helpers/jitter.rb', line 27

def jitter_enabled?
  setting = begin
    Legion::Settings[:tick]
  rescue StandardError => _e
    nil
  end
  return true if setting.nil?

  tick_hash = setting.is_a?(Hash) ? setting : nil
  return true if tick_hash.nil?

  tick_hash.fetch(:jitter_enabled, true)
end