diff options
author | vnugent <public@vaughnnugent.com> | 2024-06-28 15:48:22 -0400 |
---|---|---|
committer | vnugent <public@vaughnnugent.com> | 2024-06-28 15:48:22 -0400 |
commit | 6b8c67888731f7dd210acdb2b1160cdbdbe30d47 (patch) | |
tree | d4eb2d4b2b522bb6936671f7648a29521446b72b /lib/Plugins.Essentials.ServiceStack/src/ServiceDomain.cs | |
parent | 12391e9a207b60b41a074600fc2373ad3eb1c3ab (diff) |
refactor: Update service stack to reflect new loading patterns
Diffstat (limited to 'lib/Plugins.Essentials.ServiceStack/src/ServiceDomain.cs')
-rw-r--r-- | lib/Plugins.Essentials.ServiceStack/src/ServiceDomain.cs | 54 |
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 = []; } } } |