gcra

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2022 License: MIT Imports: 3 Imported by: 0

README

gcra

Test GoDoc Release

Package gcra implements the generic cell rate algorithm (GCRA).

Example

opts := Options{
    Burst:  50,
    Rate:   10,
    Period: time.Second,
}

now := time.Now()

bucket := MustGenerate(now, 25, opts)

bucket, result := MustCompute(now, bucket, 10, opts)
fmt.Printf("%+v\n", result)

bucket, result = MustCompute(now, bucket, 30, opts)
fmt.Printf("%+v\n", result)

bucket, result = MustCompute(now, bucket, 15, opts)
fmt.Printf("%+v\n", result)

now = now.Add(2 * time.Second)

bucket, result = MustCompute(now, bucket, 0, opts)
fmt.Printf("%+v\n", result)

fmt.Printf("Bucket Offset: %s", time.Time(bucket).Sub(now).String())

// Output:
// {Limited:false Remaining:15 RetryIn:0s ResetIn:3.5s}
// {Limited:true Remaining:15 RetryIn:1.5s ResetIn:3.5s}
// {Limited:false Remaining:0 RetryIn:0s ResetIn:5s}
// {Limited:false Remaining:20 RetryIn:0s ResetIn:3s}
// Bucket Offset: 3s

Documentation

Overview

Package gcra implements the generic cell rate algorithm (GCRA).

Example
opts := Options{
	Burst:  50,
	Rate:   10,
	Period: time.Second,
}

now := time.Now()

bucket := MustGenerate(now, 25, opts)

bucket, result := MustCompute(now, bucket, 10, opts)
fmt.Printf("%+v\n", result)

bucket, result = MustCompute(now, bucket, 30, opts)
fmt.Printf("%+v\n", result)

bucket, result = MustCompute(now, bucket, 15, opts)
fmt.Printf("%+v\n", result)

now = now.Add(2 * time.Second)

bucket, result = MustCompute(now, bucket, 0, opts)
fmt.Printf("%+v\n", result)

fmt.Printf("Bucket Offset: %s", time.Time(bucket).Sub(now).String())
Output:

{Limited:false Remaining:15 RetryIn:0s ResetIn:3.5s}
{Limited:true Remaining:15 RetryIn:1.5s ResetIn:3.5s}
{Limited:false Remaining:0 RetryIn:0s ResetIn:5s}
{Limited:false Remaining:20 RetryIn:0s ResetIn:3s}
Bucket Offset: 3s

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrCostHigherThanBurst = errors.New("cost higher than burst")

ErrCostHigherThanBurst is returned if the provided cost is higher than the specified burst.

View Source
var ErrInvalidParameter = errors.New("invalid parameter")

ErrInvalidParameter is returned if a parameter is zero.

Functions

func Compute added in v1.0.1

func Compute(now time.Time, bucket Bucket, cost int64, opts Options) (Bucket, Result, error)

Compute will perform the GCRA. Cost may be zero to query the bucket.

func ComputeRaw added in v1.0.1

func ComputeRaw(tat, now, burst, rate, period, cost int64) (int64, bool, int64, int64, int64)

ComputeRaw us the underlying raw computation used in Compute.

func GenerateRaw added in v1.0.1

func GenerateRaw(now, count, burst, rate, period int64) int64

GenerateRaw is the underlying raw computation used in Generate.

func MustCompute added in v1.0.1

func MustCompute(now time.Time, bucket Bucket, cost int64, opts Options) (Bucket, Result)

MustCompute will call Compute and panic on errors.

Types

type Bucket added in v1.0.1

type Bucket time.Time

Bucket represents a GCRA bucket. The value represents the theoretical arrival time (TAT) which encodes the point in time at which the bucket is full again.

func Generate added in v1.0.1

func Generate(now time.Time, count int64, opts Options) (Bucket, error)

Generate will create a bucket that contains the specified amount of tokens at this point in time.

func MustGenerate added in v1.0.1

func MustGenerate(now time.Time, count int64, opts Options) Bucket

MustGenerate will call Generate and panic on errors.

type Options added in v1.0.1

type Options struct {
	Burst  int64
	Rate   int64
	Period time.Duration
}

Options define the GCRA options. Specify burst as the maximum tokens available and rate as the regeneration of tokens per period.

type Result

type Result struct {
	Limited   bool
	Remaining int64
	RetryIn   time.Duration
	ResetIn   time.Duration
}

Result is the result of a GCRA computation.

Jump to

Keyboard shortcuts

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