Go API Documentation

github.com/TykTechnologies/tyk/internal/rate/model

No package summary is available.

Package

Files: 2. Third party imports: 0. Imports from organisation: 1. Tests: 0. Benchmarks: 0.

Types

Allowance

Allowance is a redis data model type. It's encoded into a redis Hash type.

Field name Field type Comment
Delay

int64

Delay is the minimum time between rate limit changes (in seconds).

Current

int64

Current holds the current rate limit allowance in effect.

NextUpdateAt

time.Time

NextUpdateAt is the next allowable update time for the allowance.

type Allowance struct {
	// Delay is the minimum time between rate limit changes (in seconds).
	Delay	int64	`redis:"delay"`

	// Current holds the current rate limit allowance in effect.
	Current	int64	`redis:"current"`

	// NextUpdateAt is the next allowable update time for the allowance.
	NextUpdateAt	time.Time	`redis:"nextUpdateAt"`
}

AllowanceRepository

AllowanceRepository is the interface for accessing rate limit allowance.

Field name Field type Comment
type

any

No comment on field.
type AllowanceRepository interface {
	// Stringer is implemented to expose repository internal info/summary.
	fmt.Stringer

	// Locker implements a distributed lock.
	Locker(name string) limiters.DistLocker

	// Get will retrieve the allowance from storage.
	Get(ctx context.Context, key string) (*Allowance, error)

	// Set will write the allowance to storage.
	Set(ctx context.Context, key string, allowance *Allowance) error
}

Locker

This type doesn't have documentation.

Field name Field type Comment
type

limiters.DistLocker

No comment on field.
type Locker = limiters.DistLocker

RedisClientProvider

RedisClientProvider is a hidden storage API, providing us with a redis.UniversalClient.

Field name Field type Comment
type

any

No comment on field.
type RedisClientProvider interface {
	// Client returns the redis.UniversalClient or an error if not available.
	Client() (redis.UniversalClient, error)
}

SmoothingFn

SmoothingFn is the signature for a rate limiter decision based on rate.

Field name Field type Comment
type

func(ctx context.Context, key string, currentRate int64, maxAllowedRate int64) bool

No comment on field.
type SmoothingFn func(ctx context.Context, key string, currentRate int64, maxAllowedRate int64) bool

Functions

func NewAllowance

NewAllowance creates a new allowance with the update delay (in seconds).

func NewAllowance(delay int64) *Allowance {
	return &Allowance{
		Delay: delay,
	}
}

Cognitive complexity: 1, Cyclomatic complexity: 1

func NewAllowanceFromMap

NewAllowanceFromMap will scan the in parameter and convert it to *Allowance.

func NewAllowanceFromMap(in map[string]string) *Allowance {
	result := &Allowance{}

	// This block of code ignores errors in favor of the zero value.
	// Regardless of error, the zero value is returned and used.
	result.Delay, _ = strconv.ParseInt(in["delay"], 10, 0)
	result.Current, _ = strconv.ParseInt(in["current"], 10, 0)
	result.NextUpdateAt, _ = time.Parse(time.RFC3339Nano, in["nextUpdateAt"])

	return result
}

Cognitive complexity: 1, Cyclomatic complexity: 1

Uses: strconv.ParseInt, time.Parse, time.RFC3339Nano.

func (*Allowance) Err

Err returns a validation error for *Allowance.

func (a *Allowance) Err() error {
	if a.Delay <= 0 {
		return errors.New("allowance: invalid delay")
	}
	return nil
}

Cognitive complexity: 2, Cyclomatic complexity: 2

Uses: errors.New.

func (*Allowance) Expired

Expired checks if the allowance can be updated based on the configured delay.

func (a *Allowance) Expired() bool {
	return time.Since(a.NextUpdateAt) > 0
}

Cognitive complexity: 0, Cyclomatic complexity: 1

Uses: time.Since.

func (*Allowance) Get

Get returns the current allowance.

func (a *Allowance) Get() int64 {
	return a.Current
}

Cognitive complexity: 0, Cyclomatic complexity: 1

func (*Allowance) GetDelay

GetDelay returns the delay for rate limit smoothing as a time.Duration.

func (a *Allowance) GetDelay() time.Duration {
	return time.Second * time.Duration(a.Delay)
}

Cognitive complexity: 0, Cyclomatic complexity: 1

Uses: time.Duration, time.Second.

func (*Allowance) Map

Map will return an allowance as a map.

func (a *Allowance) Map() map[string]any {
	return map[string]any{
		"delay":	fmt.Sprint(a.Delay),
		"current":	fmt.Sprint(a.Current),
		"nextUpdateAt":	a.NextUpdateAt.Format(time.RFC3339Nano),
	}
}

Cognitive complexity: 1, Cyclomatic complexity: 1

Uses: fmt.Sprint, time.RFC3339Nano.

func (*Allowance) Reset

Reset will clear the allowance.

func (a *Allowance) Reset() {
	if a == nil {
		return
	}
	a.Current = 0
	a.NextUpdateAt = time.Time{}
}

Cognitive complexity: 3, Cyclomatic complexity: 2

Uses: time.Time.

func (*Allowance) Set

Set updates the current allowance to the specified value and sets the next update time based on the configured delay.

func (a *Allowance) Set(allowance int64) {
	a.Current = allowance
	a.Touch()
}

Cognitive complexity: 0, Cyclomatic complexity: 1

func (*Allowance) Touch

Touch updates the next allowance time to the configured delay.

func (a *Allowance) Touch() {
	a.NextUpdateAt = time.Now().Add(a.GetDelay())
}

Cognitive complexity: 0, Cyclomatic complexity: 1

Uses: time.Now.

func (*Allowance) Valid

Valid returns false if validation with Err() fails.

func (a *Allowance) Valid() bool {
	err := a.Err()
	return err == nil
}

Cognitive complexity: 0, Cyclomatic complexity: 1


Tests

Files: 1. Third party imports: 1. Imports from organisation: 0. Tests: 9. Benchmarks: 0.

Test functions

TestAllowance_Err

References: assert.Error, assert.NoError, testing.T.

TestAllowance_Expired

References: assert.False, assert.True, time.Minute, time.Now.

TestAllowance_Get

References: assert.Equal.

TestAllowance_GetDelay

References: assert.Equal, time.Second.

TestAllowance_Reset

References: assert.Equal, assert.Nil, assert.True, time.Now.

TestAllowance_Set

References: assert.Equal, assert.False.

TestAllowance_Touch

References: assert.WithinDuration, time.Now, time.Second.

TestAllowance_Valid

References: assert.False, assert.True, testing.T.

TestNewAllowance

References: assert.Equal, assert.NoError, assert.NotNil, assert.True, testing.T, time.Now, time.Parse, time.RFC3339Nano.