aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorSteve Bennett <steveb@workware.net.au>2022-06-12 11:10:08 +1000
committerSteve Bennett <steveb@workware.net.au>2022-07-25 22:45:30 +1000
commitd222f25e3583402b940b9b1c42cf8f69b6e81b0a (patch)
tree6563b31fac27253ff208f6bf72eacc3899689d96 /examples
parent2c15f8c667a8353f467563a3b7e72f7dc7a45dfc (diff)
downloadjimtcl-d222f25e3583402b940b9b1c42cf8f69b6e81b0a.zip
jimtcl-d222f25e3583402b940b9b1c42cf8f69b6e81b0a.tar.gz
jimtcl-d222f25e3583402b940b9b1c42cf8f69b6e81b0a.tar.bz2
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 <steveb@workware.net.au>
Diffstat (limited to 'examples')
-rw-r--r--examples/redis-pubsub.tcl33
1 files 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"
}
}