Sitecore Experience Management

Create a new request throttling strategy

Abstract

How to implement a custom strategy

Note

This topic is valid for Sitecore 9, Update 2, and later.

You can create a new request throttling strategy. You must:

  • Create a strategy item template

  • Implement a strategy class

  • Apply the strategy

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:

    Create_a_new_request_throttling_strategy-Picture_17-rId9-595221051.png

To create a strategy class:

  1. In Visual Studio, create a project, and create a strategy class that inherits from the DataThrottleStrategy base class.

  2. 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.

  3. 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;
namespace CustomService.Throttle.Strategies
{
    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;
    }
}

To apply the strategy:

  • Create a strategy item from the strategy template you created:

    2-Create_a_new_request_throttling_strategy-Picture_18-rId10-1606959239.png

Now you can apply the strategy with the methods described.