slidingwindow

package
v0.0.0-...-156932d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 14, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package slidingwindow provides an implementation of Sliding Window Algorithm.

Design Let's take an limiter built on slidingwindow as an example to illustrate how the slidingwindow works.

Suppose we have a limiter that permits 100 events per minute, and now the time comes at the "75s" point, then the internal sliding window will be as below:

```

Sliding Window

|-------------------------------------| | Previous Window | Current Window | window size: 60s |------------------|------------------| | 86 | 12 | |------------------|------------------| ^ ^ ^ ^ ^ | | | | | 0s 15s 60s 75s 120s

```

In this situation, the limiter has permitted 12 events during the current window, which started 15 seconds ago, and 86 events during the entire previous window. Then the count approximation during the sliding window can be calculated like this:

``` count = 86 * ((60-15)/60) + 12

= 86 * 0.75 + 12
= 76.5 events

```

In the example, count of requests is recorded in the slidingwindow. Actually we can see the request as an event, then we can record any events if we want.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SlidingWindow

type SlidingWindow struct {
	// contains filtered or unexported fields
}

SlidingWindow sliding window consists two windows `curr` and `prev`, the window is advanced when recording events.

func NewSlidingWindow

func NewSlidingWindow(size time.Duration) *SlidingWindow

NewSlidingWindow creates a new slidingwindow

func (*SlidingWindow) Count

func (sw *SlidingWindow) Count() int64

func (*SlidingWindow) Record

func (sw *SlidingWindow) Record()

Allow is shorthand for AllowN(time.Now(), 1).

func (*SlidingWindow) Size

func (sw *SlidingWindow) Size() time.Duration

Size returns the time duration of one window size. Note that the size is defined to be read-only, if you need to change the size, create a new limiter with a new size instead.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL