Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
3f32f1f6
Commit
3f32f1f6
authored
May 20, 2009
by
antirez
Browse files
Automagically reconnection of RubyRedis
parent
ad0ea27c
Changes
1
Hide whitespace changes
Inline
Side-by-side
client-libraries/ruby_2/rubyredis.rb
View file @
3f32f1f6
...
@@ -19,8 +19,7 @@ class RedisClient
...
@@ -19,8 +19,7 @@ class RedisClient
@host
=
opts
[
:host
]
@host
=
opts
[
:host
]
@port
=
opts
[
:port
]
@port
=
opts
[
:port
]
@db
=
opts
[
:db
]
@db
=
opts
[
:db
]
@sock
=
connect_to_server
connect_to_server
call_command
([
"select"
,
@db
])
if
@db
!=
0
end
end
def
to_s
def
to_s
...
@@ -28,7 +27,8 @@ class RedisClient
...
@@ -28,7 +27,8 @@ class RedisClient
end
end
def
connect_to_server
def
connect_to_server
TCPSocket
.
new
(
@host
,
@port
,
0
)
@sock
=
TCPSocket
.
new
(
@host
,
@port
,
0
)
call_command
([
"select"
,
@db
])
if
@db
!=
0
end
end
def
method_missing
(
*
argv
)
def
method_missing
(
*
argv
)
...
@@ -36,6 +36,19 @@ class RedisClient
...
@@ -36,6 +36,19 @@ class RedisClient
end
end
def
call_command
(
argv
)
def
call_command
(
argv
)
# this wrapper to raw_call_command handle reconnection on socket
# error. We try to reconnect just one time, otherwise let the error
# araise.
begin
raw_call_command
(
argv
)
rescue
Errno
::
ECONNRESET
@sock
.
close
connect_to_server
raw_call_command
(
argv
)
end
end
def
raw_call_command
(
argv
)
bulk
=
nil
bulk
=
nil
argv
[
0
]
=
argv
[
0
].
to_s
.
downcase
argv
[
0
]
=
argv
[
0
].
to_s
.
downcase
if
BulkCommands
[
argv
[
0
]]
if
BulkCommands
[
argv
[
0
]]
...
@@ -61,6 +74,7 @@ class RedisClient
...
@@ -61,6 +74,7 @@ class RedisClient
def
read_reply
def
read_reply
line
=
@sock
.
gets
line
=
@sock
.
gets
raise
Errno
::
ECONNRESET
,
"Connection lost"
if
!
line
case
line
[
0
..
0
]
case
line
[
0
..
0
]
when
"-"
when
"-"
raise
line
.
strip
raise
line
.
strip
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment