Sha256: ca359848d0029413febd4fa1cea623ad018f547a65c28f75b938979da0e10d85

Contents?: true

Size: 1.65 KB

Versions: 9

Compression:

Stored size: 1.65 KB

Contents

module SplitIoClient
  module Cache
    module Repositories
      module Impressions
        class MemoryRepository
          def initialize(adapter, config)
            @adapter = adapter
            @config = config
          end

          # Store impression data in the selected adapter
          def add(split_name, data)
            @adapter.add_to_queue(feature: split_name, impressions: data)
          rescue ThreadError # queue is full
            if random_sampler.rand(1..1000) <= 2 # log only 0.2 % of the time
              @config.logger.warn("Dropping impressions. Current size is #{@config.impressions_queue_size}. " \
                                  "Consider increasing impressions_queue_size")
            end
          end

          def add_bulk(key, bucketing_key, treatments_labels_change_numbers, time)
            treatments_labels_change_numbers.each do |split_name, treatment_label_number|
              add(
                split_name,
                'key_name' => key,
                'bucketing_key' => bucketing_key,
                'treatment' => treatment_label_number[:treatment],
                'label' => @config.labels_enabled ? treatment_label_number[:label] : nil,
                'change_number' => treatment_label_number[:change_number],
                'time' => time
              )
            end
          end

          # Get everything from the queue and leave it empty
          def clear
            @adapter.clear.map { |impression| impression.update(ip: @config.machine_ip) }
          end

          private

          def random_sampler
            @random_sampler ||= Random.new
          end
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
splitclient-rb-3.2.4.pre.rc4 lib/cache/repositories/impressions/memory_repository.rb
splitclient-rb-3.2.4.pre.rc3 lib/cache/repositories/impressions/memory_repository.rb
splitclient-rb-3.2.4.pre.rc2 lib/cache/repositories/impressions/memory_repository.rb
splitclient-rb-3.2.4.pre.rc1 lib/cache/repositories/impressions/memory_repository.rb
splitclient-rb-3.2.3 lib/cache/repositories/impressions/memory_repository.rb
splitclient-rb-3.2.3.pre.rc1 lib/cache/repositories/impressions/memory_repository.rb
splitclient-rb-3.2.2 lib/cache/repositories/impressions/memory_repository.rb
splitclient-rb-3.2.1 lib/cache/repositories/impressions/memory_repository.rb
splitclient-rb-3.2.0 lib/cache/repositories/impressions/memory_repository.rb