Skip to content

Commit 1b58049

Browse files
fryetteclaude
andcommitted
[cronet_http] Add DNS configuration options to CronetEngine.build
Expose Cronet DnsOptions through new CronetEngine.build parameters: useBuiltInDnsResolver, enableStaleDns, persistHostCache and persistHostCachePeriod. Cronet enables QUIC by default, and with QUIC enabled it resolves hosts through its built-in DNS resolver. On some cellular networks and in background isolates (e.g. WorkManager) that resolver fails with ERROR_HOSTNAME_NOT_RESOLVED while the system resolver works. useBuiltInDnsResolver: false forces the system resolver; enableStaleDns/persistHostCache allow a freshly created engine to serve recently used hosts from a persisted cache. setDnsOptions is available on the stable CronetEngine.Builder in both cronet-api artifacts the plugin ships (play-services and embedded), and the Java API layer transparently falls back to an experimental-options JSON patch on implementations that do not support DnsOptions natively, so no experimental bindings are required. Helps with dart-lang#1217 Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 5d94ef5 commit 1b58049

6 files changed

Lines changed: 1038 additions & 4 deletions

File tree

‎pkgs/cronet_http/CHANGELOG.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
## 1.10.0-wip
2+
3+
* Add DNS configuration options to `CronetEngine.build`:
4+
`useBuiltInDnsResolver`, `enableStaleDns`, `persistHostCache` and
5+
`persistHostCachePeriod`. Setting `useBuiltInDnsResolver: false` forces the
6+
system DNS resolver, which works around `ERROR_HOSTNAME_NOT_RESOLVED`
7+
failures seen with QUIC enabled on some cellular networks and in background
8+
isolates (https://github.com/dart-lang/http/issues/1217).
9+
110
## 1.9.0
211

312
* Add `CronetEngine.startNetLogToFile` and `CronetEngine.stopNetLog`.

‎pkgs/cronet_http/example/integration_test/cronet_engine_test.dart‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,48 @@ void testQuicHints() {
149149
});
150150
}
151151

152+
void testDnsOptions() {
153+
group('dnsOptions', () {
154+
late HttpServer server;
155+
156+
setUp(() async {
157+
server = (await HttpServer.bind('localhost', 0))
158+
..listen((request) async {
159+
await request.drain<void>();
160+
request.response.headers.set('Content-Type', 'text/plain');
161+
await request.response.close();
162+
});
163+
});
164+
tearDown(() {
165+
server.close();
166+
});
167+
168+
test('system resolver', () async {
169+
final engine = CronetEngine.build(useBuiltInDnsResolver: false);
170+
final client = CronetClient.fromCronetEngine(engine, closeEngine: true);
171+
final response =
172+
await client.get(Uri.parse('http://localhost:${server.port}'));
173+
expect(response.statusCode, 200);
174+
client.close();
175+
});
176+
177+
test('persistent host cache', () async {
178+
final engine = CronetEngine.build(
179+
cacheMode: CacheMode.disk,
180+
cacheMaxSize: 1024 * 1024,
181+
storagePath: (await Directory.systemTemp.createTemp()).absolute.path,
182+
enableStaleDns: true,
183+
persistHostCache: true,
184+
persistHostCachePeriod: const Duration(seconds: 1));
185+
final client = CronetClient.fromCronetEngine(engine, closeEngine: true);
186+
final response =
187+
await client.get(Uri.parse('http://localhost:${server.port}'));
188+
expect(response.statusCode, 200);
189+
client.close();
190+
});
191+
});
192+
}
193+
152194
void testNetLog() {
153195
group('net log', () {
154196
late HttpServer server;
@@ -226,6 +268,7 @@ void main() {
226268
testInvalidConfigurations();
227269
testUserAgent();
228270
testQuicHints();
271+
testDnsOptions();
229272
testNetLog();
230273
testEngineClose();
231274
}

‎pkgs/cronet_http/jnigen.yaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ classes:
1717
- 'java.net.URL'
1818
- 'java.util.concurrent.Executors'
1919
- 'org.chromium.net.CronetEngine'
20+
- 'org.chromium.net.DnsOptions'
2021
- 'org.chromium.net.CallbackException'
2122
- 'org.chromium.net.CronetException'
2223
- 'org.chromium.net.NetworkException'

‎pkgs/cronet_http/lib/src/cronet_client.dart‎

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,30 @@ class CronetEngine {
246246
/// of (host, port, alternativePort) that indicates that the host supports
247247
/// QUIC. Note that [CacheMode.disk] or [CacheMode.diskNoHttp] is needed to
248248
/// take advantage of 0-RTT connection establishment between sessions.
249+
///
250+
/// [useBuiltInDnsResolver] controls whether the engine uses Cronet's
251+
/// built-in DNS resolver instead of the system resolver. The built-in
252+
/// resolver is only used when QUIC is enabled, which it is by default.
253+
/// Setting this to `false` forces the system resolver, which can work
254+
/// around host resolution failures (`ERROR_HOSTNAME_NOT_RESOLVED`) seen
255+
/// with QUIC on some cellular networks and in background isolates.
256+
///
257+
/// [enableStaleDns] controls whether the engine may use expired entries
258+
/// from its host cache while a new resolution is in flight. Like
259+
/// [useBuiltInDnsResolver], this only takes effect when QUIC is enabled.
260+
///
261+
/// [persistHostCache] controls whether the engine's host cache is
262+
/// persisted to disk so that a freshly created engine (for example in a
263+
/// newly spawned isolate) can resolve recently used hosts without a live
264+
/// DNS query. Requires [storagePath] to be set.
265+
///
266+
/// [persistHostCachePeriod] sets how often the host cache is written to
267+
/// disk when [persistHostCache] is `true`.
268+
///
269+
/// See [DnsOptions](https://developer.android.com/develop/connectivity/cronet/reference/org/chromium/net/DnsOptions)
270+
/// for details on the DNS configuration options. On devices whose Cronet
271+
/// implementation does not support DNS options natively, they are applied
272+
/// through Cronet's experimental options fallback.
249273
static CronetEngine build(
250274
{CacheMode? cacheMode,
251275
int? cacheMaxSize,
@@ -255,7 +279,11 @@ class CronetEngine {
255279
bool? enableQuic,
256280
String? storagePath,
257281
String? userAgent,
258-
List<(String, int, int)>? quicHints}) {
282+
List<(String, int, int)>? quicHints,
283+
bool? useBuiltInDnsResolver,
284+
bool? enableStaleDns,
285+
bool? persistHostCache,
286+
Duration? persistHostCachePeriod}) {
259287
try {
260288
return using((arena) {
261289
final builder = jb.CronetEngine$Builder(
@@ -308,6 +336,36 @@ class CronetEngine {
308336
}
309337
}
310338

339+
if (useBuiltInDnsResolver != null ||
340+
enableStaleDns != null ||
341+
persistHostCache != null ||
342+
persistHostCachePeriod != null) {
343+
if (jb.DnsOptions.builder() case final dnsOptionsBuilder?) {
344+
dnsOptionsBuilder.releasedBy(arena);
345+
if (useBuiltInDnsResolver != null) {
346+
dnsOptionsBuilder
347+
.useBuiltInDnsResolver(useBuiltInDnsResolver)
348+
?.release();
349+
}
350+
if (enableStaleDns != null) {
351+
dnsOptionsBuilder.enableStaleDns(enableStaleDns)?.release();
352+
}
353+
if (persistHostCache != null) {
354+
dnsOptionsBuilder.persistHostCache(persistHostCache)?.release();
355+
}
356+
if (persistHostCachePeriod != null) {
357+
dnsOptionsBuilder
358+
.setPersistHostCachePeriodMillis(
359+
persistHostCachePeriod.inMilliseconds)
360+
?.release();
361+
}
362+
if (dnsOptionsBuilder.build() case final dnsOptions?) {
363+
dnsOptions.releasedBy(arena);
364+
builder.setDnsOptions(dnsOptions)?.release();
365+
}
366+
}
367+
}
368+
311369
return CronetEngine._(builder.build()!);
312370
});
313371
} on JThrowable catch (e) {

0 commit comments

Comments
 (0)