RestQueue 0.2.0
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package RestQueue --version 0.2.0
NuGet\Install-Package RestQueue -Version 0.2.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RestQueue" Version="0.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RestQueue" Version="0.2.0" />
<PackageReference Include="RestQueue" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add RestQueue --version 0.2.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: RestQueue, 0.2.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package RestQueue@0.2.0
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=RestQueue&version=0.2.0
#tool nuget:?package=RestQueue&version=0.2.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
RestQueue - C# queue wrapper around HttpClient
Useful for processing messages when the response is not important, just think of logging, auditing, ..
By using RestQueue to process your requests you get
- Improved performance by releasing the original caller thread, have the processing of the messages in the background
- Limited resources usage by queuing messages and reducing the number of connections needed
- Built-in retry mechanism
- Ability to add queue adjustment strategy to react for difference load and network conditions (see example below)
- Adjustable retry mechanism - Exponential Backoff or fixed time
Example usage
Initialization
var restQueue = new RestQueue(new Uri("http://localhost:52159/api/"))
{
RequestTimeout = TimeSpan.FromSeconds(10),
RetryDelay = TimeSpan.FromMilliseconds(500),
BatchSize = 100,
BatchDelay = TimeSpan.FromSeconds(1)
};
Queue some requests
for (int i = 0; i < 10000; i++)
{
log = new Log()
{
Timestamp = DateTime.Now,
Message = "test " + i
};
restQueue.Enqueue("logs/", log);
}
Automatic Adjustment
By monitoring the statistics of the queue we can actually adjust in real time the configuration of the queue - batch size and delay between batches
/// <summary>
/// adjusts the batch delay according to success rate and thresholds
/// we're focusing on batch delay here but batch size can also be adjusted
/// </summary>
/// <param name="restQueue">the rest queue instance</param>
/// <param name="minBatchDelay">the minimum batch delay that would like to have, don't have this too low as it will consume more CPU</param>
/// <param name="maxBatchDelay">the maximum batch delay that we can afford</param>
/// <param name="minSuccessRate">minimum success rate to adjustment</param>
/// <param name="maxSuccessRate">maximum success rate to adjustment</param>
private static void AdjustQueue(RestQueue restQueue, double minBatchDelay, double maxBatchDelay, int minSuccessRate, int maxSuccessRate)
{
double batchDelay = restQueue.BatchDelay.TotalMilliseconds;
if (successfulCount > 0)
{
var rate = GetSuccessRate();
// if we're below the success rate that we're looking for and the batch delay can be adjusted then try to increase it by 10%
if (rate < minSuccessRate && batchDelay < maxBatchDelay)
{
Trace.WriteLine("success rate too low, increasing batch delay");
// increase delay
restQueue.BatchDelay = TimeSpan.FromMilliseconds(Math.Min((batchDelay + maxBatchDelay) / 2, maxBatchDelay));
}
// if we're above the success rate that we're looking for, we have pending requests and the batch delay can be adjusted then try to decrease it by 10%
if (rate > maxSuccessRate && restQueue.Pending > 0 && batchDelay < minBatchDelay)
{
Trace.WriteLine("success rate too high, decreasing batch delay");
// decrease delay
restQueue.BatchDelay = TimeSpan.FromMilliseconds(Math.Max((batchDelay + minBatchDelay) / 2, minBatchDelay));
}
}
}
License
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET Framework | net461 is compatible. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
- Newtonsoft.Json (>= 10.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Initial release.