feat(api): add automatic retry with backoff for Figma API rate limiting#262
feat(api): add automatic retry with backoff for Figma API rate limiting#262subdan merged 4 commits intoRedMadRobot:masterfrom
Conversation
- Handle HTTP 429 responses with Retry-After header - Implement exponential backoff for request timeouts - Exit gracefully when Retry-After exceeds 5 minutes - Add RetryConfiguration for customizable retry behavior - Update all loaders to use requestWithRetry() - Add FigmaAPITests for rate limiting functionality
| } | ||
|
|
||
| logger.warning("Rate limited by Figma API. Waiting \(Int(retryAfter))s before retry \(attempt + 1)/\(configuration.maxRetries)") | ||
| Thread.sleep(forTimeInterval: retryAfter) |
There was a problem hiding this comment.
It's a bad practice to use Thread.sleep. It blocks the thread. Try to use Timer or DispatchQueue+asyncAfter
There was a problem hiding this comment.
Good catch! Replaced Thread.sleep with a hybrid RunLoop-based implementation that keeps the thread responsive during the wait. When RunLoop has active sources, it processes pending callbacks; otherwise, it falls back to short Thread.sleep(0.1) intervals to ensure the delay actually elapses (pure RunLoop would return immediately on threads without input sources).
Additionally, I've added a configurable requestDelay parameter (under figma: in YAML) to proactively throttle requests between API calls, which helps prevent rate limiting when exporting large numbers of assets.
Example config:
figma:
lightFileId: "..."
requestDelay: 0.1 # 100ms between requestsAdd requestDelaySeconds to RetryConfiguration to throttle API requests and prevent hitting Figma rate limits when exporting many assets. - Add requestDelay config option under figma: section in YAML - Apply delay after each successful API request when configured - Log throttling info when delay is active - Default to 0 (disabled) for backward compatibility - Preserve full RetryConfiguration flexibility in FigmaClient
- Import FoundationNetworking on Linux for HTTPURLResponse - Use Thread.sleep directly on Linux where RunLoop behaves differently - Add test for requestDelaySeconds configuration
|
+1 |
Summary
Add automatic retry with exponential backoff for Figma API rate limiting.
Context: As of November 17, 2025, Figma has updated their API rate limits. See the official announcement and rate limits documentation.
This PR handles:
Retry-AfterheaderRetry-Afterexceeds 5 minutes (avoids indefinite waiting)Changes
RateLimitErrorenum for rate limiting error typesRetryConfigurationstruct for customizable retry behaviorHTTPURLResponseextension to extractRetry-AfterheaderrequestWithRetry()method toClientprotocol andBaseClientFigmaClientto support retry configurationrequestWithRetry()FigmaAPITeststest target with rate limiting testsDefault Configuration
Test Plan