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)
│ ... │ │ ... │ │ ... │ │ ... │
│ ... │ │ ... │ │ ... │ │ ... │
│ ... │ │ ... │ │ ... │ │ ... │
│ ... │ │ ... │ │ ... │ │ ... │
└─────────────┘ └─────┘ └─────────────┘ └─────┘
*/
///
/// The class provides centralized management for objects associated with HTTP requests and connections.
///
///
///
/// The class acts as a central registry for managing objects, each associated with a unique .
/// It facilitates the creation, retrieval, and management of instances based on HTTP requests and connections.
///
///
/// A 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 instance is used for
/// each unique host, helping optimize resource usage and connection pooling.
///
///
/// Key features of the class include:
///
///
/// -
/// Creation and Retrieval
///
/// The class allows you to create and retrieve instances based on HTTP requests, connections, or .
/// It ensures that a single is used for each unique host.
///
///
/// -
/// Queue Management
///
/// The manages the queue of pending requests for each , ensuring efficient request processing.
///
///
/// -
/// Connection Management
///
/// The class handles the management of connections associated with objects, including recycling idle connections,
/// removing idle connections, and shutting down connections when needed.
///
///
///
///
/// Usage of the 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.
///
///
public static class HostManager
{
///
/// Dictionary to store - mappings.
///
private static Dictionary hosts = new Dictionary();
///
/// Gets the associated with an HTTP request.
///
/// The HTTP request.
/// The for the request's host.
public static HostVariant GetHostVariant(HTTPRequest request) => GetHostVariant(request.CurrentHostKey);
///
/// Gets the associated with a connection.
///
/// The HTTP connection.
/// The for the connection's host.
public static HostVariant GetHostVariant(ConnectionBase connection) => GetHostVariant(connection.HostKey);
///
/// Gets the associated with a HostKey.
///
/// The HostKey for which to get the HostVariant.
/// The for the specified HostKey.
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;
}
///
/// Removes all idle connections for all hosts.
///
public static void RemoveAllIdleConnections()
{
HTTPManager.Logger.Information("HostManager", "RemoveAllIdleConnections");
foreach (var host in hosts)
host.Value.RemoveAllIdleConnections();
}
///
/// Tries to send queued requests for all hosts.
///
public static void TryToSendQueuedRequests()
{
foreach (var kvp in hosts)
kvp.Value.TryToSendQueuedRequests();
}
///
/// Shuts down all connections for all hosts.
///
public static void Shutdown()
{
HTTPManager.Logger.Information("HostManager", "Shutdown()");
foreach (var kvp in hosts)
kvp.Value.Shutdown();
}
///
/// Clears all hosts and their associated variants.
///
public static void Clear()
{
HTTPManager.Logger.Information("HostManager", "Clearing()");
hosts.Clear();
}
}
}