class Flipper::Adapters::Sync::IntervalSynchronizer
Internal: Wraps a Synchronizer
instance and only invokes it every N seconds.
Constants
- DEFAULT_INTERVAL
Private: Number of seconds between syncs (default: 10).
Attributes
interval[R]
Public: The Float or Integer number of seconds between invocations of the wrapped synchronizer.
Public Class Methods
new(synchronizer, interval: nil)
click to toggle source
Public: Initializes a new interval synchronizer.
synchronizer - The Synchronizer
to call when the interval has passed. interval - The Integer number of seconds between invocations of
the wrapped synchronizer.
# File lib/flipper/adapters/sync/interval_synchronizer.rb, line 24 def initialize(synchronizer, interval: nil) @synchronizer = synchronizer @interval = interval || DEFAULT_INTERVAL # TODO: add jitter to this so all processes booting at the same time # don't phone home at the same time. @last_sync_at = 0 end
now()
click to toggle source
Private
# File lib/flipper/adapters/sync/interval_synchronizer.rb, line 11 def self.now Process.clock_gettime(Process::CLOCK_MONOTONIC, :second) end
Public Instance Methods
call()
click to toggle source
# File lib/flipper/adapters/sync/interval_synchronizer.rb, line 32 def call return unless time_to_sync? @last_sync_at = now @synchronizer.call nil end
Private Instance Methods
now()
click to toggle source
# File lib/flipper/adapters/sync/interval_synchronizer.rb, line 48 def now self.class.now end
time_to_sync?()
click to toggle source
# File lib/flipper/adapters/sync/interval_synchronizer.rb, line 43 def time_to_sync? seconds_since_last_sync = now - @last_sync_at seconds_since_last_sync >= @interval end