Skip to content

Commit 19b304f

Browse files
authored
Add docstrings for _retry.py - v1
1 parent 85a7b10 commit 19b304f

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

src/yandex_cloud_ml_sdk/_retry.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ async def intercept_unary_stream(
317317
# pylint: disable=too-many-instance-attributes
318318
@dataclass(frozen=True)
319319
class RetryPolicy:
320+
"""A class that defines a retry policy for grpc operations."""
320321
max_attempts: int = 5
321322
initial_backoff: float = 1.0
322323
max_backoff: float = 10.0
@@ -331,13 +332,24 @@ class RetryPolicy:
331332
unary_stream_interceptor_class: type[UnaryStreamRetryInterceptor] | None = UnaryStreamRetryInterceptor
332333

333334
def get_interceptors(self) -> tuple[grpc.aio.ClientInterceptor, ...]:
335+
"""Get the configured grpc interceptors based on the retry policy."""
334336
klasses = [self.unary_unary_interceptor_class, self.unary_stream_interceptor_class]
335337
result = tuple(kls(self) for kls in klasses if kls is not None)
336338

337339
# because grpc typing are not good
338340
return result # type: ignore[return-value]
339341

340342
async def sleep(self, attempt: int, deadline: float | None) -> None:
343+
"""
344+
Asynchronously sleep for a calculated backoff time.
345+
346+
The backoff time is determined by the initial backoff, multiplier,
347+
and random jitter. It also considers the provided deadline.
348+
349+
:param attempt: the current retry attempt number (0-indexed).
350+
:param deadline: the deadline by which the operation should complete.
351+
352+
"""
341353
# first attempt == 0, so p.initial_backoff * p.backoff_multiplier ** 0 == p.initial_backoff
342354
backoff = self.initial_backoff * (self.backoff_multiplier ** attempt) + random.uniform(0, self.jitter)
343355
if deadline is None:
@@ -351,8 +363,14 @@ async def sleep(self, attempt: int, deadline: float | None) -> None:
351363

352364

353365
class NoRetryPolicy(RetryPolicy):
366+
"""
367+
A retry policy that disables retries.
368+
369+
This class overrides the behavior of the base RetryPolicy to return no interceptors.
370+
"""
354371
def __init__(self):
355372
super().__init__()
356373

357374
def get_interceptors(self) -> tuple[()]:
375+
"""Get the configured grpc interceptors for the no-retry policy."""
358376
return ()

0 commit comments

Comments
 (0)