Frontend-safe SP API timeouts + graceful add-to-cart degradation (#306) - #308
Open
chrismshea wants to merge 1 commit into
Open
Frontend-safe SP API timeouts + graceful add-to-cart degradation (#306)#308chrismshea wants to merge 1 commit into
chrismshea wants to merge 1 commit into
Conversation
The SP API is called synchronously during add-to-cart for subscription- enabled products. With the SDK default of 30s and no connect timeout, a slow or unreachable API can tie up PHP workers under load and degrade the storefront. Changes: - Add two Advanced config fields: 'API Request Timeout' (default 5s) and 'API Connect Timeout' (default 2s), per-website overridable. - Plumb these through Platform::createSdk into the SDK config (api_request_timeout / connect_timeout), only overriding when > 0 so a blank value falls back to the SDK default. - Widen AddProductToCartAfter to catch GuzzleHttp TransferException (connect/request timeouts are not response-based, so they are not SubscribePro HttpExceptions) and degrade gracefully instead of bubbling. Requires subscribepro/subscribepro-php#56 for connect_timeout support. Refs #306
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Hardens the storefront against a slow or unreachable Subscribe Pro API. The SP API is called synchronously during add-to-cart for subscription-enabled products; with the SDK default of 30s and no connect timeout, a slow/unreachable API can tie up PHP-FPM workers under load and degrade the storefront.
This PR (1) makes the frontend API timeouts configurable with frontend-safe defaults, and (2) closes a gap where a timeout in the add-to-cart path could bubble up uncaught.
Changes
API Request Timeout(default 5s) — total request timeout.API Connect Timeout(default 2s) — connection timeout;0disables the separate connect timeout.Model/Config/Advanced.php—getApiRequestTimeout()/getApiConnectTimeout()getters.Platform/Platform.php— injectsModel/Config/Advancedand plumbs both timeouts into the SDK config (api_request_timeout/connect_timeout), only overriding when the configured value is> 0(a blank/zero value falls back to the SDK default).Observer/CheckoutCart/AddProductToCartAfter.php— now also catchesGuzzleHttp\Exception\TransferException. A Guzzle connect/request timeout is not response-based, so it is not aSubscribePro\Exception\HttpExceptionand was previously not caught; it now degrades gracefully (item added without subscription options) instead of bubbling.Why the extra catch
SubscribePro\Exception\HttpExceptionrequires aResponseInterface(only thrown on a 4xx/5xx). A timeout / unreachable host raisesGuzzleHttp\Exception\ConnectException/RequestException(both extendTransferException) before any response exists, socatch (HttpException)does not catch it. Verified against a blackhole API: the old catch set let the exception bubble uncaught; the newTransferExceptioncatch degrades gracefully.Dependency
Requires subscribepro/subscribepro-php#57 (adds the
connect_timeoutSDK config key). Theapi_request_timeoutchange works with the current SDK;connect_timeoutis a no-op until the SDK PR is merged and the dependency is bumped.Testing
Verified in a Magento 2.4.8 / PHP 8.3 environment:
Platformwith the newAdvanceddependency; config defaults flow through to a built SDK (HttpreceivesrequestTimeout=5,connectTimeout=2).Scope / follow-up
This PR covers the add-to-cart path (the highest-risk hot path). Extending graceful degradation to the remaining synchronous storefront call sites is tracked separately in #307 (with a design-led, choke-point approach rather than per-site try/catch).
Notes
Closes #306