Sha256: 5b1e6aad553fbe16b4230f2435e6e88ac2d45595f14b6faceb43579793cff047

Contents?: true

Size: 1.74 KB

Versions: 31

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

module Karafka
  module TimeTrackers
    # Object used to keep track of time we've used running certain operations.
    #
    # @example Keep track of sleeping and stop after 3 seconds of 0.1 sleep intervals
    #   time_poll = Poll.new(3000)
    #   time_poll.start
    #
    #   until time_poll.exceeded?
    #     time_poll.start
    #     puts "I have #{time_poll.remaining.to_i}ms remaining to sleep..."
    #     sleep(0.1)
    #     time_poll.checkpoint
    #   end
    class Poll < Base
      attr_reader :remaining, :attempts

      # @param total_time [Integer] amount of milliseconds before we exceed the given time limit
      # @return [TimeTracker] time poll instance
      def initialize(total_time)
        @remaining = total_time
        @attempts = 0
        super()
      end

      # @return [Boolean] did we exceed the time limit
      def exceeded?
        @remaining <= 0
      end

      # Starts time tracking.
      def start
        @attempts += 1
        @started_at = now
      end

      # Stops time tracking of a given piece of code and updates the remaining time.
      def checkpoint
        @remaining -= (now - @started_at)
      end

      # @return [Boolean] If anything went wrong, can we retry after a backoff period or not
      #   (do we have enough time)
      def retryable?
        remaining > backoff_interval
      end

      # Sleeps for amount of time matching attempt, so we sleep more with each attempt in case of
      #   a retry.
      def backoff
        # Sleep requires seconds not ms
        sleep(backoff_interval / 1_000.0)
      end

      private

      # @return [Integer] milliseconds of the backoff time
      def backoff_interval
        100 * attempts
      end
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
karafka-2.0.13 lib/karafka/time_trackers/poll.rb
karafka-2.0.12 lib/karafka/time_trackers/poll.rb
karafka-2.0.11 lib/karafka/time_trackers/poll.rb
karafka-2.0.10 lib/karafka/time_trackers/poll.rb
karafka-2.0.9 lib/karafka/time_trackers/poll.rb
karafka-2.0.8 lib/karafka/time_trackers/poll.rb
karafka-2.0.7 lib/karafka/time_trackers/poll.rb
karafka-2.0.6 lib/karafka/time_trackers/poll.rb
karafka-2.0.5 lib/karafka/time_trackers/poll.rb
karafka-2.0.4 lib/karafka/time_trackers/poll.rb
karafka-2.0.3 lib/karafka/time_trackers/poll.rb
karafka-2.0.2 lib/karafka/time_trackers/poll.rb
karafka-2.0.1 lib/karafka/time_trackers/poll.rb
karafka-2.0.0 lib/karafka/time_trackers/poll.rb
karafka-2.0.0.rc6 lib/karafka/time_trackers/poll.rb
karafka-2.0.0.rc5 lib/karafka/time_trackers/poll.rb
karafka-2.0.0.rc4 lib/karafka/time_trackers/poll.rb
karafka-2.0.0.rc3 lib/karafka/time_trackers/poll.rb
karafka-2.0.0.rc2 lib/karafka/time_trackers/poll.rb
karafka-2.0.0.rc1 lib/karafka/time_trackers/poll.rb