Skip to content
This repository has been archived by the owner on Apr 8, 2020. It is now read-only.

Commit

Permalink
Performance fix
Browse files Browse the repository at this point in the history
- Usage of resolved loopback IP address instead of 'localhost'

Addresses #1588
  • Loading branch information
Tadas Mazutis authored and SteveSandersonMS committed Apr 16, 2018
1 parent 7f550fb commit 78f7dcc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@
var parsedArgs = ArgsUtil_1.parseArgs(process.argv);
var requestedPortOrZero = parsedArgs.port || 0; // 0 means 'let the OS decide'
server.listen(requestedPortOrZero, 'localhost', function () {
// Signal to HttpNodeHost which port it should make its HTTP connections on
console.log('[Microsoft.AspNetCore.NodeServices.HttpNodeHost:Listening on port ' + server.address().port + '\]');
// Signal to HttpNodeHost which loopback IP address (IPv4 or IPv6) and port it should make its HTTP connections on
console.log('[Microsoft.AspNetCore.NodeServices.HttpNodeHost:Listening on {' + server.address().address + '} port ' + server.address().port + '\]');
// Signal to the NodeServices base class that we're ready to accept invocations
console.log('[Microsoft.AspNetCore.NodeServices:Listening]');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ namespace Microsoft.AspNetCore.NodeServices.HostingModels
/// <seealso cref="Microsoft.AspNetCore.NodeServices.HostingModels.OutOfProcessNodeInstance" />
internal class HttpNodeInstance : OutOfProcessNodeInstance
{
private static readonly Regex PortMessageRegex =
new Regex(@"^\[Microsoft.AspNetCore.NodeServices.HttpNodeHost:Listening on port (\d+)\]$");
private static readonly Regex EndpointMessageRegex =
new Regex(@"^\[Microsoft.AspNetCore.NodeServices.HttpNodeHost:Listening on {(.*?)} port (\d+)\]$");

private static readonly JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings
{
Expand All @@ -32,7 +32,7 @@ internal class HttpNodeInstance : OutOfProcessNodeInstance

private readonly HttpClient _client;
private bool _disposed;
private int _portNumber;
private string _endpoint;

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

if (!response.IsSuccessStatusCode)
{
Expand Down Expand Up @@ -111,13 +111,19 @@ protected override async Task<T> InvokeExportAsync<T>(

protected override void OnOutputDataReceived(string outputData)
{
// Watch for "port selected" messages, and when observed, store the port number
// Watch for "port selected" messages, and when observed,
// store the IP (IPv4/IPv6) and port number
// so we can use it when making HTTP requests. The child process will always send
// one of these messages before it sends a "ready for connections" message.
var match = _portNumber != 0 ? null : PortMessageRegex.Match(outputData);
var match = string.IsNullOrEmpty(_endpoint) ? EndpointMessageRegex.Match(outputData) : null;
if (match != null && match.Success)
{
_portNumber = int.Parse(match.Groups[1].Captures[0].Value);
var port = int.Parse(match.Groups[2].Captures[0].Value);
var resolvedIpAddress = match.Groups[1].Captures[0].Value;

//IPv6 must be wrapped with [] brackets
resolvedIpAddress = resolvedIpAddress == "::1" ? $"[{resolvedIpAddress}]" : resolvedIpAddress;
_endpoint = $"http://{resolvedIpAddress}:{port}";
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ const server = http.createServer((req, res) => {
const parsedArgs = parseArgs(process.argv);
const requestedPortOrZero = parsedArgs.port || 0; // 0 means 'let the OS decide'
server.listen(requestedPortOrZero, 'localhost', function () {
// Signal to HttpNodeHost which port it should make its HTTP connections on
console.log('[Microsoft.AspNetCore.NodeServices.HttpNodeHost:Listening on port ' + server.address().port + '\]');
// Signal to HttpNodeHost which loopback IP address (IPv4 or IPv6) and port it should make its HTTP connections on
console.log('[Microsoft.AspNetCore.NodeServices.HttpNodeHost:Listening on {' + server.address().address + '} port ' + server.address().port + '\]');

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

0 comments on commit 78f7dcc

Please sign in to comment.