]> git.saurik.com Git - redis.git/blob - client-libraries/ruby/lib/server.rb
4fb54937fb1d7cbecb7926e78b9e76de1e95956b
[redis.git] / client-libraries / ruby / lib / server.rb
1 begin
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
8 # ruby 1.9+.
9 if defined?(JRUBY_VERSION) || (RUBY_VERSION >= '1.9')
10 require 'timeout'
11 RedisTimer = Timeout
12 else
13 require 'system_timer'
14 RedisTimer = SystemTimer
15 end
16 rescue LoadError => e
17 puts "[redis-rb] Could not load SystemTimer gem, falling back to Ruby's slower/unsafe timeout library: #{e.message}"
18 require 'timeout'
19 RedisTimer = Timeout
20 end
21
22 ##
23 # This class represents a redis server instance.
24
25 class Server
26
27 ##
28 # The host the redis server is running on.
29
30 attr_reader :host
31
32 ##
33 # The port the redis server is listening on.
34
35 attr_reader :port
36
37 ##
38 # A text status string describing the state of the server.
39
40 attr_reader :status
41
42 ##
43 # Create a new Redis::Server object for the redis instance
44 # listening on the given host and port.
45
46 def initialize(host, port = DEFAULT_PORT, timeout = 10)
47 raise ArgumentError, "No host specified" if host.nil? or host.empty?
48 raise ArgumentError, "No port specified" if port.nil? or port.to_i.zero?
49
50 @host = host
51 @port = port.to_i
52
53 @sock = nil
54 @status = 'NOT CONNECTED'
55 @timeout = timeout
56 end
57
58 ##
59 # Return a string representation of the server object.
60 def inspect
61 "<Redis::Server: %s:%d (%s)>" % [@host, @port, @status]
62 end
63
64 ##
65 # Try to connect to the redis server targeted by this object.
66 # Returns the connected socket object on success or nil on failure.
67
68 def socket
69 return @sock if socket_alive?
70 close
71 # Attempt to connect if not already connected.
72 begin
73 @sock = connect_to(@host, @port, @timeout)
74 @sock.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1
75 @status = 'CONNECTED'
76 rescue Errno::EPIPE, Errno::ECONNREFUSED => e
77 puts "Socket died... : #{e}\n" if $debug
78 retry
79 rescue SocketError, SystemCallError, IOError => err
80 puts "Unable to open socket: #{err.class.name}, #{err.message}" if $debug
81 end
82 @sock
83 end
84
85 def connect_to(host, port, timeout=nil)
86 socket = TCPSocket.new(host, port)
87 socket.set_encoding(Encoding::BINARY) if socket.respond_to?(:set_encoding)
88 if timeout
89 socket.instance_eval <<-EOR
90 alias :blocking_readline :readline
91 def readline(*args)
92 RedisTimer.timeout(#{timeout}) do
93 self.blocking_readline(*args)
94 end
95 end
96 alias :blocking_read :read
97 def read(*args)
98 RedisTimer.timeout(#{timeout}) do
99 self.blocking_read(*args)
100 end
101 end
102 alias :blocking_write :write
103 def write(*args)
104 RedisTimer.timeout(#{timeout}) do
105 self.blocking_write(*args)
106 end
107 end
108 EOR
109 end
110 socket
111 end
112
113 # Close the connection to the redis server targeted by this
114 # object.
115
116 def close
117 @sock.close if !@sock.nil? && !@sock.closed?
118 @sock = nil
119 @status = "NOT CONNECTED"
120 end
121
122 private
123 def socket_alive?
124 #BTM - TODO - FileStat is borked under JRuby
125 unless defined?(JRUBY_VERSION)
126 !@sock.nil? && !@sock.closed? && @sock.stat.readable?
127 else
128 !@sock.nil? && !@sock.closed?
129 end
130 end
131 end