From 4d151a599e28fccde8909d33e5c7011a482f8c50 Mon Sep 17 00:00:00 2001 From: Steve Sanderson Date: Mon, 16 Apr 2018 14:54:52 +0100 Subject: [PATCH] Dynamically expand timeout when waiting for Angular CLI to be ready. Fixes #1611 --- .../AngularCli/AngularCliMiddleware.cs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.AspNetCore.SpaServices.Extensions/AngularCli/AngularCliMiddleware.cs b/src/Microsoft.AspNetCore.SpaServices.Extensions/AngularCli/AngularCliMiddleware.cs index 7f78dc9..9090f77 100644 --- a/src/Microsoft.AspNetCore.SpaServices.Extensions/AngularCli/AngularCliMiddleware.cs +++ b/src/Microsoft.AspNetCore.SpaServices.Extensions/AngularCli/AngularCliMiddleware.cs @@ -105,6 +105,7 @@ namespace Microsoft.AspNetCore.SpaServices.AngularCli // connection then it's not ready. We keep trying forever because this is dev-mode // only, and only a single startup attempt will be made, and there's a further level // of timeouts enforced on a per-request basis. + var timeoutMilliseconds = 1000; using (var client = new HttpClient()) { while (true) @@ -114,12 +115,23 @@ namespace Microsoft.AspNetCore.SpaServices.AngularCli // If we get any HTTP response, the CLI server is ready await client.SendAsync( new HttpRequestMessage(HttpMethod.Head, cliServerUri), - new CancellationTokenSource(1000).Token); + new CancellationTokenSource(timeoutMilliseconds).Token); return; } catch (Exception) { - await Task.Delay(1000); // 1 second + await Task.Delay(500); + + // Depending on the host's networking configuration, the requests can take a while + // to go through, most likely due to the time spent resolving 'localhost'. + // Each time we have a failure, allow a bit longer next time (up to a maximum). + // This only influences the time until we regard the dev server as 'ready', so it + // doesn't affect the runtime perf (even in dev mode) once the first connection is made. + // Resolves https://github.com/aspnet/JavaScriptServices/issues/1611 + if (timeoutMilliseconds < 10000) + { + timeoutMilliseconds += 3000; + } } } }