Class: Flipper::Adapters::Sync::IntervalSynchronizer

Inherits:
Object
  • Object
show all
Defined in:
lib/flipper/adapters/sync/interval_synchronizer.rb

Overview

Internal: Wraps a Synchronizer instance and only invokes it every N seconds.

Constant Summary collapse

DEFAULT_INTERVAL =

Private: Number of seconds between syncs (default: 10).

10

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(synchronizer, interval: nil) ⇒ IntervalSynchronizer

Public: Initializes a new interval synchronizer.

synchronizer - The Synchronizer to call when the interval has passed. interval - The Integer number of milliseconds between invocations of

the wrapped synchronizer.


24
25
26
27
28
29
30
# 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

Instance Attribute Details

#intervalObject (readonly)

Public: The Float or Integer number of seconds between invocations of the wrapped synchronizer.



17
18
19
# File 'lib/flipper/adapters/sync/interval_synchronizer.rb', line 17

def interval
  @interval
end

Class Method Details

.now_msObject

Private



11
12
13
# File 'lib/flipper/adapters/sync/interval_synchronizer.rb', line 11

def self.now_ms
  Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
end

Instance Method Details

#callObject



32
33
34
35
36
37
38
39
# File 'lib/flipper/adapters/sync/interval_synchronizer.rb', line 32

def call
  return unless time_to_sync?

  @last_sync_at = now_ms
  @synchronizer.call

  nil
end