From 01c38eabddba69dda1f3f47256bfa22f75cc2960 Mon Sep 17 00:00:00 2001 From: Kyle Banker Date: Wed, 29 Dec 2010 13:13:54 -0500 Subject: [PATCH] Use retry instead of while loop for rescuing connection failure (quasor) --- docs/REPLICA_SETS.md | 20 ++++++++------------ test/replica_sets/rs_test_helper.rb | 20 +++++++++----------- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/docs/REPLICA_SETS.md b/docs/REPLICA_SETS.md index 5722676..12a11b9 100644 --- a/docs/REPLICA_SETS.md +++ b/docs/REPLICA_SETS.md @@ -38,18 +38,14 @@ every half second and time out after thirty seconds. # Ensure retry upon failure def rescue_connection_failure(max_retries=60) - success = false - retries = 0 - while !success - begin - yield - success = true - rescue Mongo::ConnectionFailure => ex - retries += 1 - raise ex if retries >= max_retries - sleep(0.5) - end - end + retries = 0 + begin + yield + rescue Mongo::ConnectionFailure => ex + retries += 1 + raise ex if retries > max_retries + sleep(0.5) + retry end end diff --git a/test/replica_sets/rs_test_helper.rb b/test/replica_sets/rs_test_helper.rb index 869e721..0611fa4 100644 --- a/test/replica_sets/rs_test_helper.rb +++ b/test/replica_sets/rs_test_helper.rb @@ -12,17 +12,15 @@ class Test::Unit::TestCase # Generic code for rescuing connection failures and retrying operations. # This could be combined with some timeout functionality. def rescue_connection_failure(max_retries=60) - success = false - tries = 0 - while !success && tries < max_retries - begin - yield - success = true - rescue Mongo::ConnectionFailure - puts "Rescue attempt #{tries}\n" - tries += 1 - sleep(1) - end + retries = 0 + begin + yield + rescue Mongo::ConnectionFailure => ex + puts "Rescue attempt #{retries}" + retries += 1 + raise ex if retries > max_retries + sleep(1) + retry end end