Sha256: 4d3499cc5032139dd3a07e849067ff68b588287f8ee872cfc71aa825d7e6eb30

Contents?: true

Size: 1.71 KB

Versions: 17

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

module Karafka
  module Web
    module Tracking
      module Helpers
        # Namespace for time sensitive related buffers and operators
        module Ttls
          # Array that allows us to store data points that expire over time automatically.
          class Array
            include ::Karafka::Core::Helpers::Time
            include Enumerable

            # @param ttl [Integer] milliseconds ttl
            def initialize(ttl)
              @ttl = ttl
              @accu = []
            end

            # Iterates over only active elements
            def each
              clear

              @accu.each do |sample|
                yield sample[:value]
              end
            end

            # @param value [Object] adds value to the array
            # @return [Object] added element
            def <<(value)
              @accu << { value: value, added_at: monotonic_now }

              clear

              value
            end

            # @return [Boolean] is the array empty
            def empty?
              clear
              @accu.empty?
            end

            # Samples that are within our TTL time window with the times
            #
            # @return [Hash]
            def samples
              clear
              @accu
            end

            # @return [::Array] pure array version with only active elements
            def to_a
              clear
              super
            end

            private

            # Evicts outdated samples
            def clear
              @accu.delete_if do |sample|
                monotonic_now - sample[:added_at] > @ttl
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
karafka-web-0.10.4 lib/karafka/web/tracking/helpers/ttls/array.rb
karafka-web-0.10.3 lib/karafka/web/tracking/helpers/ttls/array.rb
karafka-web-0.10.2 lib/karafka/web/tracking/helpers/ttls/array.rb
karafka-web-0.10.1 lib/karafka/web/tracking/helpers/ttls/array.rb
karafka-web-0.10.0 lib/karafka/web/tracking/helpers/ttls/array.rb
karafka-web-0.10.0.rc2 lib/karafka/web/tracking/helpers/ttls/array.rb
karafka-web-0.10.0.rc1 lib/karafka/web/tracking/helpers/ttls/array.rb
karafka-web-0.10.0.beta1 lib/karafka/web/tracking/helpers/ttls/array.rb
karafka-web-0.9.1 lib/karafka/web/tracking/helpers/ttls/array.rb
karafka-web-0.9.0 lib/karafka/web/tracking/helpers/ttls/array.rb
karafka-web-0.9.0.rc3 lib/karafka/web/tracking/helpers/ttls/array.rb
karafka-web-0.9.0.rc2 lib/karafka/web/tracking/helpers/ttls/array.rb
karafka-web-0.9.0.rc1 lib/karafka/web/tracking/helpers/ttls/array.rb
karafka-web-0.8.2 lib/karafka/web/tracking/helpers/ttls/array.rb
karafka-web-0.8.1 lib/karafka/web/tracking/helpers/ttls/array.rb
karafka-web-0.8.0 lib/karafka/web/tracking/helpers/ttls/array.rb
karafka-web-0.8.0.rc1 lib/karafka/web/tracking/helpers/ttls/array.rb