]>
git.saurik.com Git - redis.git/blob - client-libraries/ruby/lib/server.rb
2 # Timeout code is courtesy of Ruby memcache-client
3 # http://github.com/mperham/memcache-client/tree
4 # Try to use the SystemTimer gem instead of Ruby's timeout library
5 # when running on something that looks like Ruby 1.8.x. See:
6 # http://ph7spot.com/articles/system_timer
7 # We don't want to bother trying to load SystemTimer on jruby and
9 if defined?(JRUBY_VERSION
) || (RUBY_VERSION >= '1.9')
13 require 'system_timer'
14 RedisTimer
= SystemTimer
17 puts
"[redis-rb] Could not load SystemTimer gem, falling back to Ruby's slower/unsafe timeout library: #{e.message}"
23 # This class represents a redis server instance.
28 # The amount of time to wait before attempting to re-establish a
29 # connection with a server that is marked dead.
34 # The host the redis server is running on.
39 # The port the redis server is listening on.
49 # The time of next retry if the connection is dead.
54 # A text status string describing the state of the server.
59 # Create a new Redis::Server object for the redis instance
60 # listening on the given host and port.
62 def initialize(host
, port
= DEFAULT_PORT
, timeout
= 10)
63 raise ArgumentError
, "No host specified" if host
.nil? or host
.empty
?
64 raise ArgumentError
, "No port specified" if port
.nil? or port
.to_i
.zero
?
71 @status = 'NOT CONNECTED'
76 # Return a string representation of the server object.
78 "<Redis::Server: %s:%d (%s)>" % [@host, @port, @status]
82 # Try to connect to the redis server targeted by this object.
83 # Returns the connected socket object on success or nil on failure.
86 return @sock if @sock and not @sock.closed
?
90 # If the host was dead, don't retry for a while.
91 return if @retry and @retry > Time
.now
93 # Attempt to connect if not already connected.
95 @sock = connect_to(@host, @port, @timeout)
96 @sock.setsockopt Socket
::IPPROTO_TCP, Socket
::TCP_NODELAY, 1
99 rescue Errno
::EPIPE, Errno
::ECONNREFUSED => e
100 puts
"Socket died... socket: #{@sock.inspect}\n" if $debug
103 rescue SocketError
, SystemCallError
, IOError
=> err
104 puts
"Unable to open socket: #{err.class.name}, #{err.message}" if $debug
110 def connect_to(host
, port
, timeout
=nil)
111 socket
= TCPSocket
.new(host
, port
, 0)
113 socket
.instance_eval
<<-EOR
114 alias :blocking_gets :gets
116 RedisTimer.timeout(#{timeout}) do
117 self.blocking_gets(*args)
120 alias :blocking_read :read
122 RedisTimer.timeout(#{timeout}) do
123 self.blocking_read(*args)
126 alias :blocking_write :write
128 RedisTimer.timeout(#{timeout}) do
129 self.blocking_write(*args)
138 # Close the connection to the redis server targeted by this
139 # object. The server is not considered dead.
142 @sock.close
if @sock && !
@sock.closed
?
145 @status = "NOT CONNECTED"
149 # Mark the server as dead and close its socket.
151 @sock.close
if @sock && !
@sock.closed
?
153 @retry = Time
.now
#+ RETRY_DELAY
155 reason
= "#{error.class.name}: #{error.message}"
156 @status = sprintf
"%s:%s DEAD (%s), will retry at %s", @host, @port, reason
, @retry