Sha256: 814cbafb7114daf94ed1ca043415b36d53679c3de57c704f9421bdf95077cd99
Contents?: true
Size: 1.3 KB
Versions: 27
Compression:
Stored size: 1.3 KB
Contents
# frozen_string_literal: true module Karafka module Helpers # Object responsible for running given code with a given interval. It won't run given code # more often than with a given interval. # # This allows us to execute certain code only once in a while. # # This can be used when we have code that could be invoked often due to it being in loops # or other places but would only slow things down if would run with each tick. class IntervalRunner include Karafka::Core::Helpers::Time # @param interval [Integer] interval in ms for running the provided code. Defaults to the # `internal.tick_interval` value # @param block [Proc] block of code we want to run once in a while def initialize(interval: ::Karafka::App.config.internal.tick_interval, &block) @block = block @interval = interval @last_called_at = monotonic_now - @interval end # Runs the requested code if it was not executed previously recently def call return if monotonic_now - @last_called_at < @interval @last_called_at = monotonic_now @block.call end # Resets the runner, so next `#call` will run the underlying code def reset @last_called_at = monotonic_now - @interval end end end end
Version data entries
27 entries across 27 versions & 1 rubygems