Synchronous GitHub API call with client‑side timeout versus Prefer: respond‑async with polling for cancellable long‑running jobs
0 reputation · 29 Oct 2024, 09:21 UTC
0 reputation · 29 Oct 2024, 09:21 UTC
Goal: enforce a maximum wait time and allow cancellation for long‑running GitHub API operations such as creating a deployment or merging a pull request.
Constraint: a synchronous request blocks until the server finishes or the client’s timeout expires, yielding no progress information and a hard 504‑like failure if the limit is hit; using the Prefer: respond‑async header returns a 202 Accepted with a Location URL that the client can poll, but GitHub provides no dedicated cancel endpoint, so stopping the operation requires aborting the poll loop (if possible) or deleting the associated resource, and each poll consumes rate‑limit credits.
Uncertainty: which approach offers a better balance of timeout enforcement, resource usage, and implementation simplicity given the absence of a server‑side cancel mechanism.
Which approach provides more reliable enforcement of a client‑side timeout? How does the polling overhead and rate‑limit impact compare to the risk of a synchronous 504 timeout? Is there a recommended hybrid strategy that minimizes rate‑limit usage while still allowing cancellation?
33275 reputation · 29 Oct 2024, 16:52 UTC
A synchronous GitHub API request with a client‑side timeout provides more reliable enforcement of a maximum wait time than using Prefer: respond‑async and polling.
The synchronous method aborts the HTTP connection as soon as the client‑side timeout expires, returning a timeout error immediately. The async method relies on the client ending a poll loop; stopping the poll does not halt the server‑side job, so the timeout is only a best‑effort cancellation.
Prefer: respond‑async returns 202 Accepted with a Location URL; each poll consumes one rate‑limit unit and there is no dedicated cancel endpoint.Prefer: respond‑async, capture the Location URL, and poll with an exponential backoff (starting at 5 s, doubling up to a max of 30 s). Enforce an overall deadline (e.g., 2 minutes) after which you abort the poll loop.To decide whether the hybrid approach’s polling overhead is acceptable, we need to know the typical duration of the long‑running GitHub operation you are targeting (e.g., average time to create a deployment). If jobs usually finish well under your overall deadline, a longer initial poll interval can reduce rate‑limit impact.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.