Skip to content

Commit 728b225

Browse files
committed
Enhance retry policy error handling and validation
- Added validation to ensure the retry count is greater than 0 in the compute_delay method of RetryPolicyModel, raising a ValueError for invalid inputs. - Updated the compute_delay method to correctly calculate delays based on the retry count, adjusting the exponentiation logic. - Refined error handling in the GraphTemplate model by replacing ValueError with HTTPException for better integration with FastAPI, ensuring a 404 response when a graph template is not found.
1 parent 66c794a commit 728b225

2 files changed

Lines changed: 10 additions & 5 deletions

File tree

state-manager/app/models/db/graph_template_model.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from pymongo import IndexModel
66
from pydantic import Field, field_validator, PrivateAttr, model_validator
77
from typing import List, Self, Dict
8+
from fastapi.exceptions import HTTPException
9+
from fastapi import status
810

911
from .base import BaseDatabaseModel
1012
from ..graph_template_validation_status import GraphTemplateValidationStatus
@@ -303,7 +305,7 @@ def get_path_by_identifier(self, identifier: str) -> set[str]:
303305
async def get(namespace: str, graph_name: str) -> "GraphTemplate":
304306
graph_template = await GraphTemplate.find_one(GraphTemplate.namespace == namespace, GraphTemplate.name == graph_name)
305307
if not graph_template:
306-
raise ValueError(f"Graph template not found for namespace: {namespace} and graph name: {graph_name}")
308+
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Graph template not found")
307309
return graph_template
308310

309311
@staticmethod

state-manager/app/models/retry_policy_model.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,18 @@ class RetryPolicyModel(BaseModel):
2222
exponent: int = Field(default=2, description="The exponent for the exponential retry strategy", gt=0)
2323

2424
def compute_delay(self, retry_count: int) -> int:
25+
if retry_count < 1:
26+
raise ValueError(f"Retry count must be greater than 1, got {retry_count}")
27+
2528
if self.strategy == RetryStrategy.EXPONENTIAL:
26-
return (self.backoff_factor * (self.exponent ** retry_count))
29+
return (self.backoff_factor * (self.exponent ** (retry_count - 1)))
2730

2831
elif self.strategy == RetryStrategy.EXPONENTIAL_FULL_JITTER:
29-
base = self.backoff_factor * (self.exponent ** retry_count)
32+
base = self.backoff_factor * (self.exponent ** (retry_count - 1))
3033
return int(random.uniform(0, base))
3134

3235
elif self.strategy == RetryStrategy.EXPONENTIAL_EQUAL_JITTER:
33-
base = self.backoff_factor * (self.exponent ** retry_count)
36+
base = self.backoff_factor * (self.exponent ** (retry_count - 1))
3437
return int(base/2 + random.uniform(0, base / 2))
3538

3639
elif self.strategy == RetryStrategy.LINEAR:
@@ -56,4 +59,4 @@ def compute_delay(self, retry_count: int) -> int:
5659
return int(base/2 + random.uniform(0, base / 2))
5760

5861
else:
59-
raise Exception("Invalid retry strategy")
62+
raise ValueError(f"Invalid retry strategy: {self.strategy}")

0 commit comments

Comments
 (0)