class Net::TCPClient::Policy::Custom

Policy for connecting to servers in the order specified

Public Class Methods

new(server_names, block) click to toggle source
Calls superclass method Net::TCPClient::Policy::Base::new
# File lib/net/tcp_client/policy/custom.rb, line 6
def initialize(server_names, block)
  super(server_names)
  @block = block
end

Public Instance Methods

each(&block) click to toggle source

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
# File lib/net/tcp_client/policy/custom.rb, line 27
def each(&block)
  count = 1
  while address = @block.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