Commits

Max Noel committed 50845f1

* Added MAX_RETRIES protection to _save_autoincrement_hash_key.

  • Participants
  • Parent commits 48df9f2

Comments (0)

Files changed (1)

dynamodb_mapper/model.py

 log = logging.getLogger(__name__)
 
 
+MAX_RETRIES = 100
+
+
 class SchemaError(Exception):
     """Raised when a DynamoDBModel class's schema is incorrect."""
     pass
 
 
+class MaxRetriesExceededError(Exception):
+    """Raised when a failed operation couldn't be completed after retrying
+    MAX_RETRIES times (e.g. saving an autoincrementing hash_key).
+    """
+    pass
+
+
 class OverwriteError(Exception):
     """Raised when saving a DynamoDBModel instance would overwrite something
     in the database and we've forbidden that because we believe we're creating
 
         TODO Add schema checks.
         """
-        while True:
+        tries = 0
+        while tries < MAX_RETRIES:
+            tries += 1
             max_hash_item = item.table.get_item(0, consistent_read=True)
             max_hash_key = max_hash_item["__max_hash_key__"]
             max_hash_item["__max_hash_key__"] += 1