Create a new request throttling strategy
You can create a new request throttling strategy. You must:
-
Create a strategy item template
-
Implement a strategy class
-
Apply the strategy
Create a strategy item template
To add a strategy item template:
-
Create a new template under /sitecore/templates/System/Services/Throttle, using the /System/Templates/Template template. Add fields and set the base template to /sitecore/templates/System/Services/Throttle/Strategy template.
-
To configure the insert rules, add the template you created to the list in the Masters field in the /sitecore/templates/System/Services/Throttle/Strategy Folder/__Standard Values item.
-
Add standard values for the template you created and set the default values for the fields:
Implement a strategy class
To create a strategy class:
-
In Visual Studio, create a project, and create a strategy class that inherits from the
DataThrottleStrategybase class. -
You must implement these three methods:
-
InitCore – Put all initialization steps here (for example, binding the strategy properties to strategy item fields.)
-
VerifyCore – Put the logic that specifies whether a request is allowed or not here. You can save the internal state in the CustomData field of the ThrottleStrategyData class. An instance of this class is passed as an argument to the method.
-
CommitCore – Use this method if all the strategies allowed the request and the strategy can change its internal state if it needs to do it.
-
-
Register the new strategy by creating the class implementing
IServicesConfiguratorinterface, then patch theservicessection to add the following:RequestResponse<configurator type="CustomService.Throttle.Strategies.CustomStrategyInjections, YourDLLName" /> -
Put the binary assembly file in the bin folder of your Sitecore instance.
The following is an example of a strategy class implementation:
using System;
using System.Globalization;
using Sitecore.Diagnostics;
using Sitecore.Services.Infrastructure.Throttle;
using Sitecore.Services.Infrastructure.Throttle.Strategies;
using Microsoft.Extensions.DependencyInjection;
using Sitecore.DependencyInjection;
namespace CustomService.Throttle.Strategies
{
public class CustomStrategyInjections : IServicesConfigurator
{
public void Configure(IServiceCollection serviceCollection)
{
serviceCollection.AddTransient<MyCustomStrategy>();.
}
}
public class MyCustomStrategy : DataThrottleStrategy
{
private int _customField;
private int _result;
private MyCustomData _myCustomData;
public MyCustomStrategy(ThrottleStrategyDataStorage storage) : base(storage)
{
}
protected override void InitCore(ThrottleInitializationContext throttleInitializationContext)
{
// get parameters from item
Assert.ArgumentNotNull(throttleInitializationContext.StrategyItem, nameof(throttleInitializationContext.StrategyItem));
_customField = int.Parse(throttleInitializationContext.StrategyItem["{D55875C2-D5CA-48D9-8FBD-8092887F980F}"], CultureInfo.InvariantCulture);
Assert.ArgumentCondition(_customField > 0, nameof(_customField), "Custom parameter is lower or equal 0");
}
protected override ThrottleStrategyResult VerifyCore(ThrottleStrategyData throttleStrategyData)
{
_myCustomData = throttleStrategyData.CustomData as MyCustomData;
if (_myCustomData != null)
{
_result = _myCustomData.Rnd.Next(0, _customField);
// if random number is in second half of 0.._customField span then the request is not passed
if (_result > _customField / 2)
{
return new ThrottleStrategyResult
{
Passed = false,
RetryAfter = 5
};
}
}
return ThrottleStrategyResult.PassedResult;
}
protected override void CommitCore()
{
_myCustomData.Result = _result;
}
}
internal class MyCustomData
{
internal readonly Random Rnd = new Random();
internal int Result;
}
}Apply the strategy
To apply the strategy:
-
Create a strategy item from the strategy template you created:
Now you can apply the strategy with the methods described.