frappex-client v0.1.1

Going further

Retries and timeouts#

What is retried#

frappex-client retries a request only when repeating it cannot cause harm.

Situation GET (reads, call(..., http_method="GET")) POST, PUT, DELETE (writes)
Connection failed, timeout, reset Retried Raised
502, 503, 504 Retried Raised
429 Too Many Requests Retried Retried
Any other status Raised Raised

Rate-limited requests are retried even for writes because Frappe rejects them before running anything.

The default policy#

from frappex_client import RetryPolicy

RetryPolicy(
    max_attempts=3,  # first try plus two retries
    backoff_base=0.5,  # first delay up to 0.5 s, doubling each time
    backoff_max=8.0,  # a computed delay never exceeds 8 s
    max_retry_after=30.0,  # longest server-requested wait honoured
)

Delays use exponential backoff with full jitter: before retry n the client waits a random time between 0 and min(backoff_max, backoff_base × 2ⁿ⁻¹) seconds. The randomness spreads out many clients that failed at the same moment, so they do not all hit the site again together.

Rate limits#

When the site answers 429 with a Retry-After header, frappex-client waits exactly that long, as long as it is no more than max_retry_after seconds. A longer wait raises RateLimitError immediately instead of blocking your program; the advised wait is on error.retry_after.

from frappex_client import RateLimitError

try:
    client.get_list("Sales Invoice", limit=500)
except RateLimitError as error:
    schedule_later(seconds=error.retry_after or 60)

Configuring retries#

from frappex_client import Client, RetryPolicy

client = Client(url, auth, retry=RetryPolicy(max_attempts=5, backoff_base=1.0))

Turn retries off completely:

client = Client(url, auth, retry=RetryPolicy(max_attempts=1))

Why writes are never retried#

Suppose an insert reaches the site, the site saves the document, and the connection drops before the reply arrives. The client sees a network error. If it sent the insert again, you would get two documents. Because the client cannot tell "never arrived" from "arrived but the reply was lost", it raises TransportError and lets you decide.

To make a write safe to repeat yourself, give it something unique you can look up first:

from frappex_client import TransportError


def create_payment_once(client, reference, values):
    try:
        return client.insert("Payment Entry", {"reference_no": reference, **values})
    except TransportError:
        existing = client.get_value("Payment Entry", "name", {"reference_no": reference})
        if existing:
            return client.get_doc("Payment Entry", existing["name"])
        raise

Timeouts#

Every request has a timeout. The default is 30 seconds, applied to connecting, reading and writing separately.

client = Client(url, auth, timeout=10)

For finer control, pass an httpx.Timeout:

import httpx

client = Client(url, auth, timeout=httpx.Timeout(60.0, connect=5.0))

A timeout raises RequestTimeoutError, a subclass of TransportError, after any retries the policy allows. Reports and large exports can be slow: give those clients a longer timeout rather than raising it everywhere.

Found a mistake? Open an issue.