Files
git.stella-ops.org/src/__Libraries/StellaOps.Microservice/StellaMicroserviceOptions.cs
StellaOps Bot 175b750e29 Implement InMemory Transport Layer for StellaOps Router
- Added InMemoryTransportOptions class for configuration settings including timeouts and latency.
- Developed InMemoryTransportServer class to handle connections, frame processing, and event management.
- Created ServiceCollectionExtensions for easy registration of InMemory transport services.
- Established project structure and dependencies for InMemory transport library.
- Implemented comprehensive unit tests for endpoint discovery, connection management, request/response flow, and streaming capabilities.
- Ensured proper handling of cancellation, heartbeat, and hello frames within the transport layer.
2025-12-05 01:00:10 +02:00

93 lines
3.2 KiB
C#

using System.Text.RegularExpressions;
namespace StellaOps.Microservice;
/// <summary>
/// Options for configuring a Stella microservice.
/// </summary>
public sealed partial class StellaMicroserviceOptions
{
/// <summary>
/// Gets or sets the service name.
/// </summary>
public required string ServiceName { get; set; }
/// <summary>
/// Gets or sets the semantic version.
/// Must be valid semver (e.g., "1.0.0", "2.1.0-beta.1").
/// </summary>
public required string Version { get; set; }
/// <summary>
/// Gets or sets the region where this instance is deployed.
/// </summary>
public required string Region { get; set; }
/// <summary>
/// Gets or sets the unique instance identifier.
/// Auto-generated if not provided.
/// </summary>
public string InstanceId { get; set; } = Guid.NewGuid().ToString("N");
/// <summary>
/// Gets the router endpoints to connect to.
/// At least one router endpoint is required.
/// </summary>
public List<RouterEndpointConfig> Routers { get; set; } = [];
/// <summary>
/// Gets or sets the optional path to a YAML config file for endpoint overrides.
/// </summary>
public string? ConfigFilePath { get; set; }
/// <summary>
/// Gets or sets the heartbeat interval.
/// Default: 10 seconds.
/// </summary>
public TimeSpan HeartbeatInterval { get; set; } = TimeSpan.FromSeconds(10);
/// <summary>
/// Gets or sets the maximum reconnect backoff.
/// Default: 1 minute.
/// </summary>
public TimeSpan ReconnectBackoffMax { get; set; } = TimeSpan.FromMinutes(1);
/// <summary>
/// Gets or sets the initial reconnect delay.
/// Default: 1 second.
/// </summary>
public TimeSpan ReconnectBackoffInitial { get; set; } = TimeSpan.FromSeconds(1);
/// <summary>
/// Validates the options and throws if invalid.
/// </summary>
public void Validate()
{
if (string.IsNullOrWhiteSpace(ServiceName))
throw new InvalidOperationException("ServiceName is required.");
if (string.IsNullOrWhiteSpace(Version))
throw new InvalidOperationException("Version is required.");
if (!SemverRegex().IsMatch(Version))
throw new InvalidOperationException($"Version '{Version}' is not valid semver.");
if (string.IsNullOrWhiteSpace(Region))
throw new InvalidOperationException("Region is required.");
if (Routers.Count == 0)
throw new InvalidOperationException("At least one router endpoint is required.");
foreach (var router in Routers)
{
if (string.IsNullOrWhiteSpace(router.Host))
throw new InvalidOperationException("Router host is required.");
if (router.Port <= 0 || router.Port > 65535)
throw new InvalidOperationException($"Router port {router.Port} is invalid.");
}
}
[GeneratedRegex(@"^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$")]
private static partial Regex SemverRegex();
}