From d222f25e3583402b940b9b1c42cf8f69b6e81b0a Mon Sep 17 00:00:00 2001 From: Steve Bennett Date: Sun, 12 Jun 2022 11:10:08 +1000 Subject: examples/redis-pubsub.tcl: better error handling More gracefully handle errors, e.g. the redis server going away in line with how a real client would do this. Signed-off-by: Steve Bennett --- examples/redis-pubsub.tcl | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/examples/redis-pubsub.tcl b/examples/redis-pubsub.tcl index ce0f78c..0bf8372 100644 --- a/examples/redis-pubsub.tcl +++ b/examples/redis-pubsub.tcl @@ -36,26 +36,39 @@ try { } if {$op eq "sub"} { + # read will generate a bgerror if the server goes away + proc bgerror {msg} { + set ::done "$::op: $msg" + } + $r SUBSCRIBE chin $r SUBSCRIBE chan + proc sub_timeout {} { + set ::done "$::op: quitting on idle" + } + $r readable { after cancel $afterid set result [$r read] puts "$op: $result" - set afterid [after 2000 {incr done}] + set afterid [after 2000 sub_timeout] } # If no message for 2 seconds, stop - set afterid [after 2000 {incr done}] + set afterid [after 2000 sub_timeout] vwait done - puts "$op: quitting on idle" + puts $done } else { - loop i 1 15 { - $r PUBLISH chan PONG$i - puts "$op: chan PONG$i" - after 250 - $r PUBLISH chin PING$i - puts "$op: chin PING$i" - after 250 + try { + loop i 1 15 { + $r PUBLISH chan PONG$i + puts "$op: chan PONG$i" + after 250 + $r PUBLISH chin PING$i + puts "$op: chin PING$i" + after 250 + } + } on error msg { + puts "$op: $msg" } } -- cgit v1.1