Repository navigation
Honor Retry-After on Mergify queue observations #1563
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3eca5fd
d26ca8b
9d9d15f
44e7683
ec8ed7a
9b2fdeb
71a4432
71a8ff7
67df7d4
da45c03
d2614ec
9bd1845
b60a084
0234af9
2cd175e
33cb637
1aae039
0c68daf
b945542
db8a695
6c65b6a
251719a
e780406
0349474
1e7cd89
c1d8bb7
6ac05fd
ff316b1
fe2382a
f52c6f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -80,7 +80,15 @@ def _open(request: urllib.request.Request): | |
| raise MergifyError("Mergify API request has an invalid URL or header") from None | ||
|
|
||
|
|
||
| def api_get(repo: str, suffix: str) -> object | None: | ||
| def _wait_to_retry(delay: float, deadline: float | None) -> bool: | ||
| remaining = 5.0 if deadline is None else deadline - time.monotonic() | ||
| if delay >= remaining: | ||
| return False | ||
| _retry_sleep(delay) | ||
| return deadline is None or time.monotonic() < deadline | ||
|
|
||
|
|
||
| def api_get(repo: str, suffix: str, deadline: float | None = None) -> object | None: | ||
| """GET one documented endpoint below this repository's fixed Mergify API path.""" | ||
| if not suffix.startswith("/") or "://" in suffix or ".." in suffix: | ||
| raise ValueError("Mergify API suffix must be a repository-relative absolute path") | ||
|
|
@@ -91,22 +99,26 @@ def api_get(repo: str, suffix: str) -> object | None: | |
| headers={"Accept": "application/json", "User-Agent": "loopzero", | ||
| "Authorization": f"Bearer {_credential()}"}, | ||
| ) | ||
| for delay in (2.0, 5.0, None): # reads retry server errors, throttling and dropped reads | ||
| for retry in (True, False): | ||
| delay = 2.0 | ||
| try: | ||
| with _open(request) as response: | ||
| return json.load(response) | ||
| except urllib.error.HTTPError as exc: | ||
| if exc.code == 404: | ||
| return None | ||
| if delay is None or not (exc.code >= 500 or exc.code == 429): | ||
| raise MergifyError(f"Mergify queue lookup failed with HTTP {exc.code}") from exc | ||
| except (OSError, ValueError, http.client.IncompleteRead) as exc: | ||
| if delay is None: | ||
| raise MergifyError("Mergify queue lookup returned no valid JSON") from exc | ||
| _retry_sleep(delay) | ||
| wait = github.retry_after(exc.headers.get("Retry-After", "")) if exc.headers else None | ||
| delay = max(delay, wait or 0.0) | ||
| detail = f"HTTP {exc.code}" + (f"; Retry-After: {wait:g}s" if wait is not None else "") | ||
| transient = exc.code >= 500 or exc.code == 429 or (exc.code == 403 and wait is not None) | ||
| except (OSError, ValueError, http.client.IncompleteRead): | ||
| detail, transient = "transport or invalid JSON response", True | ||
| if not retry or not transient: | ||
| raise MergifyError(f"Mergify queue lookup failed with {detail}") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Lost exception cause, and a missing blank line before The |
||
| if not _wait_to_retry(delay, deadline): | ||
|
eowca marked this conversation as resolved.
Resolved
|
||
| raise MergifyError(f"Mergify queue lookup failed with {detail}; retry delay exceeds the wait budget") | ||
| return None | ||
|
|
||
|
|
||
| def _unmet_conditions(node: object) -> tuple[str, ...]: | ||
| if not isinstance(node, dict) or node.get("match") is not False: | ||
| return () | ||
|
|
@@ -117,8 +129,8 @@ def _unmet_conditions(node: object) -> tuple[str, ...]: | |
| return (label,) if isinstance(label, str) and label.isprintable() else () | ||
|
|
||
|
|
||
| def membership(repo: str, number: int, queue: str) -> Membership | None: | ||
| data = api_get(repo, f"/merge-queue/pull/{number}") | ||
| def membership(repo: str, number: int, queue: str, deadline: float | None = None) -> Membership | None: | ||
| data = api_get(repo, f"/merge-queue/pull/{number}", deadline) | ||
| if data is None: | ||
| return None | ||
| try: | ||
|
|
@@ -187,12 +199,12 @@ def construct_mapping(self, node, deep=False): | |
| and isinstance(metadata, dict) and metadata.get("default_branch") == branch) | ||
|
|
||
|
|
||
| def configured(repo: str, branch: str, queue: str) -> bool: | ||
| def configured(repo: str, branch: str, queue: str, deadline: float | None = None) -> bool: | ||
| """Require live queue identity and stable canonical speculative configuration.""" | ||
| status_data = api_get(repo, f"/merge-queue/status?branch={quote(branch, safe='')}") | ||
| status_data = api_get(repo, f"/merge-queue/status?branch={quote(branch, safe='')}", deadline) | ||
| if status_data is None: | ||
| return False | ||
| rules_data = api_get(repo, "/queues/configuration") | ||
| rules_data = api_get(repo, "/queues/configuration", deadline) | ||
| if not isinstance(status_data, dict) or not isinstance(rules_data, dict): | ||
| return False | ||
| if not isinstance(status_data.get("batches"), list): | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.