Browse Source

Store data directly on the binding (#225)

pull/229/head
David Fowler 7 years ago
committed by GitHub
parent
commit
79aad928ca
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      src/Microsoft.Tye.Hosting/DockerRunner.cs
  2. 4
      src/Microsoft.Tye.Hosting/HttpProxyService.cs
  3. 2
      src/Microsoft.Tye.Hosting/Model/Service.cs
  4. 3
      src/Microsoft.Tye.Hosting/Model/ServiceBinding.cs
  5. 10
      src/Microsoft.Tye.Hosting/PortAssigner.cs
  6. 6
      src/Microsoft.Tye.Hosting/ProcessRunner.cs
  7. 2
      src/Microsoft.Tye.Hosting/ProxyService.cs

10
src/Microsoft.Tye.Hosting/DockerRunner.cs

@ -84,7 +84,7 @@ namespace Microsoft.Tye.Hosting
var dockerInfo = new DockerInformation(new Task[service.Description.Replicas]); var dockerInfo = new DockerInformation(new Task[service.Description.Replicas]);
async Task RunDockerContainer(IEnumerable<(int Port, int? ContainerPort, int BindingPort, string? Protocol)> ports) async Task RunDockerContainer(IEnumerable<(int ExternalPort, int Port, int? ContainerPort, string? Protocol)> ports)
{ {
var hasPorts = ports.Any(); var hasPorts = ports.Any();
@ -122,7 +122,7 @@ namespace Microsoft.Tye.Hosting
if (string.Equals(p.Protocol, "https", StringComparison.OrdinalIgnoreCase)) if (string.Equals(p.Protocol, "https", StringComparison.OrdinalIgnoreCase))
{ {
// We need to set the redirect URL to the exposed port so the redirect works cleanly // We need to set the redirect URL to the exposed port so the redirect works cleanly
environment["HTTPS_PORT"] = p.BindingPort.ToString(); environment["HTTPS_PORT"] = p.ExternalPort.ToString();
} }
} }
@ -247,7 +247,7 @@ namespace Microsoft.Tye.Hosting
// port // port
for (var i = 0; i < serviceDescription.Replicas; i++) for (var i = 0; i < serviceDescription.Replicas; i++)
{ {
var ports = new List<(int, int?, int, string?)>(); var ports = new List<(int, int, int?, string?)>();
foreach (var binding in serviceDescription.Bindings) foreach (var binding in serviceDescription.Bindings)
{ {
if (binding.Port == null) if (binding.Port == null)
@ -255,7 +255,7 @@ namespace Microsoft.Tye.Hosting
continue; continue;
} }
ports.Add((service.PortMap[binding.Port.Value][i], binding.ContainerPort, binding.Port.Value, binding.Protocol)); ports.Add((binding.Port.Value, binding.ReplicaPorts[i], binding.ContainerPort, binding.Protocol));
} }
dockerInfo.Tasks[i] = RunDockerContainer(ports); dockerInfo.Tasks[i] = RunDockerContainer(ports);
@ -265,7 +265,7 @@ namespace Microsoft.Tye.Hosting
{ {
for (var i = 0; i < service.Description.Replicas; i++) for (var i = 0; i < service.Description.Replicas; i++)
{ {
dockerInfo.Tasks[i] = RunDockerContainer(Enumerable.Empty<(int, int?, int, string?)>()); dockerInfo.Tasks[i] = RunDockerContainer(Enumerable.Empty<(int, int, int?, string?)>());
} }
} }

4
src/Microsoft.Tye.Hosting/HttpProxyService.cs

@ -71,7 +71,7 @@ namespace Microsoft.Tye.Hosting
continue; continue;
} }
var port = service.PortMap[binding.Port.Value][i]; var port = binding.ReplicaPorts[i];
ports.Add(port); ports.Add(port);
var url = $"{binding.Protocol}://localhost:{port}"; var url = $"{binding.Protocol}://localhost:{port}";
addresses.Add(url); addresses.Add(url);
@ -115,7 +115,7 @@ namespace Microsoft.Tye.Hosting
// based on the replica port // based on the replica port
for (int i = 0; i < targetServiceDescription.Replicas; i++) for (int i = 0; i < targetServiceDescription.Replicas; i++)
{ {
var port = target.PortMap[targetBinding.Port!.Value][i]; var port = targetBinding.ReplicaPorts[i];
var url = $"{targetBinding.Protocol}://localhost:{port}"; var url = $"{targetBinding.Protocol}://localhost:{port}";
uris.Add(new Uri(url)); uris.Add(new Uri(url));
} }

2
src/Microsoft.Tye.Hosting/Model/Service.cs

@ -62,8 +62,6 @@ namespace Microsoft.Tye.Hosting.Model
public ConcurrentDictionary<string, ReplicaStatus> Replicas { get; set; } = new ConcurrentDictionary<string, ReplicaStatus>(); public ConcurrentDictionary<string, ReplicaStatus> Replicas { get; set; } = new ConcurrentDictionary<string, ReplicaStatus>();
public Dictionary<int, List<int>> PortMap { get; set; } = new Dictionary<int, List<int>>();
public Dictionary<object, object> Items { get; } = new Dictionary<object, object>(); public Dictionary<object, object> Items { get; } = new Dictionary<object, object>();
public Queue<string> CachedLogs { get; } = new Queue<string>(); public Queue<string> CachedLogs { get; } = new Queue<string>();

3
src/Microsoft.Tye.Hosting/Model/ServiceBinding.cs

@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license. // The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information. // See the LICENSE file in the project root for more information.
using System.Collections.Generic;
namespace Microsoft.Tye.Hosting.Model namespace Microsoft.Tye.Hosting.Model
{ {
public class ServiceBinding public class ServiceBinding
@ -13,5 +15,6 @@ namespace Microsoft.Tye.Hosting.Model
public int? ContainerPort { get; set; } public int? ContainerPort { get; set; }
public string? Host { get; set; } public string? Host { get; set; }
public string? Protocol { get; set; } public string? Protocol { get; set; }
public List<int> ReplicaPorts { get; } = new List<int>();
} }
} }

10
src/Microsoft.Tye.Hosting/PortAssigner.cs

@ -58,27 +58,23 @@ namespace Microsoft.Tye.Hosting
if (service.Description.Replicas == 1) if (service.Description.Replicas == 1)
{ {
// No need to proxy, the port maps to itself // No need to proxy, the port maps to itself
service.PortMap[binding.Port.Value] = new List<int> { binding.Port.Value }; binding.ReplicaPorts.Add(binding.Port.Value);
continue; continue;
} }
var ports = new List<int>();
for (var i = 0; i < service.Description.Replicas; i++) for (var i = 0; i < service.Description.Replicas; i++)
{ {
// Reserve a port for each replica // Reserve a port for each replica
var port = GetNextPort(); var port = GetNextPort();
ports.Add(port); binding.ReplicaPorts.Add(port);
} }
_logger.LogInformation( _logger.LogInformation(
"Mapping external port {ExternalPort} to internal port(s) {InternalPorts} for {ServiceName} binding {BindingName}", "Mapping external port {ExternalPort} to internal port(s) {InternalPorts} for {ServiceName} binding {BindingName}",
binding.Port, binding.Port,
string.Join(", ", ports.Select(p => p.ToString())), string.Join(", ", binding.ReplicaPorts.Select(p => p.ToString())),
service.Description.Name, service.Description.Name,
binding.Name ?? binding.Protocol); binding.Name ?? binding.Protocol);
service.PortMap[binding.Port.Value] = ports;
} }
var httpBinding = service.Description.Bindings.FirstOrDefault(b => b.Protocol == "http"); var httpBinding = service.Description.Bindings.FirstOrDefault(b => b.Protocol == "http");

6
src/Microsoft.Tye.Hosting/ProcessRunner.cs

@ -110,7 +110,7 @@ namespace Microsoft.Tye.Hosting
} }
} }
async Task RunApplicationAsync(IEnumerable<(int Port, int BindingPort, string? Protocol)> ports) async Task RunApplicationAsync(IEnumerable<(int ExternalPort, int Port, string? Protocol)> ports)
{ {
// Make sure we yield before trying to start the process, this is important so we don't hang startup // Make sure we yield before trying to start the process, this is important so we don't hang startup
await Task.Yield(); await Task.Yield();
@ -153,7 +153,7 @@ namespace Microsoft.Tye.Hosting
if (string.Equals(p.Protocol, "https", StringComparison.OrdinalIgnoreCase)) if (string.Equals(p.Protocol, "https", StringComparison.OrdinalIgnoreCase))
{ {
// We need to set the redirect URL to the exposed port so the redirect works cleanly // We need to set the redirect URL to the exposed port so the redirect works cleanly
environment["HTTPS_PORT"] = p.BindingPort.ToString(); environment["HTTPS_PORT"] = p.ExternalPort.ToString();
} }
} }
@ -258,7 +258,7 @@ namespace Microsoft.Tye.Hosting
continue; continue;
} }
ports.Add((service.PortMap[binding.Port.Value][i], binding.Port.Value, binding.Protocol)); ports.Add((binding.Port.Value, binding.ReplicaPorts[i], binding.Protocol));
} }
processInfo.Tasks[i] = RunApplicationAsync(ports); processInfo.Tasks[i] = RunApplicationAsync(ports);

2
src/Microsoft.Tye.Hosting/ProxyService.cs

@ -57,7 +57,7 @@ namespace Microsoft.Tye.Hosting
continue; continue;
} }
var ports = service.PortMap[binding.Port.Value]; var ports = binding.ReplicaPorts;
sockets.Listen(IPAddress.Loopback, binding.Port.Value, o => sockets.Listen(IPAddress.Loopback, binding.Port.Value, o =>
{ {

Loading…
Cancel
Save