RestQueue 0.6.0

dotnet add package RestQueue --version 0.6.0
NuGet\Install-Package RestQueue -Version 0.6.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.6.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RestQueue --version 0.6.0
#r "nuget: RestQueue, 0.6.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.
// Install RestQueue as a Cake Addin
#addin nuget:?package=RestQueue&version=0.6.0

// Install RestQueue as a Cake Tool
#tool nuget:?package=RestQueue&version=0.6.0

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

Apache License 2.0

Product Compatible and additional computed target framework versions.
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  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.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.6.0 1,027 3/12/2018
0.5.0 865 2/27/2018
0.4.0 924 2/21/2018
0.3.0 791 2/19/2018
0.2.0 867 2/19/2018
0.1.0 924 2/18/2018

fixed FixedRetryDelay initialization, renamed RestQueue.cs to Queue.cs and RestQueueRequest.cs to QueueRequest.cs to improve naming