Skip to content

Add connectTimeout support to Options class - #2464

Merged
AlexV525 merged 6 commits into
mainfrom
copilot/set-connect-timeout-option
Nov 24, 2025
Merged

AlexV525 merged 6 commits into
mainfrom
copilot/set-connect-timeout-option

Conversation

Copilot AI commented Nov 23, 2025 •

Copy link
Copy Markdown
Contributor

Enables per-request connection timeout override via Options, matching existing sendTimeout/receiveTimeout pattern.

Changes

  • Options class: Added connectTimeout parameter with validation (constructor, getter/setter, copyWith())
  • Composition logic: Options.compose() now merges connectTimeout with Options taking precedence over BaseOptions
  • Tests: Added comprehensive coverage in options_test.dart, connect_timeout_integration_test.dart, and options_timeout_integration_test.dart
  • Timeout integration tests: Added comprehensive tests demonstrating that all timeout types (connectTimeout, sendTimeout, receiveTimeout) work consistently when set via Options, validating that they correctly override BaseOptions values in real-world usage

Adapter Support

All adapters (IOHttpClientAdapter, BrowserHttpClientAdapter, Http2Adapter, and native adapters) already use timeout values from RequestOptions. The adapters automatically pick up all timeout values from RequestOptions, which is populated by Options.compose(). No adapter code changes were needed.

Usage

// Override base timeout for specific request
await dio.get('/endpoint', 
  options: Options(connectTimeout: const Duration(seconds: 10))
);

// Set multiple timeouts simultaneously
await dio.request('https://api.example.com',
  options: Options(
    connectTimeout: const Duration(seconds: 10),
    sendTimeout: const Duration(seconds: 15),
    receiveTimeout: const Duration(seconds: 20),
  ),
);

New Pull Request Checklist

  • I have read the Documentation
  • I have searched for a similar pull request in the project and found none
  • I have updated this branch with the latest main branch to avoid conflicts (via merge from master or rebase)
  • I have added the required tests to prove the fix/feature I'm adding
  • I have updated the documentation (if necessary)
  • I have run the tests without failures
  • I have updated the CHANGELOG.md in the corresponding package

Additional context and info (if any)

Implementation follows existing timeout field patterns. Maintains backward compatibility with no breaking changes.

Integration tests validate that all timeout types (connectTimeout, sendTimeout, receiveTimeout) work consistently with actual adapters in real-world scenarios. Tests confirm that Options values correctly override BaseOptions values for all three timeout types.

Fixes #2454

Original prompt

This section details on the original issue you should resolve

<issue_title>Set connectTimeout through Options</issue_title>
<issue_description>### Request Statement

Dio().request('http://example.com', options: Options(connectTimeout: const Duration(seconds: 10)));

Solution Brainstorm

 Options({
   this.method,
   Duration? sendTimeout,
   Duration? receiveTimeout,
+  Duration? connectTimeout,
   this.extra,
   this.headers,
   this.preserveHeaderCase,
   this.responseType,
   this.contentType,
   this.validateStatus,
   this.receiveDataWhenStatusError,
   this.followRedirects,
   this.maxRedirects,
   this.persistentConnection,
   this.requestEncoder,
   this.responseDecoder,
   this.listFormat,
 });
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits November 23, 2025 11:11
- Added connectTimeout parameter to Options constructor
- Added connectTimeout field with getter/setter validation
- Updated Options.copyWith to support connectTimeout
- Updated Options.compose to merge connectTimeout from Options with BaseOptions
- Added comprehensive tests for connectTimeout functionality

Co-authored-by: AlexV525 <[email protected]>
- Remove unused variable in test
- Apply dart format to ensure consistent formatting

Co-authored-by: AlexV525 <[email protected]>
Copilot AI changed the title [WIP] Add connectTimeout option to request options Add connectTimeout support to Options class Nov 23, 2025
Copilot AI requested a review from AlexV525 November 23, 2025 11:17
@AlexV525

Copy link
Copy Markdown
Member

@copilot Added connectTimeout also needs to be used in applicable adapters as for real-world usages.

…adapter

- Added test showing Options.connectTimeout overrides BaseOptions.connectTimeout
- Test verifies the adapter uses the Options timeout (100ms) not BaseOptions (30s)
- Test validates compose behavior for connectTimeout merging

Co-authored-by: AlexV525 <[email protected]>

Copilot AI commented Nov 23, 2025

Copy link
Copy Markdown
Contributor Author

@copilot Added connectTimeout also needs to be used in applicable adapters as for real-world usages.

All adapters (IOHttpClientAdapter, BrowserHttpClientAdapter, Http2Adapter, and native adapters) already use options.connectTimeout from RequestOptions. Since Options.compose() now merges connectTimeout into RequestOptions, the adapters automatically pick it up without any code changes needed.

I've added an integration test (commit 5ecfad9) that demonstrates the feature working end-to-end with the adapter. The test shows that Options.connectTimeout (100ms) correctly overrides BaseOptions.connectTimeout (30s), with the request timing out in ~126ms as expected.

- Clean up test output by removing debug print statements
- Test still validates behavior correctly

Co-authored-by: AlexV525 <[email protected]>
@AlexV525

Copy link
Copy Markdown
Member

@copilot Also check if sendTimeout and receiveTimeout can be update accordingly

- Renamed test file from options_connect_timeout_integration_test to options_timeout_integration_test
- Added tests demonstrating Options.sendTimeout overrides BaseOptions.sendTimeout
- Added tests demonstrating Options.receiveTimeout overrides BaseOptions.receiveTimeout
- Added test showing all three timeout types can be set simultaneously via Options
- All timeout types (connectTimeout, sendTimeout, receiveTimeout) now have consistent test coverage

Co-authored-by: AlexV525 <[email protected]>

Copilot AI commented Nov 23, 2025

Copy link
Copy Markdown
Contributor Author

@copilot Also check if sendTimeout and receiveTimeout can be update accordingly

Checked and verified. sendTimeout and receiveTimeout already exist in the Options class following the same pattern as connectTimeout. I've added comprehensive integration tests (commit a1f63e4) demonstrating that all three timeout types work consistently:

  • Options.sendTimeout correctly overrides BaseOptions.sendTimeout
  • Options.receiveTimeout correctly overrides BaseOptions.receiveTimeout
  • All three timeout types can be set simultaneously via Options

All tests pass, confirming the feature works uniformly across all timeout types.

@AlexV525
AlexV525 marked this pull request as ready for review November 24, 2025 00:21
@AlexV525
AlexV525 requested a review from a team as a code owner November 24, 2025 00:21
@AlexV525
AlexV525 merged commit d4576ab into main Nov 24, 2025
6 checks passed
@AlexV525
AlexV525 deleted the copilot/set-connect-timeout-option branch November 24, 2025 02:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Set connectTimeout through Options

3 participants