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.
@@ -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 == true
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)
@@ -41,7 +41,7 @@ private
41
41
  await
42
42
 
43
43
  @connections.map do |conn|
44
- res = RedisHA::Protocol.parse(conn.read_buffer)
44
+ res = conn.next
45
45
 
46
46
  if res.is_a?(Exception)
47
47
  @connections.each(&:rewind)
@@ -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.size}\r\n#{arg}\r\n"
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 RuntimeError.new(buf[1..-3])
42
- when "+" then buf[1..-3]
43
- when ":" then buf[1..-3].to_i
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"
@@ -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
 
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "redis_ha"
6
- s.version = "0.1.1"
6
+ s.version = "0.1.2"
7
7
  s.date = Date.today.to_s
8
8
  s.platform = Gem::Platform::RUBY
9
9
  s.authors = ["Paul Asmuth"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis_ha
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: