Skip to content

Commit 78f7dcc

Browse files
Tadas MazutisSteveSandersonMS
authored andcommitted
Performance fix
- Usage of resolved loopback IP address instead of 'localhost' Addresses aspnet#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
varparsedArgs=ArgsUtil_1.parseArgs(process.argv);
122122
varrequestedPortOrZero=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
internalclassHttpNodeInstance:OutOfProcessNodeInstance
2323
{
24-
privatestaticreadonlyRegexPortMessageRegex=
25-
newRegex(@"^\[Microsoft.AspNetCore.NodeServices.HttpNodeHost:Listening on port (\d+)\]$");
24+
privatestaticreadonlyRegexEndpointMessageRegex=
25+
newRegex(@"^\[Microsoft.AspNetCore.NodeServices.HttpNodeHost:Listening on {(.*?)} port (\d+)\]$");
2626

2727
privatestaticreadonlyJsonSerializerSettingsjsonSerializerSettings=newJsonSerializerSettings
2828
{
@@ -32,7 +32,7 @@ internal class HttpNodeInstance : OutOfProcessNodeInstance
3232

3333
privatereadonlyHttpClient_client;
3434
privatebool_disposed;
35-
privateint_portNumber;
35+
privatestring_endpoint;
3636

3737
publicHttpNodeInstance(NodeServicesOptionsoptions,intport=0)
3838
:base(
@@ -63,7 +63,7 @@ protected override async Task<T> InvokeExportAsync<T>(
6363
{
6464
varpayloadJson=JsonConvert.SerializeObject(invocationInfo,jsonSerializerSettings);
6565
varpayload=newStringContent(payloadJson,Encoding.UTF8,"application/json");
66-
varresponse=await_client.PostAsync("http://localhost:"+_portNumber,payload,cancellationToken);
66+
varresponse=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
protectedoverridevoidOnOutputDataReceived(stringoutputData)
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-
varmatch=_portNumber!=0?null:PortMessageRegex.Match(outputData);
118+
varmatch=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+
varport=int.Parse(match.Groups[2].Captures[0].Value);
122+
varresolvedIpAddress=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
constparsedArgs=parseArgs(process.argv);
7171
constrequestedPortOrZero=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)