Sha256: a34c20a627d2707fb5c5c85593fb5dead18b241b9e5ae3093288730823884d2c

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

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

        # Calls the block once for each server, with the addresses in the order returned
        # by the supplied block.
        # 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 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 = @block.call(addresses, count))
            unless address.is_a?(Net::TCPClient::Address) || address.nil?
              raise(ArgumentError, "Proc must return Net::TCPClient::Address, or nil")
            end

            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.2.1 lib/net/tcp_client/policy/custom.rb