dio icon indicating copy to clipboard operation
dio copied to clipboard

dio https connect can not keep Alive?

Open DevaLee opened this issue 1 year ago • 8 comments

Package

dio

Version

5.4.0

Operating-System

iOS

Output of flutter doctor -v

[!] Flutter (Channel unknown, 3.10.0, on macOS 14.1.1 23B81 darwin-arm64, locale
    zh-Hans-CN)
    ! Flutter version 3.10.0 on channel unknown at
      /Users/liyuchen/development/flutter
      Currently on an unknown channel. Run `flutter channel` to switch to an
      official channel.
      If that doesn't fix the issue, reinstall Flutter by following instructions
      at https://flutter.dev/docs/get-started/install.
    ! Unknown upstream repository.
      Reinstall Flutter by following instructions at
      https://flutter.dev/docs/get-started/install.
    • Framework revision 84a1e904f4 (9 months ago), 2023-05-09 07:41:44 -0700
    • Engine revision d44b5a94c9
    • Dart version 3.0.0
    • DevTools version 2.23.1
    • Pub download mirror https://pub.flutter-io.cn
    • Flutter download mirror https://storage.flutter-io.cn
    • If those were intentional, you can disregard the above warnings; however
      it is recommended to use "git" directly to perform update checks and
      upgrades.

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
    • Android SDK at /Users/liyuchen/Library/Android/sdk
    • Platform android-34, build-tools 33.0.0
    • Java binary at: /Applications/Android
      Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build
      11.0.13+0-b1751.21-8125866)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.0.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15A507
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build
      11.0.13+0-b1751.21-8125866)

[✓] VS Code (version 1.85.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension can be installed from:
      🔨 https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[✓] Connected device (3 available)
    • PGFM10 (mobile) • B6OZM755WKBA45GU • android-arm64  • Android 13 (API 33)
    • macOS (desktop) • macos            • darwin-arm64   • macOS 14.1.1 23B81
      darwin-arm64
    • Chrome (web)    • chrome           • web-javascript • Google Chrome
      120.0.6099.234
  
[✓] Network resources
    • All expected network resources are available.

Dart Version

3.10.0

Steps to Reproduce

use dio package to request, every request need TLS shakehand, I expected : use persistence connect, not every time need TLS shake hand

@override void initState() { // TODO: implement initState super.initState();

// _dio.options.connectTimeout?(Duration(seconds: 5));
_dio.httpClientAdapter = IOHttpClientAdapter(
  createHttpClient: () {
    final client = HttpClient();

    client.findProxy = (uri) {
      // Proxy all request to localhost:8888.
      // Be aware, the proxy should went through you running device,
      // not the host platform.
      return 'PROXY 192.168.1.31:8888';
    };
    client.badCertificateCallback =
        (X509Certificate cert, String host, int port) => true;
    return client;
  },

);

} void _incrementCounter() async { var response = await _dio.post("https://qa-xxxxxxxxxxxxxx/article/merchant/operate" ,data: { "type": 7, "articleId":9850245 },options: Options()..persistentConnection = true); print(response); }

Expected Result

kept Alive : true TLS handshake : -

https 连接能持久连接, 不需要每次都TLS 握手

Actual Result

image

image

image

DevaLee avatar Jan 25 '24 03:01 DevaLee

每发一次请求,Client Address的端口值都会变。 我使用 iOS原生网络请求框架 Alamofire进行网络请求时,Client Address的端口值不会变,HTTPS连接会进行持久连接,Kept Alive 值为true,TLS HandShake 值为 -

能帮我看一下,在dio中如何能 保持持久连接么?每次请求不需要重新进行 TLS 握手。

DevaLee avatar Jan 25 '24 07:01 DevaLee

I also encountered the same problem

hc79879 avatar Jan 26 '24 09:01 hc79879

same here

OnClickListener2048 avatar Feb 04 '24 09:02 OnClickListener2048

how to fix

zombiu avatar Feb 04 '24 09:02 zombiu

is this a new issue?

shaopx avatar Feb 05 '24 02:02 shaopx

I solve this by the following code: HttpClient createMyHttpClient() { return HttpClient()..idleTimeout = const Duration(seconds: 300); } _dio.httpClientAdapter = IOHttpClientAdapter(createHttpClient: createMyHttpClient);

shaopx avatar Feb 05 '24 02:02 shaopx

I solve this by the following code: HttpClient createMyHttpClient() { return HttpClient()..idleTimeout = const Duration(seconds: 300); } _dio.httpClientAdapter = IOHttpClientAdapter(createHttpClient: createMyHttpClient);

thanks, but i don't know if it really works, how can i test it ?

OnClickListener2048 avatar Feb 05 '24 02:02 OnClickListener2048

I solve this by the following code: HttpClient createMyHttpClient() { return HttpClient()..idleTimeout = const Duration(seconds: 300); } _dio.httpClientAdapter = IOHttpClientAdapter(createHttpClient: createMyHttpClient);

thanks, but i don't know if it really works, how can i test it ?

use flutter devtools --> network tab, check the connection establish time.

shaopx avatar Feb 05 '24 03:02 shaopx

idleTimeout corresponds to the alive time of the client. If your client got released in a short time, please consider increasing the configuration. It's also recommended to test with the dart:io:HttpClient and package:http.

AlexV525 avatar Mar 19 '24 08:03 AlexV525

This is why keep-alive not working. The default idleTimeout for underlying package:http is 15 seconds. However, don't know why dio set it to 3 seconds. According to package:http documentation. The non-active connection in pool will be closed by min(idleTimeout, keep-alive timeout).

https://github.com/cfug/dio/blob/cca84395f6ddf92869a48ca9de4f11d23de2fa77/dio/lib/src/adapters/io_adapter.dart#L242

zerg000000 avatar Mar 22 '24 17:03 zerg000000

Dio 10 requests with no sleep in between Dio 10 requests with 4s sleep in between dart:http 10 requests with 4s sleep in between

zerg000000 avatar Mar 22 '24 18:03 zerg000000

The HttpClient.idleTimeout of 3 seconds has already been removed on the 6.0.0 branch. The behavior will not be changed for 5.x since this is in some ways a breaking change.

kuhnroyal avatar Mar 22 '24 23:03 kuhnroyal

In order to get rid of the default 3 seconds timeout, either create a new HttpClient with a new timeout using createHttpClient or set the timeout using onHttpClientCreate (deprecated).

AlexV525 avatar Mar 24 '24 02:03 AlexV525