Skip to content

Commit 78f7dcc

Browse files
Tadas MazutisSteveSandersonMS
Tadas Mazutis
authored andcommitted
Performance fix
- Usage of resolved loopback IP address instead of 'localhost' Addresses #1588
1 parent 7f550fb commit 78f7dcc

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

src/Microsoft.AspNetCore.NodeServices/Content/Node/entrypoint-http.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@
121121
var parsedArgs = ArgsUtil_1.parseArgs(process.argv);
122122
var requestedPortOrZero = parsedArgs.port || 0; // 0 means 'let the OS decide'
123123
server.listen(requestedPortOrZero, 'localhost', function () {
124-
// Signal to HttpNodeHost which port it should make its HTTP connections on
125-
console.log('[Microsoft.AspNetCore.NodeServices.HttpNodeHost:Listening on port ' + server.address().port + '\]');
124+
// Signal to HttpNodeHost which loopback IP address (IPv4 or IPv6) and port it should make its HTTP connections on
125+
console.log('[Microsoft.AspNetCore.NodeServices.HttpNodeHost:Listening on {' + server.address().address + '} port ' + server.address().port + '\]');
126126
// Signal to the NodeServices base class that we're ready to accept invocations
127127
console.log('[Microsoft.AspNetCore.NodeServices:Listening]');
128128
});

src/Microsoft.AspNetCore.NodeServices/HostingModels/HttpNodeInstance.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ namespace Microsoft.AspNetCore.NodeServices.HostingModels
2121
/// <seealso cref="Microsoft.AspNetCore.NodeServices.HostingModels.OutOfProcessNodeInstance" />
2222
internal class HttpNodeInstance : OutOfProcessNodeInstance
2323
{
24-
private static readonly Regex PortMessageRegex =
25-
new Regex(@"^\[Microsoft.AspNetCore.NodeServices.HttpNodeHost:Listening on port (\d+)\]$");
24+
private static readonly Regex EndpointMessageRegex =
25+
new Regex(@"^\[Microsoft.AspNetCore.NodeServices.HttpNodeHost:Listening on {(.*?)} port (\d+)\]$");
2626

2727
private static readonly JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings
2828
{
@@ -32,7 +32,7 @@ internal class HttpNodeInstance : OutOfProcessNodeInstance
3232

3333
private readonly HttpClient _client;
3434
private bool _disposed;
35-
private int _portNumber;
35+
private string _endpoint;
3636

3737
public HttpNodeInstance(NodeServicesOptions options, int port = 0)
3838
: base(
@@ -63,7 +63,7 @@ protected override async Task<T> InvokeExportAsync<T>(
6363
{
6464
var payloadJson = JsonConvert.SerializeObject(invocationInfo, jsonSerializerSettings);
6565
var payload = new StringContent(payloadJson, Encoding.UTF8, "application/json");
66-
var response = await _client.PostAsync("http://localhost:" + _portNumber, payload, cancellationToken);
66+
var response = await _client.PostAsync(_endpoint, payload, cancellationToken);
6767

6868
if (!response.IsSuccessStatusCode)
6969
{
@@ -111,13 +111,19 @@ protected override async Task<T> InvokeExportAsync<T>(
111111

112112
protected override void OnOutputDataReceived(string outputData)
113113
{
114-
// Watch for "port selected" messages, and when observed, store the port number
114+
// Watch for "port selected" messages, and when observed,
115+
// store the IP (IPv4/IPv6) and port number
115116
// so we can use it when making HTTP requests. The child process will always send
116117
// one of these messages before it sends a "ready for connections" message.
117-
var match = _portNumber != 0 ? null : PortMessageRegex.Match(outputData);
118+
var match = string.IsNullOrEmpty(_endpoint) ? EndpointMessageRegex.Match(outputData) : null;
118119
if (match != null && match.Success)
119120
{
120-
_portNumber = int.Parse(match.Groups[1].Captures[0].Value);
121+
var port = int.Parse(match.Groups[2].Captures[0].Value);
122+
var resolvedIpAddress = match.Groups[1].Captures[0].Value;
123+
124+
//IPv6 must be wrapped with [] brackets
125+
resolvedIpAddress = resolvedIpAddress == "::1" ? $"[{resolvedIpAddress}]" : resolvedIpAddress;
126+
_endpoint = $"http://{resolvedIpAddress}:{port}";
121127
}
122128
else
123129
{

src/Microsoft.AspNetCore.NodeServices/TypeScript/HttpNodeInstanceEntryPoint.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ const server = http.createServer((req, res) => {
7070
const parsedArgs = parseArgs(process.argv);
7171
const requestedPortOrZero = parsedArgs.port || 0; // 0 means 'let the OS decide'
7272
server.listen(requestedPortOrZero, 'localhost', function () {
73-
// Signal to HttpNodeHost which port it should make its HTTP connections on
74-
console.log('[Microsoft.AspNetCore.NodeServices.HttpNodeHost:Listening on port ' + server.address().port + '\]');
73+
// Signal to HttpNodeHost which loopback IP address (IPv4 or IPv6) and port it should make its HTTP connections on
74+
console.log('[Microsoft.AspNetCore.NodeServices.HttpNodeHost:Listening on {' + server.address().address + '} port ' + server.address().port + '\]');
7575

7676
// Signal to the NodeServices base class that we're ready to accept invocations
7777
console.log('[Microsoft.AspNetCore.NodeServices:Listening]');

0 commit comments

Comments
 (0)