This repository was archived by the owner on Jul 7, 2026. It is now read-only.
Refines timeout and network error handling - #57
Merged
Conversation
Decreases the HttpClient timeout for Google Cloud requests from 2 minutes to 40 seconds to ensure more responsive error handling and prevent long hangs in the chat service.
Expands retry policies and fallback logic to include TimeoutException, HttpIOException, and additional API error codes (502, 504). This ensures the application more reliably switches to fallback models when encountering transient network issues or service timeouts.
Contributor
There was a problem hiding this comment.
Pull request overview
Improves resilience when calling Google Generative AI / Google Cloud services by tightening request timeouts and expanding transient error detection, including switching chat requests to a fallback model after certain failures.
Changes:
- Reduced the configured Google Cloud
HttpClienttimeout to 40 seconds. - Expanded retry conditions in
MentionHandlerto include additional timeout/network exception types and added fallback switching on those failures. - Broadened image model “unavailable” exception detection to include additional HTTP/network/timeout exception types.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
Pacos/Services/GenerativeAi/ImageGenerationService.cs |
Expands the set of exceptions treated as “model unavailable” to trigger retry/fallback attempts. |
Pacos/Services/ChatCommandHandlers/MentionHandler.cs |
Adds more retryable exception types and introduces fallback switching for timeout/network IO failures. |
Pacos/Program.cs |
Shortens the Google Cloud HttpClient timeout configuration to detect stalled calls sooner. |
Comments suppressed due to low confidence (1)
Pacos/Program.cs:72
- With AddStandardResilienceHandler configuring AttemptTimeout and TotalRequestTimeout, setting HttpClient.Timeout to googleRequestTimeout can cap the entire SendAsync operation (including retries) at 40s, preventing TotalRequestTimeout (and any built-in retries) from being effective. Consider setting HttpClient.Timeout to Timeout.InfiniteTimeSpan (or at least >= TotalRequestTimeout) and relying on the resilience handler timeouts for enforcement.
var googleRequestTimeout = TimeSpan.FromSeconds(40);
services.AddHttpClient(nameof(HttpClientType.GoogleCloud), httpClient => httpClient.Timeout = googleRequestTimeout)
.ConfigurePrimaryHttpMessageHandler((handler, serviceProvider) =>
{
var proxyAddress = serviceProvider.GetRequiredService<IOptions<PacosOptions>>().Value.WebProxy;
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Updates MentionHandler to trigger fallback logic when an API error message contains "try again". This ensures the service can switch to fallback models for transient failures that may not return standard 502, 503, or 504 status codes.
Expands the exception filter to include HttpRequestException, ensuring that general network request failures trigger the fallback chat service logic alongside existing timeout and I/O error handling.
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Delegates timeout management to the standard resilience handler. This ensures that the resilience policies have full control over request lifecycles without interference from the default HttpClient timeout.
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Improves the application's resilience to network interruptions and slow API responses:
TimeoutRejectedException,TimeoutException,HttpIOException,TaskCanceledExceptionwith innerTimeoutException).IsModelUnavailableExceptionlogic to include a wider range of HTTP, network I/O, and timeout exceptions, leading to more robust error classification for generative AI model availability.