redis_ha 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/redis_ha/connection.rb +19 -2
- data/lib/redis_ha/connection_pool.rb +1 -1
- data/lib/redis_ha/protocol.rb +11 -4
- data/lib/test.rb +7 -4
- data/redis_ha.gemspec +1 -1
- metadata +1 -1
data/lib/redis_ha/connection.rb
CHANGED
@@ -4,14 +4,21 @@ class RedisHA::Connection < Socket
|
|
4
4
|
def initialize(redis, pool)
|
5
5
|
@write_buffer = ""
|
6
6
|
@read_buffer = ""
|
7
|
+
@response_offset = 0
|
7
8
|
|
8
9
|
super(AF_INET, SOCK_STREAM, 0)
|
9
10
|
|
11
|
+
@redis = redis
|
10
12
|
@pool = pool
|
11
13
|
setup(redis)
|
12
14
|
end
|
13
15
|
|
14
16
|
def yield_connect
|
17
|
+
if @redis[:db] && !@db_selected
|
18
|
+
@db_selected = true
|
19
|
+
self << RedisHA::Protocol.request("select", @redis[:db])
|
20
|
+
end
|
21
|
+
|
15
22
|
connect_nonblock(@__addr)
|
16
23
|
rescue Errno::EINPROGRESS, Errno::ECONNABORTED, Errno::EINVAL
|
17
24
|
nil
|
@@ -41,13 +48,19 @@ class RedisHA::Connection < Socket
|
|
41
48
|
end
|
42
49
|
|
43
50
|
def <<(buf)
|
51
|
+
@response_offset += 1
|
44
52
|
@write_buffer << buf
|
45
53
|
end
|
46
54
|
|
47
55
|
def rewind
|
48
56
|
@read_buffer = ""
|
49
|
-
@write_buffer = ""
|
50
57
|
@ready = false
|
58
|
+
@response_offset = 0
|
59
|
+
end
|
60
|
+
|
61
|
+
def next
|
62
|
+
@response_offset -= 1
|
63
|
+
RedisHA::Protocol.parse(@read_buffer)
|
51
64
|
end
|
52
65
|
|
53
66
|
def wait_read?
|
@@ -65,7 +78,11 @@ class RedisHA::Connection < Socket
|
|
65
78
|
end
|
66
79
|
|
67
80
|
def ready?
|
68
|
-
@ready
|
81
|
+
if @ready && @response_offset > 0
|
82
|
+
self.next; @ready = false; check
|
83
|
+
end
|
84
|
+
|
85
|
+
!!@ready
|
69
86
|
end
|
70
87
|
|
71
88
|
def setup(redis)
|
data/lib/redis_ha/protocol.rb
CHANGED
@@ -2,7 +2,7 @@ class RedisHA::Protocol
|
|
2
2
|
|
3
3
|
def self.request(*args)
|
4
4
|
args.inject("*#{args.size}\r\n") do |s, arg|
|
5
|
-
s << "$#{arg.
|
5
|
+
s << "$#{arg.to_s.length}\r\n#{arg}\r\n"
|
6
6
|
end
|
7
7
|
end
|
8
8
|
|
@@ -38,9 +38,16 @@ class RedisHA::Protocol
|
|
38
38
|
|
39
39
|
def self.parse(buf)
|
40
40
|
case buf[0]
|
41
|
-
when "-" then
|
42
|
-
|
43
|
-
|
41
|
+
when "-", "+", ":" then
|
42
|
+
len = buf.index("\r\n")
|
43
|
+
ret = buf[0..len-1]
|
44
|
+
buf.replace(buf[len+2..-1])
|
45
|
+
|
46
|
+
case ret[0]
|
47
|
+
when "+" then ret[1..-1]
|
48
|
+
when ":" then ret[1..-1].to_i
|
49
|
+
when "-" then RuntimeError.new(ret[1..-1])
|
50
|
+
end
|
44
51
|
|
45
52
|
when "$"
|
46
53
|
if buf[1..2] == "-1"
|
data/lib/test.rb
CHANGED
@@ -17,16 +17,19 @@ pool = RedisHA::ConnectionPool.new
|
|
17
17
|
pool.retry_timeout = 0.5
|
18
18
|
pool.read_timeout = 10.1
|
19
19
|
pool.connect(
|
20
|
-
{:host => "localhost", :port => 6379})
|
20
|
+
{:host => "localhost", :port => 6379, :db => 2})
|
21
21
|
|
22
|
-
map = RedisHA::HashMap.new(pool, "fnordmap")
|
23
|
-
set = RedisHA::Set.new(pool, "fnordset")
|
24
|
-
ctr = RedisHA::Counter.new(pool, "fnordctr")
|
22
|
+
#map = RedisHA::HashMap.new(pool, "fnordmap")
|
23
|
+
#set = RedisHA::Set.new(pool, "fnordset")
|
24
|
+
#ctr = RedisHA::Counter.new(pool, "fnordctr")
|
25
25
|
|
26
26
|
#set.add(:fnord, :bar, :fubar, :blubb)
|
27
27
|
#puts pool.smembers("fnordset").inspect
|
28
28
|
#puts set.get.inspect
|
29
29
|
|
30
|
+
puts pool.get("dawanda:session:fnord").inspect
|
31
|
+
exit
|
32
|
+
|
30
33
|
Ripl.start :binding => binding
|
31
34
|
exit
|
32
35
|
|
data/redis_ha.gemspec
CHANGED