Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func NewAlgorithm ¶
func NewAlgorithm(maxEvents uint32, interval time.Duration, segments uint32, options ...Option) (rate.LimiterAlgorithm, error)
NewAlgorithm returns rate.LimiterAlgorithm with Sliding Window Counter implementation that's based on Fixed Window and Sliding Window algorithms to make a perfect balance between memory consumption and accuracy. The higher value of segments means better accuracy but also more memory consumption. The minimum value of segments is 2 and there is no check for maximum, however too big value can lead to inaccurate results so test the values for yourself to find the best accuracy to your usecase since it can vary depending on interval value. Segments are fixed-length time slots of value `interval/segments`. Each has an internal counter. The last segment is the current one which is incrementing, after segment duration, it's shifted to the back of 1 segment slot so if the segment duration passes, the last has 0 value, and the oldest is replaced by a counter at 1 index e.g [1, 2, 3, 4, 5] => [2, 3, 4, 5, 0]. It's recommended to use for frequent events in relatively small interval like web API rate limitation.