Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dio https connect can not keep Alive? #2106

Closed
DevaLee opened this issue Jan 25, 2024 · 13 comments
Closed

dio https connect can not keep Alive? #2106

DevaLee opened this issue Jan 25, 2024 · 13 comments
Labels
e: performance Improvements or additions to performance fixed p: dio Targeting `dio` package platform: io

Comments

@DevaLee
Copy link

DevaLee commented Jan 25, 2024

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 DevaLee added h: need triage This issue needs to be categorized s: bug Something isn't working labels Jan 25, 2024
@DevaLee
Copy link
Author

DevaLee commented Jan 25, 2024

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

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

@hc79879
Copy link

hc79879 commented Jan 26, 2024

I also encountered the same problem

@OnClickListener2048
Copy link

same here

@zombiu
Copy link

zombiu commented Feb 4, 2024

how to fix

@shaopx
Copy link

shaopx commented Feb 5, 2024

is this a new issue?

@shaopx
Copy link

shaopx commented Feb 5, 2024

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

@OnClickListener2048
Copy link

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 ?

@shaopx
Copy link

shaopx commented Feb 5, 2024

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.

@AlexV525
Copy link
Member

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 AlexV525 added h: need more info Further information is requested and removed h: need triage This issue needs to be categorized labels Mar 19, 2024
@zerg000000
Copy link

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).

final client = HttpClient()..idleTimeout = Duration(seconds: 3);

@zerg000000
Copy link

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

@kuhnroyal
Copy link
Member

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.

@AlexV525
Copy link
Member

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 AlexV525 closed this as not planned Won't fix, can't repro, duplicate, stale Mar 24, 2024
@AlexV525 AlexV525 added p: dio Targeting `dio` package e: performance Improvements or additions to performance fixed platform: io and removed h: need more info Further information is requested s: bug Something isn't working labels Mar 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
e: performance Improvements or additions to performance fixed p: dio Targeting `dio` package platform: io
Projects
None yet
Development

No branches or pull requests

8 participants