Sha256: 62b8aa207022f94a0bac09df0640c53ae610e8390bf054e251f118432d2c4426

Contents?: true

Size: 1.47 KB

Versions: 13

Compression:

Stored size: 1.47 KB

Contents

module Flipper
  module Adapters
    class Sync
      # Internal: Wraps a Synchronizer instance and only invokes it every
      # N seconds.
      class IntervalSynchronizer
        # Private: Number of seconds between syncs (default: 10).
        DEFAULT_INTERVAL = 10

        # Private
        def self.now
          Process.clock_gettime(Process::CLOCK_MONOTONIC, :second)
        end

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

        # 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.
        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

        def call
          return unless time_to_sync?

          @last_sync_at = now
          @synchronizer.call

          nil
        end

        private

        def time_to_sync?
          seconds_since_last_sync = now - @last_sync_at
          seconds_since_last_sync >= @interval
        end

        def now
          self.class.now
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
flipper-0.20.1 lib/flipper/adapters/sync/interval_synchronizer.rb
flipper-0.20.0 lib/flipper/adapters/sync/interval_synchronizer.rb
flipper-0.20.0.beta3 lib/flipper/adapters/sync/interval_synchronizer.rb
flipper-0.20.0.beta2 lib/flipper/adapters/sync/interval_synchronizer.rb
flipper-0.20.0.beta1 lib/flipper/adapters/sync/interval_synchronizer.rb
flipper-0.19.1 lib/flipper/adapters/sync/interval_synchronizer.rb
flipper-0.19.0 lib/flipper/adapters/sync/interval_synchronizer.rb
flipper-0.18.0 lib/flipper/adapters/sync/interval_synchronizer.rb
flipper-0.17.2 lib/flipper/adapters/sync/interval_synchronizer.rb
flipper-0.17.1 lib/flipper/adapters/sync/interval_synchronizer.rb
flipper-0.16.2 lib/flipper/adapters/sync/interval_synchronizer.rb
flipper-0.16.1 lib/flipper/adapters/sync/interval_synchronizer.rb
flipper-0.16.0 lib/flipper/adapters/sync/interval_synchronizer.rb