Skip to content
This repository was archived by the owner on Jul 7, 2026. It is now read-only.

Refines timeout and network error handling - #57

Merged
magicxor merged 5 commits into
stagingfrom
feature/better-timeout-handling
Mar 26, 2026
Merged

Refines timeout and network error handling#57
magicxor merged 5 commits into
stagingfrom
feature/better-timeout-handling

Conversation

@magicxor

Copy link
Copy Markdown
Owner

Improves the application's resilience to network interruptions and slow API responses:

  • Reduces Google Cloud Request Timeout: Sets a shorter default timeout for HTTP client requests to Google Cloud services, allowing for quicker detection of unresponsive calls.
  • Enhances Chat Service Error Handling:
    • Expands the retry policy to cover various timeout and network I/O exceptions (TimeoutRejectedException, TimeoutException, HttpIOException, TaskCanceledException with inner TimeoutException).
    • Introduces dedicated error handling to automatically switch to a fallback chat model when these errors occur, improving user experience during service disruptions.
  • Broadens Image Generation Service Error Detection: Updates the IsModelUnavailableException logic to include a wider range of HTTP, network I/O, and timeout exceptions, leading to more robust error classification for generative AI model availability.

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.
@magicxor magicxor self-assigned this Mar 26, 2026
Copilot AI review requested due to automatic review settings March 26, 2026 13:26
@magicxor magicxor added the enhancement New feature or request label Mar 26, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 HttpClient timeout to 40 seconds.
  • Expanded retry conditions in MentionHandler to 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.

Comment thread Pacos/Services/ChatCommandHandlers/MentionHandler.cs
Comment thread Pacos/Services/ChatCommandHandlers/MentionHandler.cs Outdated
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.
Copilot AI review requested due to automatic review settings March 26, 2026 13:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread Pacos/Program.cs
Comment thread Pacos/Services/ChatCommandHandlers/MentionHandler.cs
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.
@magicxor
magicxor merged commit b1f4e5b into staging Mar 26, 2026
2 checks passed
@magicxor
magicxor deleted the feature/better-timeout-handling branch March 26, 2026 13:47
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants