Skip to content

Commit

Permalink
Instead of Exception, catch only RedisError
Browse files Browse the repository at this point in the history
Tighten up and target our `try`/`except` blocks in our distributed
algorithms.  `RedisError` is the base exception class for all exceptions
raised by redis-py.

https://github.com/andymccurdy/redis-py/blob/1a41cfd95a53a95b078084d8627be6b6fba3bb71/redis/exceptions.py#L4-L5
  • Loading branch information
brainix committed Dec 4, 2020
1 parent b1fb908 commit d6216f1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
5 changes: 3 additions & 2 deletions pottery/nextid.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from typing import cast

from redis import Redis
from redis import RedisError
from redis.client import Script
from typing_extensions import Final

Expand Down Expand Up @@ -138,7 +139,7 @@ def __current_id(self) -> int:
for future in concurrent.futures.as_completed(futures):
try:
current_ids.append(int(future.result()))
except Exception as error:
except RedisError as error:
_logger.error(error, exc_info=True)
num_masters_gotten = len(current_ids)
if num_masters_gotten < len(self.masters) // 2 + 1:
Expand All @@ -161,7 +162,7 @@ def __current_id(self, value: int) -> None:
for future in concurrent.futures.as_completed(futures):
try:
num_masters_set += future.result() == value
except Exception as error:
except RedisError as error:
_logger.error(error, exc_info=True)
if num_masters_set < len(self.masters) // 2 + 1:
raise QuorumNotAchieved(self.masters, self.key)
Expand Down
9 changes: 5 additions & 4 deletions pottery/redlock.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from typing import cast

from redis import Redis
from redis import RedisError
from redis.client import Script
from typing_extensions import Final

Expand Down Expand Up @@ -254,7 +255,7 @@ def __acquire_masters(self) -> bool:
for future in concurrent.futures.as_completed(futures):
try:
num_masters_acquired += future.result()
except Exception as error: # pragma: no cover
except RedisError as error: # pragma: no cover
_logger.error(error, exc_info=True)
quorum = num_masters_acquired >= len(self.masters) // 2 + 1
elapsed = timer.elapsed() - self.__drift()
Expand Down Expand Up @@ -359,7 +360,7 @@ def locked(self) -> int:
for future in concurrent.futures.as_completed(futures):
try:
ttls.append(future.result())
except Exception as error: # pragma: no cover
except RedisError as error: # pragma: no cover
_logger.error(error, exc_info=True)
num_masters_acquired = sum(1 for ttl in ttls if ttl > 0)
quorum = num_masters_acquired >= len(self.masters) // 2 + 1
Expand Down Expand Up @@ -401,7 +402,7 @@ def extend(self) -> None:
for future in concurrent.futures.as_completed(futures):
try:
num_masters_extended += future.result()
except Exception as error: # pragma: no cover
except RedisError as error: # pragma: no cover
_logger.error(error, exc_info=True)
quorum = num_masters_extended >= len(self.masters) // 2 + 1
self._extension_num += quorum
Expand Down Expand Up @@ -431,7 +432,7 @@ def release(self) -> None:
for future in concurrent.futures.as_completed(futures):
try:
num_masters_released += future.result()
except Exception as error: # pragma: no cover
except RedisError as error: # pragma: no cover
_logger.error(error, exc_info=True)
quorum = num_masters_released >= len(self.masters) // 2 + 1
if not quorum:
Expand Down

0 comments on commit d6216f1

Please sign in to comment.