Skip to content

Commit d1a8c36

Browse files
browsquare-tomb
andauthored
perf(mobile): use HTTP/2 on Android through app-wide Cronet engines (#9435)
Follows #8897 (iOS HTTP/2), now on main. ## What Android gets the same app-wide shared sessions as iOS, backed by Cronet (`cronet_http`) instead of `NSURLSession`: - One Cronet engine per traffic class (API, media), so requests to a host multiplex over one HTTP/2 connection and gzip is inflated natively, off the Dart isolate. - HTTP/2 on, HTTP cache off, QUIC off. QUIC would be a separate behavior change, and `cronet_http`'s changelog lists DNS failures with QUIC on cellular. - Streamed uploads (media and voice notes) stay on `dart:io` on Android: `CronetClient` reads a request body into memory before sending it, which would break upload progress and copy large attachments in full. - If no Cronet provider is available (no Google Play services), the engine fails to build and the app keeps `dart:io`, as today. - Transport error classification covers Cronet failures alongside `NSURLSession` ones, so invite and pairing errors keep their friendly messages. ## Measurements Android emulator (API 36, arm64), profile builds of this branch and main, signed in to the Feedback community. 8 cold starts per build, alternating builds in blocks of 4. Times are for relay `/query` requests started in the first 8 s. | | main (`dart:io`) | this branch (Cronet) | |---|---|---| | First query response | 6.99 s | 1.99 s | | Startup burst settled | 8.36 s | 2.78 s | | Burst after the first connection | 1.44 s | 0.77 s | | Query latency after the first connection, median / p90 | 165 / 1567 ms | 106 / 158 ms | **Unexplained stall:** most of main's startup gap is a ~5 s stall before its first response on every cold start. Its cause is unconfirmed; my guess is `dart:io` loading its CA store on the emulator, which HTTP/2 itself wouldn't fix, so the first two rows likely overstate the win on real devices. The rows after the first connection exclude the stall and are the fairer comparison. I also tried emulated EDGE throttling, but the emulator's throttle did not slow its traffic, so there are no slow-link numbers yet. ## Regressions None found. Devices without Play services keep `dart:io`. Cleartext `http://` requests keep the handle's own `dart:io` client, as on iOS. ## Testing - Full mobile suite passes (`flutter test`, 4784 tests). - Automated app run on the emulator: onboarding, Feedback nsec import, channel and DM list load, and 16 cold starts with request timing. Media loading wasn't separately exercised. - No visible UI change, so no screenshots. --------- Signed-off-by: Tom Brow <[email protected]> Co-authored-by: Tom Brow <[email protected]>
1 parent 36fa0fe commit d1a8c36

8 files changed

Lines changed: 191 additions & 43 deletions

File tree

‎mobile/lib/features/channels/channels_page/badges.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class _ErrorView extends HookConsumerWidget {
4747
return 'Server error (${error.statusCode}). Try again later.';
4848
}
4949
if (error is SocketException ||
50-
urlSessionFailure(error) == TransportFailure.unreachable) {
50+
transportFailure(error) == TransportFailure.unreachable) {
5151
return 'Could not reach the relay server.';
5252
}
5353
return 'Something went wrong. Check your connection.';

‎mobile/lib/features/invites/invite_join_provider.dart‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -516,14 +516,14 @@ String _friendlyInviteError(Object error) {
516516
if (message.contains('join_policy_required')) {
517517
return 'Review this community’s join requirements in your browser, then return to Buzz.';
518518
}
519-
final transportFailure = urlSessionFailure(error);
520-
if (transportFailure == TransportFailure.insecure) {
519+
final failure = transportFailure(error);
520+
if (failure == TransportFailure.insecure) {
521521
return 'Secure connection failed. Check your network settings and try again.';
522522
}
523-
if (transportFailure == TransportFailure.timedOut) {
523+
if (failure == TransportFailure.timedOut) {
524524
return 'Connection timed out. Check your internet connection and try again.';
525525
}
526-
if (transportFailure == TransportFailure.unreachable ||
526+
if (failure == TransportFailure.unreachable ||
527527
message.contains('SocketException') ||
528528
message.contains('Connection refused') ||
529529
message.contains('Network is unreachable') ||
@@ -535,7 +535,7 @@ String _friendlyInviteError(Object error) {
535535

536536
String _friendlyStarterSetupError(Object error) {
537537
final message = error.toString();
538-
if (urlSessionFailure(error) == TransportFailure.unreachable ||
538+
if (transportFailure(error) == TransportFailure.unreachable ||
539539
message.contains('SocketException') ||
540540
message.contains('Connection refused') ||
541541
message.contains('Network is unreachable') ||

‎mobile/lib/features/pairing/pairing_provider_helpers.dart‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ part of 'pairing_provider.dart';
22

33
String _friendlyErrorMessage(Object error) {
44
final message = error.toString();
5-
final transportFailure = urlSessionFailure(error);
6-
if (transportFailure == TransportFailure.unreachable ||
5+
final failure = transportFailure(error);
6+
if (failure == TransportFailure.unreachable ||
77
message.contains('SocketException') ||
88
message.contains('Connection refused') ||
99
message.contains('Network is unreachable') ||
@@ -20,13 +20,13 @@ String _friendlyErrorMessage(Object error) {
2020
message.contains('Null check operator used on a null value')) {
2121
return 'Pairing stopped because of an internal error. Please try again.';
2222
}
23-
if (transportFailure == TransportFailure.insecure ||
23+
if (failure == TransportFailure.insecure ||
2424
message.contains('HandshakeException') ||
2525
message.contains('CERTIFICATE_VERIFY_FAILED')) {
2626
return 'Secure connection failed. Check your network settings '
2727
'and try again.';
2828
}
29-
if (transportFailure == TransportFailure.timedOut ||
29+
if (failure == TransportFailure.timedOut ||
3030
message.contains('TimeoutException') ||
3131
message.contains('timed out')) {
3232
return 'Connection timed out. Check your internet connection and '

‎mobile/lib/shared/http/app_http_client.dart‎

Lines changed: 98 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import 'dart:async';
22
import 'dart:io' show Platform;
33

4+
import 'package:cronet_http/cronet_http.dart';
45
import 'package:cupertino_http/cupertino_http.dart';
56
import 'package:http/http.dart' as http;
7+
import 'package:jni/jni.dart' show JThrowable;
68
import 'package:objective_c/objective_c.dart';
7-
import 'package:flutter/foundation.dart' show visibleForTesting;
9+
import 'package:flutter/foundation.dart' show debugPrint, visibleForTesting;
810

911
/// Which app-wide connection pool a request belongs to.
1012
enum HttpTraffic {
@@ -15,20 +17,34 @@ enum HttpTraffic {
1517
media,
1618
}
1719

18-
final _sessions = <HttpTraffic, http.Client>{};
20+
/// The app-wide session for each traffic class, or null where the platform
21+
/// has none.
22+
final _sessions = <HttpTraffic, http.Client?>{};
1923

2024
/// An HTTP client for [traffic]. Callers own it and close it as usual.
2125
///
22-
/// On iOS, every client for the same [traffic] shares one `NSURLSession`, so
23-
/// requests to a host multiplex over one HTTP/2 connection instead of opening
24-
/// an HTTP/1.1 connection each, and gzip is inflated natively, off the Dart
25-
/// isolate. Media has its own session, and so its own connection, so image
26-
/// bytes cannot starve queries queued behind them on a shared connection.
26+
/// On iOS and Android, every client for the same [traffic] shares one native
27+
/// session (`NSURLSession` or a Cronet engine), so requests to a host
28+
/// multiplex over one HTTP/2 connection instead of opening an HTTP/1.1
29+
/// connection each, and gzip is inflated natively, off the Dart isolate.
30+
/// Media has its own session, and so its own connection, so image bytes
31+
/// cannot starve queries queued behind them on a shared connection.
2732
///
2833
/// Elsewhere this is `http.Client()`, which also honors `http.runWithClient`.
2934
http.Client newAppHttpClient([HttpTraffic traffic = HttpTraffic.api]) {
30-
if (!Platform.isIOS) return http.Client();
31-
return SharedSessionClient(_sessions[traffic] ??= _openUrlSession());
35+
final session = _sessions.putIfAbsent(traffic, _openNativeSession);
36+
if (session == null) return http.Client();
37+
return SharedSessionClient(session, streamsUploads: !Platform.isAndroid);
38+
}
39+
40+
/// Whether [newAppHttpClient] uses a native session on this device.
41+
bool get hasNativeHttpSession =>
42+
_sessions.putIfAbsent(HttpTraffic.api, _openNativeSession) != null;
43+
44+
http.Client? _openNativeSession() {
45+
if (Platform.isIOS) return _openUrlSession();
46+
if (Platform.isAndroid) return _openCronetSession();
47+
return null;
3248
}
3349

3450
/// Matches the `dart:io` transport apart from the protocol: no HTTP cache and
@@ -42,22 +58,55 @@ http.Client _openUrlSession() {
4258
return CupertinoClient.fromSessionConfiguration(config);
4359
}
4460

61+
/// Matches the `dart:io` transport apart from the protocol: no HTTP cache,
62+
/// and Cronet keeps no cookies. QUIC stays off, so this changes only the
63+
/// HTTP version.
64+
///
65+
/// Null when no Cronet provider is available: Cronet comes from Google Play
66+
/// services, which some Android devices don't have. Those keep `dart:io`.
67+
http.Client? _openCronetSession() {
68+
final CronetEngine engine;
69+
try {
70+
engine = CronetEngine.build(
71+
cacheMode: CacheMode.disabled,
72+
enableHttp2: true,
73+
enableQuic: false,
74+
);
75+
} on JThrowable catch (error) {
76+
debugPrint('Cronet is unavailable, using dart:io: ${error.message}');
77+
return null;
78+
}
79+
return CronetClient.fromCronetEngine(engine);
80+
}
81+
4582
/// A caller's handle on an app-wide session. Like closing a `dart:io` client,
4683
/// [close] cancels the handle's requests still in flight and refuses later
4784
/// ones, but leaves the session open for other handles. Closing a
48-
/// `CupertinoClient` while any of its requests run would also throw.
85+
/// `CupertinoClient` while any of its requests run would also throw, and
86+
/// closing a `CronetClient` would stop the shared engine.
4987
///
5088
/// App Transport Security blocks cleartext `URLSession` loads from public
5189
/// hosts, while saved community icons and remote media may still use
5290
/// `http://`. Those keep the handle's own `dart:io` client, as before.
91+
///
92+
/// Unless [streamsUploads], a streamed request with a body (media and voice
93+
/// note uploads) also keeps `dart:io`: `CronetClient` reads the whole body
94+
/// into memory before sending it, so progress would reach 100% before any
95+
/// byte left and a large attachment would be copied in full.
5396
@visibleForTesting
5497
class SharedSessionClient extends http.BaseClient {
55-
SharedSessionClient(this._session, {http.Client Function()? cleartext})
56-
: _newCleartext = cleartext ?? http.Client.new;
98+
SharedSessionClient(
99+
this._session, {
100+
http.Client Function()? cleartext,
101+
this.streamsUploads = true,
102+
}) : _newCleartext = cleartext ?? http.Client.new;
57103

58104
final http.Client _session;
59105
final http.Client Function() _newCleartext;
60106

107+
/// Whether the session streams request bodies instead of buffering them.
108+
final bool streamsUploads;
109+
61110
/// One abort trigger per request still running, so [close] can cancel it.
62111
/// A request leaves the set once its response body is done, failed or
63112
/// cancelled, or once the caller's own abort trigger fires. A body nobody
@@ -75,7 +124,8 @@ class SharedSessionClient extends http.BaseClient {
75124
request.url,
76125
);
77126
}
78-
if (request.url.scheme != 'https') {
127+
if (request.url.scheme != 'https' ||
128+
(!streamsUploads && _isStreamedUpload(request))) {
79129
return (_cleartext ??= _newCleartext()).send(request);
80130
}
81131
final abort = Completer<void>();
@@ -130,6 +180,11 @@ class SharedSessionClient extends http.BaseClient {
130180
);
131181
}
132182

183+
static bool _isStreamedUpload(http.BaseRequest request) =>
184+
request is! http.Request &&
185+
request.method != 'GET' &&
186+
request.method != 'HEAD';
187+
133188
/// A copy of [request] that the session cancels when [trigger] completes.
134189
http.BaseRequest _withAbortTrigger(
135190
http.BaseRequest request,
@@ -178,17 +233,20 @@ class SharedSessionClient extends http.BaseClient {
178233
}
179234
}
180235

181-
/// How an iOS transport request failed, for user-facing messages. `dart:io`
182-
/// failures are recognized by their messages instead.
236+
/// How a native transport request failed, for user-facing messages.
237+
/// `dart:io` failures are recognized by their messages instead.
183238
enum TransportFailure { unreachable, insecure, timedOut }
184239

185-
/// The [TransportFailure] for a `URLSession` error, or null for any other
186-
/// error.
187-
TransportFailure? urlSessionFailure(Object error) {
188-
if (error is! NSErrorClientException) return null;
189-
if (error.error.domain.toDartString() != 'NSURLErrorDomain') return null;
190-
return urlErrorFailure(error.error.code);
191-
}
240+
/// The [TransportFailure] for a `URLSession` or Cronet error, or null for
241+
/// any other error.
242+
TransportFailure? transportFailure(Object error) => switch (error) {
243+
NSErrorClientException(:final error)
244+
when error.domain.toDartString() == 'NSURLErrorDomain' =>
245+
urlErrorFailure(error.code),
246+
NetworkException(:final errorCode, :final cronetInternalErrorCode) =>
247+
cronetErrorFailure(errorCode, cronetInternalErrorCode),
248+
_ => null,
249+
};
192250

193251
/// Classifies an `NSURLErrorDomain` code.
194252
@visibleForTesting
@@ -207,3 +265,21 @@ TransportFailure? urlErrorFailure(int code) => switch (code) {
207265
>= -1206 && <= -1200 => TransportFailure.insecure,
208266
_ => null,
209267
};
268+
269+
/// Classifies a Cronet `NetworkException` by its `ERROR_*` code and, for
270+
/// `ERROR_OTHER`, Chromium's internal net error code.
271+
@visibleForTesting
272+
TransportFailure? cronetErrorFailure(int errorCode, int internalCode) =>
273+
switch (errorCode) {
274+
4 || 6 => TransportFailure.timedOut,
275+
// Host not resolved, offline, network changed, connection closed,
276+
// refused or reset, address unreachable.
277+
1 || 2 || 3 || 5 || 7 || 8 || 9 => TransportFailure.unreachable,
278+
// SSL protocol error, cipher mismatch, and the certificate errors.
279+
11
280+
when internalCode == -107 ||
281+
internalCode == -113 ||
282+
(internalCode <= -200 && internalCode >= -299) =>
283+
TransportFailure.insecure,
284+
_ => null,
285+
};

‎mobile/lib/shared/relay/relay_http_query_client.dart‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ import 'package:http/http.dart' as http;
55
import 'package:http/io_client.dart';
66
import 'package:buzz/shared/http/app_http_client.dart';
77

8-
/// The default transport. On iOS, the app-wide `NSURLSession` inflates gzip
9-
/// natively, off the Dart isolate. `dart:io` would inflate on the calling
10-
/// isolate, so leave its responses compressed for the response decoder.
8+
/// The default transport. On iOS and Android, the app-wide native session
9+
/// inflates gzip natively, off the Dart isolate. `dart:io` would inflate on
10+
/// the calling isolate, so leave its responses compressed for the response
11+
/// decoder.
1112
///
1213
/// A non-`dart:io` client supplied through `http.runWithClient`, such as a
1314
/// `MockClient`, is used as is. An `IOClient` is replaced, not closed, since
1415
/// it may be shared.
1516
http.Client _defaultTransport() {
16-
if (Platform.isIOS) return newAppHttpClient();
17+
if (hasNativeHttpSession) return newAppHttpClient();
1718
final client = http.Client();
1819
if (client is! IOClient) return client;
1920
return IOClient(HttpClient()..autoUncompress = false);

‎mobile/pubspec.lock‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,14 @@ packages:
288288
url: "https://pub.dev"
289289
source: hosted
290290
version: "1.15.0"
291+
cronet_http:
292+
dependency: "direct main"
293+
description:
294+
name: cronet_http
295+
sha256: "7f8976ce1531399e42b4c5ad0937bd57bd93fbcff564f7d51c695be19850e977"
296+
url: "https://pub.dev"
297+
source: hosted
298+
version: "1.10.1"
291299
cross_file:
292300
dependency: transitive
293301
description:
@@ -808,13 +816,13 @@ packages:
808816
source: hosted
809817
version: "1.0.5"
810818
jni:
811-
dependency: transitive
819+
dependency: "direct main"
812820
description:
813821
name: jni
814-
sha256: c2230682d5bc2362c1c9e8d3c7f406d9cbba23ab3f2e203a025dd47e0fb2e68f
822+
sha256: "2e91e6087291affd10b31b6c872f3e3ac7dc5d93b51b5ba485d930c8f08bde6c"
815823
url: "https://pub.dev"
816824
source: hosted
817-
version: "1.0.0"
825+
version: "1.1.2"
818826
jni_flutter:
819827
dependency: transitive
820828
description:
@@ -823,6 +831,14 @@ packages:
823831
url: "https://pub.dev"
824832
source: hosted
825833
version: "1.0.1"
834+
jni_util:
835+
dependency: transitive
836+
description:
837+
name: jni_util
838+
sha256: "1ba86da04a5f2bf18fde2edb235587e70c5b0fc5bd4ba955f46b00942c3fc35f"
839+
url: "https://pub.dev"
840+
source: hosted
841+
version: "1.0.0"
826842
json_annotation:
827843
dependency: transitive
828844
description:

‎mobile/pubspec.yaml‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ dependencies:
1818
hooks_riverpod: ^3.0.3
1919
flutter_hooks: ^0.21.3
2020
http: ^1.4.0
21-
# iOS HTTP transport (NSURLSession): HTTP/2 and native gzip. See
22-
# lib/shared/http/app_http_client.dart.
21+
# Native HTTP transports, NSURLSession on iOS and Cronet on Android: HTTP/2
22+
# and native gzip. See lib/shared/http/app_http_client.dart.
23+
cronet_http: ^1.10.1
2324
cupertino_http: ^3.1.0
25+
jni: ^1.1.0
2426
objective_c: ^9.5.0
2527
crypto: ^3.0.7
2628
flutter_secure_storage: ^10.0.0

0 commit comments

Comments
 (0)