This commit is contained in:
2026-06-15 18:18:16 +08:00
parent 97c9fba14e
commit 2b9f134e5f
4164 changed files with 386922 additions and 79 deletions

View File

@@ -0,0 +1,134 @@
using System;
using System.Collections.Generic;
using Best.HTTP.Request.Settings;
using UnityEngine;
namespace Best.HTTP.HostSetting
{
/// <summary>
/// The <see cref="HostKey"/> struct represents a unique key for identifying hosts based on their <see cref="System.Uri"/> and <see cref="ProxySettings"/>.
/// </summary>
/// <remarks>
/// <para>
/// The <see cref="HostKey"/> struct is designed to uniquely identify a host based on its URI (Uniform Resource Identifier) and optional proxy settings.
/// It provides a way to create, compare, and hash host keys, enabling efficient host variant management in the <see cref="HostManager"/>.
/// </para>
/// <para>
/// Key features of the <see cref="HostKey"/> struct include:
/// </para>
/// <list type="bullet">
/// <item>
/// <term>Uniqueness</term>
/// <description>
/// Each <see cref="HostKey"/> is guaranteed to be unique for a specific host, considering both the URI and proxy settings.
/// </description>
/// </item>
/// <item>
/// <term>Hashing</term>
/// <description>
/// The struct provides a method to calculate a hash code for a <see cref="HostKey"/>, making it suitable for use as a dictionary key.
/// </description>
/// </item>
/// <item>
/// <term>Creation</term>
/// <description>
/// You can create a <see cref="HostKey"/> instance from a <see cref="System.Uri"/> and optional <see cref="ProxySettings"/>.
/// </description>
/// </item>
/// </list>
/// <para>
/// Usage of the <see cref="HostKey"/> struct is typically handled internally by the BestHTTP library to manage unique hosts and optimize resource usage.
/// Developers can use it when dealing with host-specific operations or customization of the library's behavior.
/// </para>
/// </remarks>
public readonly struct HostKey
{
/// <summary>
/// Gets the URI (Uniform Resource Identifier) associated with the host.
/// </summary>
public readonly Uri Uri;
/// <summary>
/// Gets the proxy settings associated with the host.
/// </summary>
public readonly ProxySettings Proxy;
/// <summary>
/// Gets the unique hash key for the host.
/// </summary>
public readonly Hash128 Key;
/// <summary>
/// Gets the host name from the URI or "file" if the URI is a file URI.
/// </summary>
public string Host { get => !this.Uri.IsFile ? this.Uri.Host : "file"; }
/// <summary>
/// Initializes a new instance of the <see cref="HostKey"/> struct with the specified URI and proxy settings.
/// </summary>
/// <param name="uri">The URI of the host.</param>
/// <param name="proxy">The proxy settings associated with the host, or <c>null</c> if no proxy is used.</param>
public HostKey(Uri uri, ProxySettings proxy)
{
this.Uri = uri;
this.Proxy = proxy;
this.Key = CalculateHash(uri, proxy);
}
public override bool Equals(object obj) => obj switch
{
HostKey hostKey => hostKey.Equals(this),
_ => false
};
public bool Equals(HostKey hostKey) => this.Key.Equals(hostKey.Key);
public override int GetHashCode() => this.Key.GetHashCode();
public override string ToString() => $"{{\"Uri\":\"{this.Uri.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped)}\", \"Proxy\": {this.Proxy.ToString()}, \"Key\": {this.Key.ToString()}}}";
private static Hash128 CalculateHash(Uri uri, ProxySettings proxy)
{
Hash128 hash = new Hash128();
Append(uri, ref hash);
proxy?.AddToHash(uri, ref hash);
return hash;
}
internal static void Append(Uri uri, ref Hash128 hash)
{
if (uri != null)
{
hash.Append(uri.Scheme);
hash.Append(uri.Host);
hash.Append(uri.Port);
}
}
/// <summary>
/// Creates a <see cref="HostKey"/> instance from an HTTP request.
/// </summary>
/// <param name="request">The HTTP request from which to extract the current URI and proxy settings.</param>
/// <returns>A <see cref="HostKey"/> representing the host of the HTTP request.</returns>
public static HostKey From(HTTPRequest request) => new HostKey(request.CurrentUri, request.ProxySettings);
/// <summary>
/// Creates a <see cref="HostKey"/> instance from a URI and proxy settings.
/// </summary>
/// <param name="uri">The URI of the host.</param>
/// <param name="proxy">The proxy settings associated with the host, or <c>null</c> if no proxy is used.</param>
/// <returns>A <see cref="HostKey"/> representing the host with the given URI and proxy settings.</returns>
public static HostKey From(Uri uri, ProxySettings proxy) => new HostKey(uri, proxy);
}
internal sealed class HostKeyEqualityComparer : IEqualityComparer<HostKey>
{
public bool Equals(HostKey x, HostKey y) => x.Equals(y);
public int GetHashCode(HostKey obj) => obj.GetHashCode();
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 19e27b6611d69a348b1cd90fe7fb4cfb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,150 @@
using System.Collections.Generic;
using Best.HTTP.Hosts.Connections;
using Best.HTTP.Shared;
namespace Best.HTTP.HostSetting
{
/*
┌───────────────┐
┌──────┤ HostManager ├──────────────────────┐
│ └───────────────┘ │
│ │
┌──────────▼───────┐ ┌──────────▼────────┐
│ HostVariant │ │ HostVariant │
│(http://host:port)│ │(https://host:port)│
┌────┴──────┬──────────┬┘ ┌────┴──────┬───────────┬┘
│ │ │ │ │ │
┌──────▼──────┐ ┌──▼──┐ ▼ ┌──────▼──────┐ ┌──▼──┐ ▼
│ Connections │ │Queue│ ProtocolSupport │ Connections │ │Queue│ ProtocolSupport
├─────────────┤ ├─────┤ (http/1.1) ├─────────────┤ ├─────┤ (http/1.1, h2)
│ ... │ │ ... │ │ ... │ │ ... │
│ ... │ │ ... │ │ ... │ │ ... │
│ ... │ │ ... │ │ ... │ │ ... │
│ ... │ │ ... │ │ ... │ │ ... │
└─────────────┘ └─────┘ └─────────────┘ └─────┘
*/
/// <summary>
/// The <see cref="HostManager"/> class provides centralized management for <see cref="HostVariant"/> objects associated with HTTP requests and connections.
/// </summary>
/// <remarks>
/// <para>
/// The <see cref="HostManager"/> class acts as a central registry for managing <see cref="HostVariant"/> objects, each associated with a unique <see cref="HostKey"/>.
/// It facilitates the creation, retrieval, and management of <see cref="HostVariant"/> instances based on HTTP requests and connections.
/// </para>
/// <para>
/// A <see cref="HostVariant"/> represents a specific host and port combination (e.g., "http://example.com:80" or "https://example.com:443") and
/// manages the connections and request queues for that host. The class ensures that a single <see cref="HostVariant"/> instance is used for
/// each unique host, helping optimize resource usage and connection pooling.
/// </para>
/// <para>
/// Key features of the <see cref="HostManager"/> class include:
/// </para>
/// <list type="bullet">
/// <item>
/// <term>Creation and Retrieval</term>
/// <description>
/// The class allows you to create and retrieve <see cref="HostVariant"/> instances based on HTTP requests, connections, or <see cref="HostKey"/>.
/// It ensures that a single <see cref="HostVariant"/> is used for each unique host.
/// </description>
/// </item>
/// <item>
/// <term>Queue Management</term>
/// <description>
/// The <see cref="HostManager"/> manages the queue of pending requests for each <see cref="HostVariant"/>, ensuring efficient request processing.
/// </description>
/// </item>
/// <item>
/// <term>Connection Management</term>
/// <description>
/// The class handles the management of connections associated with <see cref="HostVariant"/> objects, including recycling idle connections,
/// removing idle connections, and shutting down connections when needed.
/// </description>
/// </item>
/// </list>
/// <para>
/// Usage of the <see cref="HostManager"/> class is typically transparent to developers and is handled internally by the Best HTTP library. However,
/// it provides a convenient and efficient way to manage connections and requests when needed.
/// </para>
/// </remarks>
public static class HostManager
{
/// <summary>
/// Dictionary to store <see cref="HostKey"/>-<see cref="HostVariant"/> mappings.
/// </summary>
private static Dictionary<HostKey, HostVariant> hosts = new Dictionary<HostKey, HostVariant>(new HostKeyEqualityComparer());
/// <summary>
/// Gets the <see cref="HostVariant"/> associated with an HTTP request.
/// </summary>
/// <param name="request">The HTTP request.</param>
/// <returns>The <see cref="HostVariant"/> for the request's host.</returns>
public static HostVariant GetHostVariant(HTTPRequest request) => GetHostVariant(request.CurrentHostKey);
/// <summary>
/// Gets the <see cref="HostVariant"/> associated with a connection.
/// </summary>
/// <param name="connection">The HTTP connection.</param>
/// <returns>The <see cref="HostVariant"/> for the connection's host.</returns>
public static HostVariant GetHostVariant(ConnectionBase connection) => GetHostVariant(connection.HostKey);
/// <summary>
/// Gets the <see cref="HostVariant"/> associated with a HostKey.
/// </summary>
/// <param name="key">The HostKey for which to get the HostVariant.</param>
/// <returns>The <see cref="HostVariant"/> for the specified HostKey.</returns>
public static HostVariant GetHostVariant(HostKey key)
{
if (!hosts.TryGetValue(key, out var variant))
{
var settings = HTTPManager.PerHostSettings.Get(key).HostVariantSettings;
variant = settings?.VariantFactory?.Invoke(settings, key) ?? new HostVariant(key);
hosts.Add(key, variant);
HTTPManager.Logger.Information("HostManager", $"Variant added with key: {key}");
}
return variant;
}
/// <summary>
/// Removes all idle connections for all hosts.
/// </summary>
public static void RemoveAllIdleConnections()
{
HTTPManager.Logger.Information("HostManager", "RemoveAllIdleConnections");
foreach (var host in hosts)
host.Value.RemoveAllIdleConnections();
}
/// <summary>
/// Tries to send queued requests for all hosts.
/// </summary>
public static void TryToSendQueuedRequests()
{
foreach (var kvp in hosts)
kvp.Value.TryToSendQueuedRequests();
}
/// <summary>
/// Shuts down all connections for all hosts.
/// </summary>
public static void Shutdown()
{
HTTPManager.Logger.Information("HostManager", "Shutdown()");
foreach (var kvp in hosts)
kvp.Value.Shutdown();
}
/// <summary>
/// Clears all hosts and their associated variants.
/// </summary>
public static void Clear()
{
HTTPManager.Logger.Information("HostManager", "Clearing()");
hosts.Clear();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8717fe3a1943db149958dd51a8dc3a86
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,357 @@
using Best.HTTP.Hosts.Connections;
using Best.HTTP.Hosts.Connections.File;
using Best.HTTP.Hosts.Settings;
using Best.HTTP.Shared;
using Best.HTTP.Shared.Extensions;
using Best.HTTP.Shared.Logger;
using System;
using System.Collections.Generic;
using System.Globalization;
namespace Best.HTTP.HostSetting
{
/// <summary>
/// An enumeration representing the protocol support for a host.
/// </summary>
public enum HostProtocolSupport : byte
{
/// <summary>
/// Protocol support is unknown or undetermined.
/// </summary>
Unknown = 0x00,
/// <summary>
/// The host supports HTTP/1.
/// </summary>
HTTP1 = 0x01,
/// <summary>
/// The host supports HTTP/2.
/// </summary>
HTTP2 = 0x02,
/// <summary>
/// This is a file-based host.
/// </summary>
File = 0x03,
}
/// <summary>
/// <para>The HostVariant class is a critical component in managing HTTP connections and handling HTTP requests for a specific host. It maintains a queue of requests and a list of active connections associated with the host, ensuring efficient utilization of available resources. Additionally, it supports protocol version detection (HTTP/1 or HTTP/2) for optimized communication with the host.</para>
/// <list type="bullet">
/// <item><description>It maintains a queue of requests to ensure efficient and controlled use of available connections.</description></item>
/// <item><description>It supports HTTP/1 and HTTP/2 protocol versions, allowing requests to be sent using the appropriate protocol based on the host's protocol support.</description></item>
/// <item><description>Provides methods for sending requests, recycling connections, managing connection state, and handling the shutdown of connections and the host variant itself.</description></item>
/// <item><description>It includes logging for diagnostic purposes, helping to monitor and debug the behavior of connections and requests.</description></item>
/// </list>
/// <para>In summary, the HostVariant class plays a central role in managing HTTP connections and requests for a specific host, ensuring efficient and reliable communication with that host while supporting different protocol versions.</para>
/// </summary>
public class HostVariant
{
public HostKey Host { get; private set; }
public HostProtocolSupport ProtocolSupport { get; private set; }
public DateTime LastProtocolSupportUpdate { get; private set; }
public LoggingContext Context { get; private set; }
// All the connections. Free and processing ones too.
protected readonly List<ConnectionBase> Connections = new List<ConnectionBase>();
// Queued requests that aren't passed yet to a connection.
protected readonly Queue<HTTPRequest> Queue = new Queue<HTTPRequest>();
// Host-variant settings
protected readonly HostVariantSettings _settings;
// Cached list
protected List<KeyValuePair<int, ConnectionBase>> availableConnections;
public HostVariant(HostKey host)
{
this.Host = host;
if (this.Host.Uri.IsFile)
this.ProtocolSupport = HostProtocolSupport.File;
this.Context = new LoggingContext(this);
this.Context.Add("Host", this.Host.Host);
this._settings = HTTPManager.PerHostSettings.Get(this).HostVariantSettings;
this.availableConnections = new List<KeyValuePair<int, ConnectionBase>>(2);
}
public virtual void AddProtocol(HostProtocolSupport protocolSupport)
{
this.LastProtocolSupportUpdate = HTTPManager.CurrentFrameDateTime;
var oldProtocol = this.ProtocolSupport;
if (oldProtocol != protocolSupport)
{
this.ProtocolSupport = protocolSupport;
HTTPManager.Logger.Information(nameof(HostVariant), $"AddProtocol({oldProtocol} => {protocolSupport})", this.Context);
}
TryToSendQueuedRequests();
}
public virtual HostVariant Send(HTTPRequest request)
{
if (HTTPManager.Logger.IsDiagnostic)
HTTPManager.Logger.Verbose(nameof(HostVariant), $"Send({request})", this.Context);
request.Context.Remove(nameof(HostVariant));
request.Context.Add(nameof(HostVariant), this.Context);
this.Queue.Enqueue(request);
return TryToSendQueuedRequests();
}
public virtual HostVariant TryToSendQueuedRequests()
{
if (this.Queue.Count == 0)
return this;
(int activeConnections, int theoreticalMaximumPerConnection, int assignedRequests) = QueryAnyAvailableOrNew(ref availableConnections);
if (availableConnections.Count == 0)
{
#if !UNITY_WEBGL || UNITY_EDITOR
if (activeConnections > 0 && this.ProtocolSupport == HostProtocolSupport.Unknown)
return this;
#endif
if (activeConnections < this._settings.MaxConnectionPerVariant)
{
int queueSize = this.Queue.Count;
int currentMaximum = (activeConnections * theoreticalMaximumPerConnection) - assignedRequests;
while (activeConnections < this._settings.MaxConnectionPerVariant && currentMaximum < queueSize)
{
availableConnections.Add(new KeyValuePair<int, ConnectionBase>(0, CreateNew()));
currentMaximum += theoreticalMaximumPerConnection;
activeConnections++;
}
}
else
return this;
}
// Sort connections by theirs key (assigned requests count)
availableConnections.Sort((a, b) => a.Key - b.Key);
while (this.Queue.Count > 0 && availableConnections.Count > 0)
{
var nextRequest = this.Queue.Peek();
// If the queue is large, or timeouts are set low, a request might be in a queue while its state is set to > Finished.
// So we have to prevent sending it again.
if (nextRequest.State <= HTTPRequestStates.Queued)
{
var kvp = availableConnections[0];
if (HTTPManager.Logger.IsDiagnostic)
{
nextRequest.Context.Remove(nameof(HostVariant));
nextRequest.Context.Add(nameof(HostVariant), this.Context);
var key = kvp.Value.GetType().Name;
nextRequest.Context.Add(key, kvp.Value.Context);
HTTPManager.Logger.Information(nameof(HostVariant), $"Send({nextRequest.Context.Hash}, {key} => {kvp.Value.Context.Hash})", nextRequest.Context);
}
RequestEventHelper.EnqueueRequestEvent(new RequestEventInfo(nextRequest, HTTPRequestStates.Processing, null));
OnConnectionStartedProcessingRequest(kvp.Value, nextRequest);
// then start process the request
kvp.Value.Process(nextRequest);
if (kvp.Key + 1 >= kvp.Value.MaxAssignedRequests)
availableConnections.RemoveAt(0);
else
availableConnections[0] = new KeyValuePair<int, ConnectionBase>(kvp.Key + 1, kvp.Value);
availableConnections.Sort((a, b) => a.Key - b.Key);
}
this.Queue.Dequeue();
}
return this;
}
public virtual (int activeConnections, int theoreticalMaximumPerConnection, int assignedRequests) QueryAnyAvailableOrNew(ref List<KeyValuePair<int, ConnectionBase>> connectionCollector)
{
int activeConnections = 0;
int maxAssignedRequest = 1;
int assignedRequests = 0;
connectionCollector.Clear();
// Check the last created connection first. This way, if a higher level protocol is present that can handle more requests (== HTTP/2) that protocol will be chosen
// and others will be closed when their inactivity time is reached.
for (int i = Connections.Count - 1; i >= 0; --i)
{
var conn = Connections[i];
if (conn.State == HTTPConnectionStates.Initial ||
conn.State == HTTPConnectionStates.Free ||
(conn.CanProcessMultiple && conn.AssignedRequests < conn.MaxAssignedRequests))
connectionCollector.Add(new KeyValuePair<int, ConnectionBase>(conn.AssignedRequests, conn));
maxAssignedRequest = Math.Max(maxAssignedRequest, conn.MaxAssignedRequests);
assignedRequests += conn.AssignedRequests;
activeConnections++;
}
return (activeConnections, Math.Max(1, (int)(maxAssignedRequest * this._settings.MaxAssignedRequestsFactor)), assignedRequests);
}
public virtual ConnectionBase CreateNew()
{
if (HTTPManager.Logger.IsDiagnostic)
HTTPManager.Logger.Verbose(nameof(HostVariant), $"CreateNew({this.Host})", this.Context);
ConnectionBase conn = this._settings.ConnectionFactory?.Invoke(this._settings, this);
if (conn == null)
{
if (this.ProtocolSupport == HostProtocolSupport.File)
conn = new FileConnection(this.Host);
else
{
#if UNITY_WEBGL && !UNITY_EDITOR
conn = new Best.HTTP.Hosts.Connections.WebGL.WebGLXHRConnection(this.Host);
#else
conn = new HTTPOverTCPConnection(this.Host);
#endif
}
}
Connections.Add(conn);
return conn;
}
protected virtual void OnConnectionStartedProcessingRequest(ConnectionBase connection, HTTPRequest request)
{
}
public virtual HostVariant RecycleConnection(ConnectionBase conn)
{
conn.State = HTTPConnectionStates.Free;
Best.HTTP.Shared.Extensions.Timer.Add(new TimerData(TimeSpan.FromSeconds(1), conn, CloseConnectionAfterInactivity));
return this;
}
protected virtual bool RemoveConnectionImpl(ConnectionBase conn, HTTPConnectionStates setState)
{
if (HTTPManager.Logger.IsDiagnostic)
HTTPManager.Logger.Information(nameof(HostVariant), $"RemoveConnectionImpl({conn}, {setState})", this.Context);
conn.State = setState;
conn.Dispose();
bool found = this.Connections.Remove(conn);
if (!found) //
{
if (HTTPManager.Logger.IsDiagnostic)
HTTPManager.Logger.Information(nameof(HostVariant),
$"RemoveConnectionImpl - Couldn't find connection! key: {conn.HostKey}", this.Context);
}
return found;
}
public virtual HostVariant RemoveConnection(ConnectionBase conn, HTTPConnectionStates setState)
{
RemoveConnectionImpl(conn, setState);
return this;
}
public ConnectionBase Find(Predicate<ConnectionBase> match) => this.Connections.Find(match);
public bool HasConnection(ConnectionBase connection) => this.Connections.Contains(connection);
protected virtual bool CloseConnectionAfterInactivity(DateTime now, object context)
{
var conn = context as ConnectionBase;
bool closeConnection = conn.State == HTTPConnectionStates.Free && now - conn.LastProcessTime >= conn.KeepAliveTime;
if (closeConnection)
{
HTTPManager.Logger.Information(nameof(HostVariant), string.Format("CloseConnectionAfterInactivity - [{0}] Closing! State: {1}, Now: {2}, LastProcessTime: {3}, KeepAliveTime: {4}",
conn.ToString(), conn.State, now.ToString(System.Globalization.CultureInfo.InvariantCulture), conn.LastProcessTime.ToString(System.Globalization.CultureInfo.InvariantCulture), conn.KeepAliveTime), this.Context);
RemoveConnection(conn, HTTPConnectionStates.Closed);
return false;
}
// repeat until the connection's state is free
return conn.State == HTTPConnectionStates.Free;
}
public virtual void RemoveAllIdleConnections()
{
for (int i = 0; i < this.Connections.Count; i++)
if (this.Connections[i].State == HTTPConnectionStates.Free)
{
int countBefore = this.Connections.Count;
RemoveConnection(this.Connections[i], HTTPConnectionStates.Closed);
if (countBefore != this.Connections.Count)
i--;
}
}
public virtual void Shutdown()
{
this.Queue.Clear();
foreach (var conn in this.Connections)
{
// Swallow any exceptions, we are quitting anyway.
try
{
conn.Shutdown(ShutdownTypes.Immediate);
}
catch { }
}
//this.Connections.Clear();
}
public virtual void SaveTo(System.IO.BinaryWriter bw)
{
bw.Write(this.LastProtocolSupportUpdate.ToBinary());
bw.Write((byte)this.ProtocolSupport);
}
public virtual void LoadFrom(int version, System.IO.BinaryReader br)
{
this.LastProtocolSupportUpdate = DateTime.FromBinary(br.ReadInt64());
this.ProtocolSupport = (HostProtocolSupport)br.ReadByte();
if (DateTime.UtcNow - this.LastProtocolSupportUpdate >= TimeSpan.FromDays(1))
{
if (HTTPManager.Logger.IsDiagnostic)
HTTPManager.Logger.Verbose(nameof(HostVariant), $"LoadFrom - Too Old! LastProtocolSupportUpdate: {this.LastProtocolSupportUpdate.ToString(CultureInfo.InvariantCulture)}, ProtocolSupport: {this.ProtocolSupport}", this.Context);
this.ProtocolSupport = HostProtocolSupport.Unknown;
}
else if (HTTPManager.Logger.IsDiagnostic)
HTTPManager.Logger.Verbose(nameof(HostVariant), $"LoadFrom - LastProtocolSupportUpdate: {this.LastProtocolSupportUpdate.ToString(CultureInfo.InvariantCulture)}, ProtocolSupport: {this.ProtocolSupport}", this.Context);
}
public override string ToString() => $"{this.Host}, {this.Queue.Count}/{this.Connections?.Count}, {this.ProtocolSupport}";
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: cc3e7ed2db74f6b46aed6ce220fea2e7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: