mirror of
https://github.com/block/buzz.git
synced 2026-11-13 21:01:53 +00:00
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>
253 lines
7.8 KiB
Dart
253 lines
7.8 KiB
Dart
import 'dart:async';
|
|
import 'dart:convert';
|
|
|
|
import 'package:buzz/shared/http/app_http_client.dart';
|
|
import 'package:buzz/shared/utils/bounded_fetch.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:http/testing.dart';
|
|
|
|
void main() {
|
|
test('off iOS, clients come from http.Client()', () {
|
|
final mock = MockClient((_) async => http.Response('', 200));
|
|
for (final traffic in HttpTraffic.values) {
|
|
expect(
|
|
http.runWithClient(() => newAppHttpClient(traffic), () => mock),
|
|
same(mock),
|
|
);
|
|
}
|
|
});
|
|
|
|
test('closing a shared-session handle leaves the session usable', () async {
|
|
var sessionClosed = false;
|
|
final session = _ClosingMock(
|
|
(_) async => http.Response('ok', 200),
|
|
onClose: () => sessionClosed = true,
|
|
);
|
|
final first = SharedSessionClient(session);
|
|
final second = SharedSessionClient(session);
|
|
|
|
first.close();
|
|
|
|
expect(sessionClosed, isFalse);
|
|
expect((await second.get(Uri.parse('https://relay.example/'))).body, 'ok');
|
|
});
|
|
|
|
test('closing a handle cancels its own in-flight requests only', () async {
|
|
final session = _AbortRecorder();
|
|
final first = SharedSessionClient(session);
|
|
final second = SharedSessionClient(session);
|
|
unawaited(
|
|
first
|
|
.get(Uri.parse('https://relay.example/a'))
|
|
.then<void>((_) {}, onError: (_) {}),
|
|
);
|
|
unawaited(
|
|
second
|
|
.get(Uri.parse('https://relay.example/b'))
|
|
.then<void>((_) {}, onError: (_) {}),
|
|
);
|
|
await pumpEventQueue();
|
|
|
|
first.close();
|
|
await pumpEventQueue();
|
|
|
|
expect(session.aborted, [Uri.parse('https://relay.example/a')]);
|
|
await expectLater(
|
|
first.get(Uri.parse('https://relay.example/c')),
|
|
throwsA(isA<http.ClientException>()),
|
|
);
|
|
});
|
|
|
|
test('finished requests hold no abort trigger until close', () async {
|
|
var fired = 0;
|
|
var sessionBodyCancelled = false;
|
|
final client = SharedSessionClient(
|
|
MockClient.streaming((request, _) async {
|
|
(request as http.Abortable).abortTrigger?.then((_) => fired++);
|
|
if (request.url.path == '/video') {
|
|
final body = StreamController<List<int>>(
|
|
onCancel: () => sessionBodyCancelled = true,
|
|
);
|
|
return http.StreamedResponse(body.stream, 500);
|
|
}
|
|
final status = request.url.path == '/missing' ? 404 : 200;
|
|
return http.StreamedResponse(Stream.value([1]), status);
|
|
}),
|
|
);
|
|
for (var i = 0; i < 3; i++) {
|
|
await client.get(Uri.parse('https://relay.example/$i'));
|
|
}
|
|
// A non-200 body is never read, and boundedGet fires its own trigger.
|
|
final missing = await boundedGet(
|
|
client,
|
|
Uri.parse('https://relay.example/missing'),
|
|
maxBytes: 10,
|
|
);
|
|
expect(missing.statusCode, 404);
|
|
await pumpEventQueue();
|
|
// A body cancelled before its first event still cancels the session's
|
|
// download.
|
|
final video = await client.send(
|
|
http.Request('GET', Uri.parse('https://relay.example/video')),
|
|
);
|
|
await video.stream.listen(null).cancel();
|
|
expect(sessionBodyCancelled, isTrue);
|
|
final ownAborts = fired;
|
|
|
|
client.close();
|
|
await pumpEventQueue();
|
|
|
|
expect(fired, ownAborts);
|
|
});
|
|
|
|
test('a request\'s own abort trigger still cancels it', () async {
|
|
final session = _AbortRecorder();
|
|
final client = SharedSessionClient(session);
|
|
final abort = Completer<void>();
|
|
unawaited(
|
|
client
|
|
.send(
|
|
http.AbortableRequest(
|
|
'GET',
|
|
Uri.parse('https://relay.example/a'),
|
|
abortTrigger: abort.future,
|
|
),
|
|
)
|
|
.then<void>((_) {}, onError: (_) {}),
|
|
);
|
|
await pumpEventQueue();
|
|
|
|
abort.complete();
|
|
await pumpEventQueue();
|
|
|
|
expect(session.aborted, [Uri.parse('https://relay.example/a')]);
|
|
});
|
|
|
|
test('requests reach the session with their headers and body', () async {
|
|
final received =
|
|
<({String method, Map<String, String> headers, String body})>[];
|
|
final client = SharedSessionClient(
|
|
MockClient.streaming((request, body) async {
|
|
received.add((
|
|
method: request.method,
|
|
headers: request.headers,
|
|
body: utf8.decode(await body.toBytes()),
|
|
));
|
|
return http.StreamedResponse(const Stream.empty(), 200);
|
|
}),
|
|
);
|
|
|
|
await client.post(
|
|
Uri.parse('https://relay.example/query'),
|
|
headers: {'authorization': 'Nostr x'},
|
|
body: '[]',
|
|
);
|
|
final upload = http.MultipartRequest(
|
|
'PUT',
|
|
Uri.parse('https://relay.example/upload'),
|
|
)..fields['name'] = 'value';
|
|
await client.send(upload);
|
|
|
|
expect(received[0].method, 'POST');
|
|
expect(received[0].headers['authorization'], 'Nostr x');
|
|
expect(received[0].body, '[]');
|
|
expect(received[1].method, 'PUT');
|
|
expect(
|
|
received[1].headers['content-type'],
|
|
startsWith('multipart/form-data'),
|
|
);
|
|
expect(received[1].body, contains('value'));
|
|
});
|
|
|
|
test(
|
|
'a streamed body is read only as fast as the session reads it',
|
|
() async {
|
|
var produced = 0;
|
|
Stream<List<int>> chunks() async* {
|
|
for (var i = 0; i < 100; i++) {
|
|
produced++;
|
|
yield [i];
|
|
}
|
|
}
|
|
|
|
final request = http.StreamedRequest(
|
|
'PUT',
|
|
Uri.parse('https://relay.example/upload'),
|
|
)..contentLength = 100;
|
|
unawaited(
|
|
request.sink.addStream(chunks()).whenComplete(request.sink.close),
|
|
);
|
|
final client = SharedSessionClient(_AbortRecorder());
|
|
unawaited(client.send(request).then<void>((_) {}, onError: (_) {}));
|
|
await pumpEventQueue();
|
|
|
|
// Upload progress counts bytes as they are produced, so a copy that
|
|
// drained the body ahead of the transport would report it sent.
|
|
expect(produced, lessThan(10));
|
|
client.close();
|
|
},
|
|
);
|
|
|
|
test('cleartext requests use the handle\'s own client', () async {
|
|
final sessionUrls = <Uri>[];
|
|
final cleartextUrls = <Uri>[];
|
|
var cleartextClosed = false;
|
|
final client = SharedSessionClient(
|
|
MockClient((request) async {
|
|
sessionUrls.add(request.url);
|
|
return http.Response('', 200);
|
|
}),
|
|
cleartext: () => _ClosingMock((request) async {
|
|
cleartextUrls.add(request.url);
|
|
return http.Response('', 200);
|
|
}, onClose: () => cleartextClosed = true),
|
|
);
|
|
|
|
await client.get(Uri.parse('https://relay.example/a'));
|
|
await client.get(Uri.parse('http://icons.example/b'));
|
|
client.close();
|
|
|
|
expect(sessionUrls, [Uri.parse('https://relay.example/a')]);
|
|
expect(cleartextUrls, [Uri.parse('http://icons.example/b')]);
|
|
expect(cleartextClosed, isTrue);
|
|
});
|
|
|
|
test('URLSession error codes map to user-facing failure kinds', () {
|
|
expect(urlErrorFailure(-1009), TransportFailure.unreachable);
|
|
expect(urlErrorFailure(-1004), TransportFailure.unreachable);
|
|
expect(urlErrorFailure(-1001), TransportFailure.timedOut);
|
|
expect(urlErrorFailure(-1200), TransportFailure.insecure);
|
|
expect(urlErrorFailure(-1202), TransportFailure.insecure);
|
|
expect(urlErrorFailure(-999), isNull);
|
|
expect(urlSessionFailure(http.ClientException('offline')), isNull);
|
|
});
|
|
}
|
|
|
|
class _ClosingMock extends MockClient {
|
|
_ClosingMock(super.fn, {required this.onClose});
|
|
|
|
final void Function() onClose;
|
|
|
|
@override
|
|
void close() {
|
|
onClose();
|
|
super.close();
|
|
}
|
|
}
|
|
|
|
/// Never responds; records which requests were aborted.
|
|
class _AbortRecorder extends http.BaseClient {
|
|
final aborted = <Uri>[];
|
|
|
|
@override
|
|
Future<http.StreamedResponse> send(http.BaseRequest request) {
|
|
final response = Completer<http.StreamedResponse>();
|
|
(request as http.Abortable).abortTrigger?.then((_) {
|
|
aborted.add(request.url);
|
|
response.completeError(http.RequestAbortedException(request.url));
|
|
});
|
|
return response.future;
|
|
}
|
|
}
|