Files
buzz/mobile/lib/shared/http/app_http_client.dart
Tom Brow a2d4ba0267 feat(mobile): use HTTP/2 on iOS through app-wide URLSessions (#8897)
On iOS, Buzz's HTTP traffic now uses HTTP/2. Dart's `dart:io` client
speaks only HTTP/1.1, so every feature's client opened its own
connections to the relay, which sits behind Cloudflare and offers
HTTP/2. On a slow link those extra handshakes, and the HTTP/1.1
head-of-line queueing, are a large share of cold-start time.

### What changes

- iOS HTTP goes through two app-wide `NSURLSession`s via
[cupertino_http](https://pub.dev/packages/cupertino_http)
(`lib/shared/http/app_http_client.dart`): one for API traffic (relay
queries, NIP-11, account, admission, pairing) and one for media
(image/video/voice downloads and uploads). Requests to a host multiplex
over one connection per session. Media has its own session so image
bytes can't starve queries on a shared HTTP/2 connection: in prototype
measurements with a single shared session, the channel-list batch query
finished *later* than on HTTP/1.1.
- Callers keep owning and closing their clients; on iOS `close()` leaves
the shared session open. Both sessions are ephemeral with no URL cache
and no cookies, so apart from the protocol (and native gzip, inflated
off the Dart isolate) behavior matches `dart:io`.
- Android, and tests, keep `http.Client()` unchanged, including
`http.runWithClient` overrides.
- Because closing a client no longer cancels a request on a shared
session, the relay query client (both `/query` posts and bounded
snapshot reads) and the dev push-lease fetch abort their request on
timeout instead.
- Non-`https` requests (`http://` media and community icons from other
clients, local dev relays) stay on the handle's own `dart:io` client,
because App Transport Security would block cleartext loads through
URLSession. An HTTPS URL that redirects to `http://` is the one case
that newly fails on iOS (URLSession follows the redirect and ATS blocks
the downgrade); we accept that rather than follow redirects in Dart.
- URLSession failures arrive as `NSErrorClientException`, not
`SocketException`, so pairing and invite-join error messages now also
recognize `NSURLErrorDomain` offline, TLS and timeout codes.
- cupertino_http 3.0.1+ requires native_toolchain_c 0.19 and hooks 2.x,
so `buzz_secp256k1` moves to those. Its build hook compiles unchanged.

### What gets better, and what doesn't get worse

- **Better:** on a slow link, requests reuse one warm connection per
session instead of paying TCP+TLS setup each time, so they complete
where `main` times out (see the table). On Wi-Fi, small queries are
slightly faster.
- **Not worse:** request semantics, caching (none) and cookies (none)
match `dart:io`; Android and tests are untouched; timed-out requests are
aborted so they can't keep using bandwidth on the shared connection;
offline errors keep their friendly messages. The accepted regression is
HTTPS-to-HTTP redirects on iOS, above.

WebSockets stay on `dart:io`. Android (Cronet) is a possible follow-up
using the same seam.

### Validation

Added tests: shared-session handles don't close the session; off iOS the
transport honors `http.runWithClient`; NSURLError code classification;
timed-out relay queries and bounded snapshot reads abort their request;
cleartext requests stay off the shared session.

Focused existing validation: full mobile suite (the only failures are 11
push/notification tests that fail identically on `main` on the same
machine), `flutter analyze`, `buzz_secp256k1` package tests (27/27), and
an iOS release build.

On-device A/B on an iPhone 12 mini against the internal prod relay over
WARP, comparing cold starts of `main` and this branch (profile builds
with local-only request timing, 3 cold starts each):

| | `main` | this branch |
|---|---|---|
| Wi-Fi: first query done (median) | 0.31s | 0.25s |
| Wi-Fi: small-query latency (median) | 0.22s | 0.18s |
| NLC Edge: requests completed in 90s | 0, 0, 0 (29–30 failed) | 12, 14,
16 (5–7 failed) |
| NLC Edge: first request done | never | 6.3–7.0s |

On Wi-Fi the two are about even. Under Edge, every HTTP/1.1 request on
`main` hit its timeout while still opening a connection, and each retry
started a fresh TCP+TLS handshake. The branch reuses one connection, so
queries complete. Connection counts weren't measured on the device,
since no proxy was in the path.

Cold-cache demo on an iOS simulator (this branch, app caches and cached
prefs wiped before launch): avatars, community icons, channel lists,
public channel images, a DM image and custom emoji, video playback,
reactions, and an image and video upload with progress all load. The DM
image and clip are synthetic test media.


https://github.com/user-attachments/assets/039d1a25-46f2-4e0e-b147-f96d0cfabd40

buzz-review-completed (agent review and device validation done; the
human-test step is not applicable under Tom's standing exception for
mobile changes)

---------

Signed-off-by: Tom Brow <tomb@squareup.com>
Co-authored-by: Tom Brow <tomb@squareup.com>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 10:48:58 -08:00

210 lines
7.4 KiB
Dart

import 'dart:async';
import 'dart:io' show Platform;
import 'package:cupertino_http/cupertino_http.dart';
import 'package:http/http.dart' as http;
import 'package:objective_c/objective_c.dart';
import 'package:flutter/foundation.dart' show visibleForTesting;
/// Which app-wide connection pool a request belongs to.
enum HttpTraffic {
/// Relay queries, NIP-11 documents, account and admission calls.
api,
/// Image, video and voice-note downloads and media uploads.
media,
}
final _sessions = <HttpTraffic, http.Client>{};
/// An HTTP client for [traffic]. Callers own it and close it as usual.
///
/// On iOS, every client for the same [traffic] shares one `NSURLSession`, so
/// requests to a host multiplex over one HTTP/2 connection instead of opening
/// an HTTP/1.1 connection each, and gzip is inflated natively, off the Dart
/// isolate. Media has its own session, and so its own connection, so image
/// bytes cannot starve queries queued behind them on a shared connection.
///
/// Elsewhere this is `http.Client()`, which also honors `http.runWithClient`.
http.Client newAppHttpClient([HttpTraffic traffic = HttpTraffic.api]) {
if (!Platform.isIOS) return http.Client();
return SharedSessionClient(_sessions[traffic] ??= _openUrlSession());
}
/// Matches the `dart:io` transport apart from the protocol: no HTTP cache and
/// no cookies.
http.Client _openUrlSession() {
final config = URLSessionConfiguration.ephemeralSessionConfiguration()
..cache = null
..httpShouldSetCookies = false
..httpCookieAcceptPolicy =
NSHTTPCookieAcceptPolicy.NSHTTPCookieAcceptPolicyNever;
return CupertinoClient.fromSessionConfiguration(config);
}
/// A caller's handle on an app-wide session. Like closing a `dart:io` client,
/// [close] cancels the handle's requests still in flight and refuses later
/// ones, but leaves the session open for other handles. Closing a
/// `CupertinoClient` while any of its requests run would also throw.
///
/// App Transport Security blocks cleartext `URLSession` loads from public
/// hosts, while saved community icons and remote media may still use
/// `http://`. Those keep the handle's own `dart:io` client, as before.
@visibleForTesting
class SharedSessionClient extends http.BaseClient {
SharedSessionClient(this._session, {http.Client Function()? cleartext})
: _newCleartext = cleartext ?? http.Client.new;
final http.Client _session;
final http.Client Function() _newCleartext;
/// One abort trigger per request still running, so [close] can cancel it.
/// A request leaves the set once its response body is done, failed or
/// cancelled, or once the caller's own abort trigger fires. A body nobody
/// listens to stays until then: the session keeps downloading it, so
/// callers that drop a response unread must abort it.
final _inFlight = <Completer<void>>{};
var _closed = false;
http.Client? _cleartext;
@override
Future<http.StreamedResponse> send(http.BaseRequest request) async {
if (_closed) {
throw http.ClientException(
'HTTP request failed. Client is already closed.',
request.url,
);
}
if (request.url.scheme != 'https') {
return (_cleartext ??= _newCleartext()).send(request);
}
final abort = Completer<void>();
void fire() {
_inFlight.remove(abort);
if (!abort.isCompleted) abort.complete();
}
_inFlight.add(abort);
if (request case http.Abortable(:final abortTrigger?)) {
abortTrigger.then<void>((_) => fire(), onError: (_) => fire());
}
final http.StreamedResponse response;
try {
response = await _session.send(_withAbortTrigger(request, abort.future));
} catch (_) {
_inFlight.remove(abort);
rethrow;
}
// A controller, not an async* wrapper: async* never listens to the
// session's stream when cancelled before its first event, so the
// session would keep downloading a body nobody reads.
late final StreamSubscription<List<int>> inner;
final body = StreamController<List<int>>(sync: true);
body
..onListen = () {
inner = response.stream.listen(
body.add,
onError: body.addError,
onDone: () {
_inFlight.remove(abort);
body.close();
},
);
}
..onPause = (() => inner.pause())
..onResume = (() => inner.resume())
..onCancel = () {
_inFlight.remove(abort);
return inner.cancel();
};
return http.StreamedResponse(
http.ByteStream(body.stream),
response.statusCode,
contentLength: response.contentLength,
request: response.request,
headers: response.headers,
isRedirect: response.isRedirect,
persistentConnection: response.persistentConnection,
reasonPhrase: response.reasonPhrase,
);
}
/// A copy of [request] that the session cancels when [trigger] completes.
http.BaseRequest _withAbortTrigger(
http.BaseRequest request,
Future<void> trigger,
) {
if (request is http.Request) {
return http.AbortableRequest(
request.method,
request.url,
abortTrigger: trigger,
)
..bodyBytes = request.bodyBytes
..headers.addAll(request.headers)
..followRedirects = request.followRedirects
..maxRedirects = request.maxRedirects
..persistentConnection = request.persistentConnection;
}
// Finalizing first lets a multipart request set its content type.
final body = request.finalize();
final copy =
http.AbortableStreamedRequest(
request.method,
request.url,
abortTrigger: trigger,
)
..headers.addAll(request.headers)
..contentLength = request.contentLength
..followRedirects = request.followRedirects
..maxRedirects = request.maxRedirects
..persistentConnection = request.persistentConnection;
// addStream keeps backpressure: the body is read only as the session
// reads the copy, so upload progress tracks bytes the session has taken
// (including its read-ahead buffers), as with plain cupertino_http.
unawaited(copy.sink.addStream(body).whenComplete(copy.sink.close));
return copy;
}
@override
void close() {
_closed = true;
for (final abort in _inFlight.toList()) {
if (!abort.isCompleted) abort.complete();
}
_inFlight.clear();
_cleartext?.close();
}
}
/// How an iOS transport request failed, for user-facing messages. `dart:io`
/// failures are recognized by their messages instead.
enum TransportFailure { unreachable, insecure, timedOut }
/// The [TransportFailure] for a `URLSession` error, or null for any other
/// error.
TransportFailure? urlSessionFailure(Object error) {
if (error is! NSErrorClientException) return null;
if (error.error.domain.toDartString() != 'NSURLErrorDomain') return null;
return urlErrorFailure(error.error.code);
}
/// Classifies an `NSURLErrorDomain` code.
@visibleForTesting
TransportFailure? urlErrorFailure(int code) => switch (code) {
-1001 => TransportFailure.timedOut,
// Cannot find host, cannot connect, connection lost, DNS lookup failed,
// international roaming off, cellular data not allowed, offline.
-1003 ||
-1004 ||
-1005 ||
-1006 ||
-1018 ||
-1020 ||
-1009 => TransportFailure.unreachable,
// Secure connection failed and the server certificate errors.
>= -1206 && <= -1200 => TransportFailure.insecure,
_ => null,
};