Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Update redis client to use socket primitives only + socket timeouts in

order to remove the slow Ruby timeout code.
  • Loading branch information...
commit d26c67c911da2d01ca58e1bc269bc1b74306bf81 1 parent e13812e
@mperham mperham authored
Showing with 37 additions and 6 deletions.
  1. +19 −6 lib/redis.rb
  2. +18 −0 spec/redis_spec.rb
View
25 lib/redis.rb
@@ -746,8 +746,8 @@ def close
socket.close unless socket.closed?
end
- def timeout_retry(time, retries, &block)
- timeout(time, &block)
+ def timeout_retry(time, retries)
+ yield
rescue TimeoutError
retries -= 1
retry unless retries < 0
@@ -758,9 +758,18 @@ def socket
end
def connect
- @socket = TCPSocket.new(@opts[:host], @opts[:port])
- @socket.sync = true
- @socket
+ addrs = Socket.getaddrinfo(@opts[:host], nil)
+ addr = addrs.detect { |ad| ad[0] == 'AF_INET' }
+ sock = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
+
+ timeout = 3.0
+ secs = Integer(timeout)
+ usecs = Integer((timeout - secs) * 1_000_000)
+ optval = [secs, usecs].pack("l_2")
+ sock.setsockopt Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, optval
+ sock.setsockopt Socket::SOL_SOCKET, Socket::SO_SNDTIMEO, optval
+ sock.connect(Socket.pack_sockaddr_in(@opts[:port], addr[3]))
+ sock
end
def read(length, nodebug=true)
@@ -793,7 +802,11 @@ def nibble_end
end
def read_proto
- socket.gets.chomp
+ buff = ""
+ while buff[-2..-1] != CTRLF
+ buff << read(1)
+ end
+ buff[0..-3]
end
View
18 spec/redis_spec.rb
@@ -30,6 +30,24 @@ class MyFail; def fail; 'it will' end; end
end
+ # 1.9 sec with 'better timeout'
+ # 1.3 sec with socket timeouts
+ it "should be have decent GET performance" do
+ require 'benchmark'
+ Benchmark.bm(20) do |bm|
+ bm.report 'GET' do
+ 10_000.times do
+ @r['foo']
+ end
+ end
+ bm.report 'SET' do
+ 10_000.times do
+ @r['foo'] = 125782
+ end
+ end
+ end
+ end
+
it "should be able to GET a key" do
@r['foo'].should == 'bar'
end
Please sign in to comment.
Something went wrong with that request. Please try again.