Method: ResilientSocket::TCPClient#initialize
- Defined in:
- lib/resilient_socket/tcp_client.rb
#initialize(parameters = {}) ⇒ TCPClient
Create a new TCP Client connection
Parameters:
:server [String]
URL of the server to connect to with port number
'localhost:2000'
:servers [Array of String]
Array of URL's of servers to connect to with port numbers
['server1:2000', 'server2:2000']
The second server will only be attempted once the first server
cannot be connected to or has timed out on connect
A read failure or timeout will not result in switching to the second
server, only a connection failure or during an automatic reconnect
:read_timeout [Float]
Time in seconds to timeout on read
Can be overridden by supplying a timeout in the read call
Default: 60
:connect_timeout [Float]
Time in seconds to timeout when trying to connect to the server
A value of -1 will cause the connect wait time to be infinite
Default: Half of the :read_timeout ( 30 seconds )
:log_level [Symbol]
Set the logging level for the TCPClient
Any valid SemanticLogger log level:
:trace, :debug, :info, :warn, :error, :fatal
Default: SemanticLogger.default_level
:buffered [Boolean]
Whether to use Nagle's Buffering algorithm (http://en.wikipedia.org/wiki/Nagle's_algorithm)
Recommend disabling for RPC style invocations where we don't want to wait for an
ACK from the server before sending the last partial segment
Buffering is recommended in a browser or file transfer style environment
where multiple sends are expected during a single response
Default: true
:connect_retry_count [Fixnum]
Number of times to retry connecting when a connection fails
Default: 10
:connect_retry_interval [Float]
Number of seconds between connection retry attempts after the first failed attempt
Default: 0.5
:retry_count [Fixnum]
Number of times to retry when calling #retry_on_connection_failure
This is independent of :connect_retry_count which still applies with
connection failures. This retry controls upto how many times to retry the
supplied block should a connection failure occurr during the block
Default: 3
:on_connect [Proc]
Directly after a connection is established and before it is made available
for use this Block is invoked.
Typical Use Cases:
- Initialize per connection session sequence numbers
- Pass any authentication information to the server
- Perform a handshake with the server
:server_selector [Symbol|Proc]
When multiple servers are supplied using :servers, this option will
determine which server is selected from the list
:ordered
Select a server in the order supplied in the array, with the first
having the highest priority. The second server will only be connected
to if the first server is unreachable
:random
Randomly select a server from the list every time a connection
is established, including during automatic connection recovery.
:nearest
FUTURE - Not implemented yet
The server with an IP address that most closely matches the
local ip address will be attempted first
This will result in connections to servers on the localhost
first prior to looking at remote servers
:ping_time
FUTURE - Not implemented yet
The server with the lowest ping time will be selected first
Proc:
When a Proc is supplied, it will be called passing in the list
of servers. The Proc must return one server name
Example:
:server_selector => Proc.new do |servers|
servers.last
end
Default: :ordered
:close_on_error [True|False]
To prevent the connection from going into an inconsistent state
automatically close the connection if an error occurs
This includes a Read Timeout
Default: true
Example
client = ResilientSocket::TCPClient.new(
:server => 'server:3300',
:connect_retry_interval => 0.1,
:connect_retry_count => 5
)
client.retry_on_connection_failure do
client.send('Update the database')
end
# Read upto 20 characters from the server
response = client.read(20)
puts "Received: #{response}"
client.close
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/resilient_socket/tcp_client.rb', line 219 def initialize(parameters={}) params = parameters.dup @read_timeout = (params.delete(:read_timeout) || 60.0).to_f @connect_timeout = (params.delete(:connect_timeout) || (@read_timeout/2)).to_f buffered = params.delete(:buffered) @buffered = buffered.nil? ? true : buffered @connect_retry_count = params.delete(:connect_retry_count) || 10 @retry_count = params.delete(:retry_count) || 3 @connect_retry_interval = (params.delete(:connect_retry_interval) || 0.5).to_f @on_connect = params.delete(:on_connect) @server_selector = params.delete(:server_selector) || :ordered @close_on_error = params.delete(:close_on_error) @close_on_error = true if @close_on_error.nil? unless @servers = params.delete(:servers) raise "Missing mandatory :server or :servers" unless server = params.delete(:server) @servers = [ server ] end self.logger = SemanticLogger::Logger.new("#{self.class.name} #{@servers.inspect}", params.delete(:log_level)) params.each_pair {|k,v| logger.warn "Ignoring unknown option #{k} = #{v}"} # Connect to the Server connect end |