Skip to content

Commit

Permalink
Make reconnect asynchronous again
Browse files Browse the repository at this point in the history
Fixes #8
  • Loading branch information
gahr committed Feb 20, 2018
1 parent ff4160a commit 11e90a4
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 17 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,9 @@ any command (like `PING`). This is demonstrated in the following example:

It is also possible to manage the connection to the Redis server manually,
possibly constructing the retcl object in disconnected mode and using the
`connect`, `disconnect` and `reconnect` methods. The latter tries to reconnect
to the server for as much as 10 seconds before giving up.
`connect`, `disconnect` and `reconnect` methods. The latter returns
immediately, tries to reconnect to the server for as much as 10 seconds, and
calls the error handler on failure.

% retcl create r -noconnect
::r
Expand Down Expand Up @@ -339,7 +340,7 @@ Returns 1 if the client is connected, 0 otherwise.

r reconnect

Try to reconnect for 10 seconds before giving up.
Returns immediately, try to reconnect for 10 seconds before giving up.

r errorHandler ?cmdPrefix?

Expand Down
6 changes: 3 additions & 3 deletions doc/retcl.html
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ <h1 class="doctools_title">retcl(n) 0.3.0 retcl &quot;Redis client&quot;</h1>
<dt><a name="5"><em>r</em> <b class="cmd">disconnect</b></a></dt>
<dd><p>Disconnect from the Redis server.</p></dd>
<dt><a name="6"><em>r</em> <b class="cmd">reconnect</b></a></dt>
<dd><p>Try to reconnect to the server that was specified upon construction for up to
10 seconds. If no connection can be established after this time, an error is
reported using the <em>error handler</em>.</p></dd>
<dd><p>Return immediately, then try to reconnect to the server that was specified upon
construction for up to 10 seconds. If no connection can be established after
this time, an error is reported using the <em>error handler</em>.</p></dd>
<dt><a name="7"><em>r</em> <span class="opt">?-sync?</span> <span class="opt">?-cb cmdPrefix?</span> <b class="cmd">REDIS_CMD</b> <span class="opt">?arg ...?</span></a></dt>
<dd><p>Methods not explicitely defined by the <b class="package">retcl</b> class are forwarded to
Redis by concatenating the <b class="cmd">REDIS_CMD</b> and any optional arguments.
Expand Down
6 changes: 3 additions & 3 deletions doc/retcl.man
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ Disconnect from the Redis server.

[call [emph r] [cmd reconnect]]

Try to reconnect to the server that was specified upon construction for up to
10 seconds. If no connection can be established after this time, an error is
reported using the [emph {error handler}].
Return immediately, then try to reconnect to the server that was specified upon
construction for up to 10 seconds. If no connection can be established after
this time, an error is reported using the [emph {error handler}].

[call [emph r] [opt -sync] [opt {-cb cmdPrefix}] [cmd REDIS_CMD] [opt {arg ...}]]

Expand Down
6 changes: 3 additions & 3 deletions doc/retcl.n
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,9 @@ connected object\&.
Disconnect from the Redis server\&.
.TP
\fIr\fR \fBreconnect\fR
Try to reconnect to the server that was specified upon construction for up to
10 seconds\&. If no connection can be established after this time, an error is
reported using the \fIerror handler\fR\&.
Return immediately, then try to reconnect to the server that was specified upon
construction for up to 10 seconds\&. If no connection can be established after
this time, an error is reported using the \fIerror handler\fR\&.
.TP
\fIr\fR ?-sync? ?-cb cmdPrefix? \fBREDIS_CMD\fR ?arg \&.\&.\&.?
Methods not explicitely defined by the \fBretcl\fR class are forwarded to
Expand Down
14 changes: 9 additions & 5 deletions retcl.tm
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ oo::class create retcl {
# valid.
variable checkEventId

##
# After Id for the automatic reconnection.
variable reconnectEventId

##
# Simple sentinel that's set whenever there's activity
variable activity
Expand All @@ -131,6 +135,7 @@ oo::class create retcl {
set pipeline {}
set isPipelined 0
set checkEventId {}
set reconnectEventId {}

my +keepCache
my +async
Expand Down Expand Up @@ -186,8 +191,8 @@ oo::class create retcl {
set err [catch {my {*}$connect_cmd} msg]
my errorHandler $saveErrorCallback
if {$err} {
after $waitMillis
my reconnect [incr i]
set reconnectEventId \
[after $waitMillis [list [self object] reconnect [incr i]]]
}
}

Expand All @@ -213,9 +218,8 @@ oo::class create retcl {
method disconnect {} {
catch {close $sock}
set sock {}
if {$checkEventId ne {}} {
after cancel $checkEventId
}
after cancel $checkEventId
after cancel $reconnectEventId
}

##
Expand Down
33 changes: 33 additions & 0 deletions test/003-connect.test
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,36 @@ tcltest::test connect-1.5 {reconnect from disconnected} -constraints {
} -cleanup {
$r destroy
} -result 1

tcltest::test connect-1.6 {reconnect failure} -constraints {
serverIsRunning
} -body {
set ::result [list]
set r [retcl new]
lappend ::result [$r connected]
stopServer
$r errorHandler [list apply {{msg} { lappend ::result $msg }}]
$r reconnect
vwait ::result
lappend ::result [$r connected]
} -cleanup {
startServer
$r destroy
} -result {1 {Could not reconnect to Redis server} 0}

tcltest::test connect-1.7 {reconnect success} -constraints {
serverIsRunning
} -body {
set ::result [list]
set r [retcl new]
lappend ::result [$r connected]
stopServer
$r errorHandler [list apply {{msg} { lappend ::result $msg }}]
$r reconnect
after 3000 { startServer }
after 12000 { lappend ::result {no timeout} }
vwait ::result
lappend ::result [$r connected]
} -cleanup {
$r destroy
} -result {1 {no timeout} 1}

0 comments on commit 11e90a4

Please sign in to comment.