Sha256: f3e9f8cd3bc9643c79432dc878c2bee81336270aa18eeb47b9e9c8993e4d9337

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 KB

Contents

module QueueKit
  module Clients
    module RoundRobinShuffler
      include QueueKit::Instrumentable

      def self.included(klass)
        super(klass)
        klass.class_eval do
          def command_clients_size
            @clients.size
          end
        end
      end

      def client_command_with_retries(retries = 10)
        attempts = 0

        while attempts < retries
          if data = (yield client, attempts)
            return data
          end

          rotate_client
          attempts += 1
        end

        nil
      end

      def client
        @client_command_count += 1

        if @client_command_count > commands_per_client
          rotate_client
        end

        @current_client
      end

      def round_robin_from(options)
        @commands_per_client = options[:commands_per_client]
      end

      def rotate_client
        instrument "queue.rotate_client"
        @client_index ||= -1
        @client_len ||= clients.size

        @client_command_count = 0
        @client_index += 1

        if @client_index >= @client_len
          @client_index = 0
        end

        @current_client = clients[@client_index]
      end

      def commands_per_client
        @commands_per_client ||= 100
      end

      def clients
        []
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
queue_kit-0.0.10 lib/queue_kit/clients/round_robin_shuffler.rb
queue_kit-0.0.9 lib/queue_kit/clients/round_robin_shuffler.rb