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 ¶
- Variables
- func Compute(now time.Time, bucket Bucket, cost int64, opts Options) (Bucket, Result, error)
- func ComputeRaw(tat, now, burst, rate, period, cost int64) (int64, bool, int64, int64, int64)
- func GenerateRaw(now, count, burst, rate, period int64) int64
- func MustCompute(now time.Time, bucket Bucket, cost int64, opts Options) (Bucket, Result)
- type Bucket
- type Options
- type Result
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 ComputeRaw ¶ added in v1.0.1
ComputeRaw us the underlying raw computation used in Compute.
func GenerateRaw ¶ added in v1.0.1
GenerateRaw is the underlying raw computation used in Generate.
Types ¶
type Bucket ¶ added in v1.0.1
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.
Click to show internal directories.
Click to hide internal directories.