Per-Host Settings¶
The HostSettingsManager class in the Best.HTTP.Hosts.Settings
namespace is a comprehensive solution for managing host-specific settings for HTTP requests. This guide will help you grasp its functionality, understand its relevance, and employ it efficiently in your applications. It can be accessed via HTTPManager.PerHostSettings.
What Does It Do?¶
The HostSettingsManager allows you to fine-tune HTTP request and connection behaviors based on the hostname. This can be beneficial when interacting with multiple endpoints, each requiring its unique set of configurations.
Key Features¶
- Host-Specific Settings: Enables custom configurations for specific hostnames.
- Fallback Configuration: If a specific hostname configuration is not found, a default configuration (
*
) is used. - Subdomain Matching: Supports wildcard (
*
) notation for matching subdomains. E.g.,*.example.com
matchesa.example.com
anda.b.example.com
but notexample.com
.
Why Use It?¶
- Granular Control: In large-scale applications, different servers or services might need unique connection settings. Instead of using a one-size-fits-all configuration, HostSettingsManager allows you to specify settings per hostname.
- Efficient Resource Utilization: For instance, by disabling connection pooling or HTTP/2 for particular services, you can ensure optimal resource utilization and performance.
- Simplified Management: HostSettingsManager offers a structured way to manage host-specific settings, making it easier to maintain and modify configurations as your application scales.
How to Use It¶
Initializing¶
The manager instance is accessible through HTTPManager.PerHostSettings with default settings for the root/fallback/default configuration (*
).
There are multiple ways to add new configurations, but the most straightforward is to use AddDefault and alter its fields:
Now all requests sent to example.com
will use a 20 seconds request timeout.
Warning
Adding or modifying settings isn't thread-safe, initialize them before any other plugin related call!
How To Retrieve Settings for a Host¶
If settings for the specified hostname aren't found, the manager will return the default settings associated with '*
'.
Modifying Global Settings¶
For example, to turn off connection pooling and HTTP/2 for global settings:
Adding Settings for Multiple Hosts¶
You can easily add settings for multiple hosts. By default, both connection pooling and HTTP/2 are enabled:
Clearing All Settings¶
To remove all configurations and reset the default call Clear()
:
Clear keeps the fallback configuration (*
), but resets its settings back to default.