@@ -464,15 +464,22 @@ private static Task GetHostEntryOrAddressesCoreAsync(string hostName, bool justR
464
464
{
465
465
ValidateHostName ( hostName ) ;
466
466
467
+ Task ? t ;
467
468
if ( NameResolutionTelemetry . Log . IsEnabled ( ) )
468
469
{
469
- return justAddresses
470
- ? ( Task ) GetAddrInfoWithTelemetryAsync < IPAddress [ ] > ( hostName , justAddresses )
471
- : ( Task ) GetAddrInfoWithTelemetryAsync < IPHostEntry > ( hostName , justAddresses ) ;
470
+ t = justAddresses
471
+ ? ( Task ? ) GetAddrInfoWithTelemetryAsync < IPAddress [ ] > ( hostName , justAddresses )
472
+ : ( Task ? ) GetAddrInfoWithTelemetryAsync < IPHostEntry > ( hostName , justAddresses ) ;
472
473
}
473
474
else
474
475
{
475
- return NameResolutionPal . GetAddrInfoAsync ( hostName , justAddresses ) ;
476
+ t = NameResolutionPal . GetAddrInfoAsync ( hostName , justAddresses ) ;
477
+ }
478
+
479
+ // If async resolution started, return task to user. otherwise fall back to sync API on threadpool.
480
+ if ( t != null )
481
+ {
482
+ return t ;
476
483
}
477
484
}
478
485
@@ -481,20 +488,34 @@ private static Task GetHostEntryOrAddressesCoreAsync(string hostName, bool justR
481
488
RunAsync ( s => GetHostEntryCore ( ( string ) s ) , hostName ) ;
482
489
}
483
490
484
- private static async Task < T > GetAddrInfoWithTelemetryAsync < T > ( string hostName , bool justAddresses )
491
+ private static Task < T > ? GetAddrInfoWithTelemetryAsync < T > ( string hostName , bool justAddresses )
485
492
where T : class
486
493
{
487
- ValueStopwatch stopwatch = NameResolutionTelemetry . Log . BeforeResolution ( hostName ) ;
494
+ ValueStopwatch stopwatch = ValueStopwatch . StartNew ( ) ;
495
+ Task ? task = NameResolutionPal . GetAddrInfoAsync ( hostName , justAddresses ) ;
488
496
489
- T ? result = null ;
490
- try
497
+ if ( task != null )
491
498
{
492
- result = await ( ( Task < T > ) NameResolutionPal . GetAddrInfoAsync ( hostName , justAddresses ) ) . ConfigureAwait ( false ) ;
493
- return result ;
499
+ return CompleteAsync ( task , hostName , stopwatch ) ;
494
500
}
495
- finally
501
+
502
+ // If resolution even did not start don't bother with telemetry.
503
+ // We will retry on thread-pool.
504
+ return null ;
505
+
506
+ static async Task < T > CompleteAsync ( Task task , string hostName , ValueStopwatch stopwatch )
496
507
{
497
- NameResolutionTelemetry . Log . AfterResolution ( stopwatch , successful : result is not null ) ;
508
+ _ = NameResolutionTelemetry . Log . BeforeResolution ( hostName ) ;
509
+ T ? result = null ;
510
+ try
511
+ {
512
+ result = await ( ( Task < T > ) task ) . ConfigureAwait ( false ) ;
513
+ return result ;
514
+ }
515
+ finally
516
+ {
517
+ NameResolutionTelemetry . Log . AfterResolution ( stopwatch , successful : result is not null ) ;
518
+ }
498
519
}
499
520
}
500
521
0 commit comments