Sha256: 7d7a6e6a40d7e4bbe03d6c1364bad247516149d467986e575065090703218b4c

Contents?: true

Size: 1.39 KB

Versions: 32

Compression:

Stored size: 1.39 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

        # 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 seconds 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
          Process.clock_gettime(Process::CLOCK_MONOTONIC, :second)
        end
      end
    end
  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
flipper-1.3.2 lib/flipper/adapters/sync/interval_synchronizer.rb
flipper-1.3.1 lib/flipper/adapters/sync/interval_synchronizer.rb
flipper-1.3.0 lib/flipper/adapters/sync/interval_synchronizer.rb
flipper-1.3.0.pre lib/flipper/adapters/sync/interval_synchronizer.rb
flipper-1.2.2 lib/flipper/adapters/sync/interval_synchronizer.rb
flipper-1.2.1 lib/flipper/adapters/sync/interval_synchronizer.rb
flipper-1.2.0 lib/flipper/adapters/sync/interval_synchronizer.rb
flipper-1.1.2 lib/flipper/adapters/sync/interval_synchronizer.rb
flipper-1.1.1 lib/flipper/adapters/sync/interval_synchronizer.rb
flipper-1.1.0 lib/flipper/adapters/sync/interval_synchronizer.rb
flipper-1.0.0 lib/flipper/adapters/sync/interval_synchronizer.rb
flipper-1.0.0.pre lib/flipper/adapters/sync/interval_synchronizer.rb
flipper-0.28.3 lib/flipper/adapters/sync/interval_synchronizer.rb
flipper-0.28.2 lib/flipper/adapters/sync/interval_synchronizer.rb
flipper-0.28.1 lib/flipper/adapters/sync/interval_synchronizer.rb
flipper-0.28.0 lib/flipper/adapters/sync/interval_synchronizer.rb
flipper-0.27.1 lib/flipper/adapters/sync/interval_synchronizer.rb
flipper-0.27.0 lib/flipper/adapters/sync/interval_synchronizer.rb
flipper-0.26.2 lib/flipper/adapters/sync/interval_synchronizer.rb
flipper-0.26.1 lib/flipper/adapters/sync/interval_synchronizer.rb