Sha256: ee2ec6bb99d086ab77de46dd779da2b514db4079085d7e59581ac2fe9084ed53

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

module Net
  class TCPClient
    module Policy
      # Policy for connecting to servers in the order specified
      class Custom < Base
        def initialize(server_names, proc)
          super(server_names)
          @proc = proc
        end

        # Calls the block once for each server, with the addresses in the order returned
        # by the supplied proc.
        # The block must return a Net::TCPClient::Address instance,
        # or nil to stop trying to connect to servers
        #
        # Note:
        #   If every address fails the block will be called constantly until it returns nil.
        #
        # Example:
        #   # Returns addresses in random order but without checking if a host name has been used before
        #   policy.each_proc do |addresses, count|
        #     # Return nil after the last address has been tried so that retry logic can take over
        #     if count <= address.size
        #       addresses.sample
        #     end
        #   end
        def each(&block)
          count = 1
          while address = @proc.call(addresses, count)
            raise(ArgumentError, 'Proc must return Net::TCPClient::Address, or nil') unless address.is_a?(Net::TCPClient::Address) || address.nil?
            block.call(address)
            count += 1
          end
        end

      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
net_tcp_client-2.0.0 lib/net/tcp_client/policy/custom.rb