]> git.saurik.com Git - redis.git/blame - client-libraries/ruby/lib/server.rb
Ruby client updated
[redis.git] / client-libraries / ruby / lib / server.rb
CommitLineData
29fac617 1##
2# This class represents a redis server instance.
3
4class Server
5
6 ##
7 # The amount of time to wait before attempting to re-establish a
8 # connection with a server that is marked dead.
9
10 RETRY_DELAY = 30.0
11
12 ##
13 # The host the redis server is running on.
14
15 attr_reader :host
16
17 ##
18 # The port the redis server is listening on.
19
20 attr_reader :port
21
22 ##
23 #
24
25 attr_reader :replica
26
27 ##
28 # The time of next retry if the connection is dead.
29
30 attr_reader :retry
31
32 ##
33 # A text status string describing the state of the server.
34
35 attr_reader :status
36
37 ##
38 # Create a new Redis::Server object for the redis instance
39 # listening on the given host and port.
40
41 def initialize(host, port = DEFAULT_PORT)
42 raise ArgumentError, "No host specified" if host.nil? or host.empty?
43 raise ArgumentError, "No port specified" if port.nil? or port.to_i.zero?
44
45 @host = host
46 @port = port.to_i
47
48 @sock = nil
49 @retry = nil
50 @status = 'NOT CONNECTED'
51 @timeout = 1
52 end
53
54 ##
55 # Return a string representation of the server object.
56 def inspect
57 "<Redis::Server: %s:%d (%s)>" % [@host, @port, @status]
58 end
59
60 ##
61 # Try to connect to the redis server targeted by this object.
62 # Returns the connected socket object on success or nil on failure.
63
64 def socket
65 return @sock if @sock and not @sock.closed?
66
67 @sock = nil
68
69 # If the host was dead, don't retry for a while.
70 return if @retry and @retry > Time.now
71
72 # Attempt to connect if not already connected.
73 begin
74 @sock = connect_to(@host, @port, @timeout)
75 @sock.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1
76 @retry = nil
77 @status = 'CONNECTED'
78 rescue Errno::EPIPE, Errno::ECONNREFUSED => e
79 puts "Socket died... socket: #{@sock.inspect}\n" if $debug
80 @sock.close
81 retry
82 rescue SocketError, SystemCallError, IOError => err
83 puts "Unable to open socket: #{err.class.name}, #{err.message}" if $debug
84 mark_dead err
85 end
86
87 return @sock
88 end
89
90 def connect_to(host, port, timeout=nil)
9495122b 91 addrs = Socket.getaddrinfo(host, nil)
29fac617 92 addr = addrs.detect { |ad| ad[0] == 'AF_INET' }
93 sock = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
29fac617 94 if timeout
95 secs = Integer(timeout)
96 usecs = Integer((timeout - secs) * 1_000_000)
97 optval = [secs, usecs].pack("l_2")
98 sock.setsockopt Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, optval
99 sock.setsockopt Socket::SOL_SOCKET, Socket::SO_SNDTIMEO, optval
100 end
9495122b 101 sock.connect(Socket.pack_sockaddr_in(port, addr[3]))
29fac617 102 sock
103 end
104
105 ##
106 # Close the connection to the redis server targeted by this
107 # object. The server is not considered dead.
108
109 def close
110 @sock.close if @sock && !@sock.closed?
111 @sock = nil
112 @retry = nil
113 @status = "NOT CONNECTED"
114 end
115
116 ##
117 # Mark the server as dead and close its socket.
118 def mark_dead(error)
119 @sock.close if @sock && !@sock.closed?
120 @sock = nil
121 @retry = Time.now #+ RETRY_DELAY
122
123 reason = "#{error.class.name}: #{error.message}"
124 @status = sprintf "%s:%s DEAD (%s), will retry at %s", @host, @port, reason, @retry
125 puts @status
126 end
127
128end