aboutsummaryrefslogtreecommitdiff
path: root/lib/Plugins.Essentials.ServiceStack/src/ServiceDomain.cs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Plugins.Essentials.ServiceStack/src/ServiceDomain.cs')
-rw-r--r--lib/Plugins.Essentials.ServiceStack/src/ServiceDomain.cs54
1 files changed, 13 insertions, 41 deletions
diff --git a/lib/Plugins.Essentials.ServiceStack/src/ServiceDomain.cs b/lib/Plugins.Essentials.ServiceStack/src/ServiceDomain.cs
index 35bf65b..52e4984 100644
--- a/lib/Plugins.Essentials.ServiceStack/src/ServiceDomain.cs
+++ b/lib/Plugins.Essentials.ServiceStack/src/ServiceDomain.cs
@@ -23,11 +23,10 @@
*/
using System;
-using System.Net;
-using System.Linq;
using System.Collections.Generic;
using VNLib.Utils.Extensions;
+using VNLib.Plugins.Essentials.ServiceStack.Construction;
namespace VNLib.Plugins.Essentials.ServiceStack
{
@@ -38,7 +37,7 @@ namespace VNLib.Plugins.Essentials.ServiceStack
/// </summary>
public sealed class ServiceDomain
{
- private readonly LinkedList<ServiceGroup> _serviceGroups = new();
+ private ServiceGroup[] _serviceGroups = [];
/// <summary>
/// Gets all service groups loaded in the service manager
@@ -51,15 +50,11 @@ namespace VNLib.Plugins.Essentials.ServiceStack
/// </summary>
/// <param name="hostBuilder">The callback method to build virtual hosts</param>
/// <returns>A value that indicates if any virtual hosts were successfully loaded</returns>
- public bool BuildDomain(Action<ICollection<IServiceHost>> hostBuilder)
+ public void BuildDomain(ServiceBuilder hostBuilder)
{
- //LL to store created hosts
- LinkedList<IServiceHost> hosts = new();
+ ArgumentNullException.ThrowIfNull(hostBuilder);
- //build hosts
- hostBuilder.Invoke(hosts);
-
- return FromExisting(hosts);
+ FromExisting(hostBuilder.BuildGroups());
}
/// <summary>
@@ -67,36 +62,11 @@ namespace VNLib.Plugins.Essentials.ServiceStack
/// </summary>
/// <param name="hosts">The enumeration of virtual hosts</param>
/// <returns>A value that indicates if any virtual hosts were successfully loaded</returns>
- public bool FromExisting(IEnumerable<IServiceHost> hosts)
- {
- //Get service groups and pass service group list
- CreateServiceGroups(_serviceGroups, hosts);
- return _serviceGroups.Any();
- }
-
- private static void CreateServiceGroups(ICollection<ServiceGroup> groups, IEnumerable<IServiceHost> hosts)
+ public void FromExisting(IEnumerable<ServiceGroup> hosts)
{
- //Get distinct interfaces
- IPEndPoint[] interfaces = hosts.Select(static s => s.TransportInfo.TransportEndpoint).Distinct().ToArray();
-
- //Select hosts of the same interface to create a group from
- foreach (IPEndPoint iface in interfaces)
- {
- IEnumerable<IServiceHost> groupHosts = hosts.Where(host => host.TransportInfo.TransportEndpoint.Equals(iface));
+ ArgumentNullException.ThrowIfNull(hosts);
- //Find any duplicate hostnames for the same service gorup
- IServiceHost[] overlap = groupHosts.Where(vh => groupHosts.Select(static s => s.Processor.Hostname).Count(hostname => vh.Processor.Hostname == hostname) > 1).ToArray();
-
- if(overlap.Length > 0)
- {
- throw new ArgumentException($"The hostname '{overlap.Last().Processor.Hostname}' is already in use by another virtual host");
- }
-
- //init new service group around an interface and its roots
- ServiceGroup group = new(iface, groupHosts);
-
- groups.Add(group);
- }
+ hosts.ForEach(h => _serviceGroups = [.. _serviceGroups, h]);
}
/// <summary>
@@ -106,9 +76,11 @@ namespace VNLib.Plugins.Essentials.ServiceStack
public void TearDown()
{
//Manually cleanup if unload missed data
- _serviceGroups.TryForeach(static sg => sg.UnloadAll());
- //empty service groups
- _serviceGroups.Clear();
+ Array.ForEach(_serviceGroups, static sg => sg.UnloadAll());
+
+ Array.Clear(_serviceGroups);
+
+ _serviceGroups = [];
}
}
}