From 6b8c67888731f7dd210acdb2b1160cdbdbe30d47 Mon Sep 17 00:00:00 2001 From: vnugent Date: Fri, 28 Jun 2024 15:48:22 -0400 Subject: refactor: Update service stack to reflect new loading patterns --- lib/Net.Http/src/Core/HttpServerBase.cs | 35 ++++++++++++++------------- lib/Net.Http/src/Core/HttpServerProcessing.cs | 20 +++++++-------- lib/Net.Http/src/HttpConfig.cs | 12 +++------ 3 files changed, 31 insertions(+), 36 deletions(-) (limited to 'lib/Net.Http/src') diff --git a/lib/Net.Http/src/Core/HttpServerBase.cs b/lib/Net.Http/src/Core/HttpServerBase.cs index 1ccace9..0eb5cef 100644 --- a/lib/Net.Http/src/Core/HttpServerBase.cs +++ b/lib/Net.Http/src/Core/HttpServerBase.cs @@ -115,7 +115,7 @@ namespace VNLib.Net.Http /// /// Gets a value indicating whether the server is listening for connections /// - public bool Running { get; private set; } + public bool Running => Transports.Any(static t => t.Running); /// /// Cached supported compression methods @@ -284,22 +284,11 @@ namespace VNLib.Net.Http //Listen to connections on all transports async IEnumerable runTasks = Transports.Select(ListenAsync); - //Set running flag and will be reset when all listening tasks are done - Running = true; - //Calling WhenAll() will force the numeration and schedule listening tasks - return Task.WhenAll(runTasks) - .ContinueWith( - OnAllStopped, - CancellationToken.None, - TaskContinuationOptions.RunContinuationsAsynchronously, - TaskScheduler.Default - ); + return Task.WhenAll(runTasks); //Defer listening tasks to the task scheduler to avoid blocking this thread Task ListenAsync(ListenerState tp) => Task.Run(() => ListenWorkerDoWork(tp), cancellationToken); - - void OnAllStopped(Task _) => Running = false; } /* @@ -308,11 +297,22 @@ namespace VNLib.Net.Http private async Task ListenWorkerDoWork(ListenerState state) { state.Running = true; - - _config.ServerLog.Information("HTTP server {hc} listening for connections", GetHashCode()); + + if (_config.ServerLog.IsEnabled(LogLevel.Verbose)) + { + _config.ServerLog.Verbose( + format: "HTTP server {hc} listening for connections on {iface}", + GetHashCode(), + state.OriginServer + ); + } + else + { + _config.ServerLog.Information("HTTP server {hc} listening for connections", GetHashCode()); + } //Listen for connections until canceled - while (true) + do { try { @@ -331,7 +331,8 @@ namespace VNLib.Net.Http { _config.ServerLog.Error(ex); } - } + + } while (true); //Clear all caches before leaving to aid gc CacheHardClear(); diff --git a/lib/Net.Http/src/Core/HttpServerProcessing.cs b/lib/Net.Http/src/Core/HttpServerProcessing.cs index f5dbbc7..8594ea0 100644 --- a/lib/Net.Http/src/Core/HttpServerProcessing.cs +++ b/lib/Net.Http/src/Core/HttpServerProcessing.cs @@ -29,7 +29,6 @@ using System.Threading; using System.Net.Sockets; using System.Diagnostics; using System.Threading.Tasks; -using System.Collections.Generic; using System.Runtime.CompilerServices; using VNLib.Utils.Memory; @@ -386,16 +385,17 @@ namespace VNLib.Net.Http private async Task ProcessRequestAsync(ListenerState listenState, HttpContext context) { //Get the server root for the specified location or fallback to a wildcard host if one is selected - IWebRoot? root = listenState.Roots.GetValueOrDefault( - context.Request.State.Location.DnsSafeHost, - listenState.DefaultRoute - ); - - if (root == null) + + if (!listenState.Roots.TryGetValue(context.Request.State.Location.DnsSafeHost, out IWebRoot? root)) { - context.Respond(HttpStatusCode.NotFound); - //make sure control leaves - return true; + if (listenState.DefaultRoute is null) + { + context.Respond(HttpStatusCode.NotFound); + //make sure control leaves + return true; + } + + root = listenState.DefaultRoute; } //Check the expect header and return an early status code diff --git a/lib/Net.Http/src/HttpConfig.cs b/lib/Net.Http/src/HttpConfig.cs index aa6e34a..40e9f88 100644 --- a/lib/Net.Http/src/HttpConfig.cs +++ b/lib/Net.Http/src/HttpConfig.cs @@ -53,17 +53,11 @@ namespace VNLib.Net.Http /// /// Initializes a new instance of the struct /// - /// - /// /// - public HttpConfig(ILogProvider serverLog, IHttpMemoryPool memoryPool, Encoding httpEncoding) + public HttpConfig(Encoding httpEncoding) { - ArgumentNullException.ThrowIfNull(serverLog); - ArgumentNullException.ThrowIfNull(memoryPool); ArgumentNullException.ThrowIfNull(httpEncoding); - ServerLog = serverLog; - MemoryPool = memoryPool; HttpEncoding = httpEncoding; //Init pre-encded segments @@ -77,12 +71,12 @@ namespace VNLib.Net.Http /// /// A log provider that all server related log entiries will be written to /// - public readonly ILogProvider ServerLog { get; init; } + public required readonly ILogProvider ServerLog { get; init; } /// /// Server memory pool to use for allocating buffers /// - public readonly IHttpMemoryPool MemoryPool { get; init; } + public required readonly IHttpMemoryPool MemoryPool { get; init; } /// /// The absolute request entity body size limit in bytes -- cgit