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/Net.Http | |
parent | 12391e9a207b60b41a074600fc2373ad3eb1c3ab (diff) |
refactor: Update service stack to reflect new loading patterns
Diffstat (limited to 'lib/Net.Http')
-rw-r--r-- | lib/Net.Http/src/Core/HttpServerBase.cs | 35 | ||||
-rw-r--r-- | lib/Net.Http/src/Core/HttpServerProcessing.cs | 20 | ||||
-rw-r--r-- | lib/Net.Http/src/HttpConfig.cs | 12 |
3 files changed, 31 insertions, 36 deletions
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 /// <summary> /// Gets a value indicating whether the server is listening for connections /// </summary> - public bool Running { get; private set; } + public bool Running => Transports.Any(static t => t.Running); /// <summary> /// Cached supported compression methods @@ -284,22 +284,11 @@ namespace VNLib.Net.Http //Listen to connections on all transports async IEnumerable<Task> 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<bool> 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 /// <summary> /// Initializes a new instance of the <see cref="HttpConfig"/> struct /// </summary> - /// <param name="serverLog"></param> - /// <param name="memoryPool"></param> /// <param name="httpEncoding"></param> - 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 /// <summary> /// A log provider that all server related log entiries will be written to /// </summary> - public readonly ILogProvider ServerLog { get; init; } + public required readonly ILogProvider ServerLog { get; init; } /// <summary> /// Server memory pool to use for allocating buffers /// </summary> - public readonly IHttpMemoryPool MemoryPool { get; init; } + public required readonly IHttpMemoryPool MemoryPool { get; init; } /// <summary> /// The absolute request entity body size limit in bytes |