Sha256: 5b991dee10f5680be914155acdd532d212b52b8ef4956ec6b4271af373888f74

Contents?: true

Size: 1.49 KB

Versions: 3

Compression:

Stored size: 1.49 KB

Contents

module LiveQA
  module Processor
    class Batch

      attr_reader :messages

      MAX_BYTES = 204_800 # 200Kb
      MAX_MESSAGES = 100
      MAX_MESSAGE_BYTES = 32_768 # 32Kb

      DEFAULT_RETRY_TIME = 60

      MAX_RETRY = 10
      RETRY_MAP = {
        1 => 2,
        2 => 5,
        3 => 10,
        4 => 20,
        5 => 30,
        6 => 30,
        7 => 30,
        8 => 30,
        9 => 30,
        10 => 30,
      }.freeze

      def initialize
        @retry_count = 0
        @total_bytes = 0
        @messages = []
      end

      def <<(message)
        message_json_size = message.to_json.bytesize

        if max_message_reached?(message_json_size)
          LiveQA.configurations.logger.error('Message is too big to be send')
          return false
        end

        @total_bytes += message_json_size

        @messages << message

        true
      end

      def empty?
        @messages.size.zero?
      end

      def full?
        max_messages_reached? || max_size_reached?
      end

      def next_retry
        RETRY_MAP[@retry_count] || DEFAULT_RETRY_TIME
      end

      def update_retry
        @retry_count += 1
      end

      def can_retry?
        @retry_count < MAX_RETRY
      end

      private

      def max_messages_reached?
        @messages.length >= MAX_MESSAGES
      end

      def max_size_reached?
        @total_bytes >= MAX_BYTES
      end

      def max_message_reached?(message_json_size)
        message_json_size > MAX_MESSAGE_BYTES
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
liveqa-1.9.6 lib/liveqa/processor/batch.rb
liveqa-1.9.5 lib/liveqa/processor/batch.rb
liveqa-1.9.4 lib/liveqa/processor/batch.rb