throttle

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 12, 2022 License: MIT Imports: 6 Imported by: 0

README

throttle

A simple golang throttle utility for redis and in memory, allowing inline and curried throttling.

Installation

go get github.com/DillonStreator/throttle

Usage

Both the RedisThrottler and MemoryThrottler adhere to the Throttler interface which exposes a Do method for inline throttling and a New method for curried throttling.

The error throttle.ErrThrottled is returned in the event that a function call is throttled, allowing handling of this event if necessary.

Inline throttling with .Do
package main

import "github.com/DillonStreator/throttle"

func main() {
    throttler := throttle.NewMemoryThrottler()

    key := "example:do"
	fn := func(ctx context.Context) error {
		fmt.Printf("called %s\n", key)
		return nil
	}

    err := throttler.Do(ctx, key, time.Second, fn)
    fmt.Println(err) // nil

    time.Sleep(time.Millisecond * 500)

    err = throttler.Do(ctx, key, time.Second, fn)
    fmt.Println(err) // throttle.ErrThrottled

    time.Sleep(time.Millisecond * 500)

    err = throttler.Do(ctx, key, time.Second, fn)
    fmt.Println(err) // nil
}
Curried throttling with .New
package main

import "github.com/DillonStreator/throttle"

func main() {
    throttler := throttle.NewMemoryThrottler()
    
	key := "example:new"
	fn := throttler.New(key, time.Second, func(ctx context.Context) error {
		fmt.Printf("called %s\n", key)
		return nil
	})

	ctx := context.Background()

	err := fn(ctx)
	fmt.Println(err) // nil

	time.Sleep(time.Millisecond * 500)

	err = fn(ctx)
	fmt.Println(err) // throttle.ErrThrottled

	time.Sleep(time.Millisecond * 500)

	err = fn(ctx)
	fmt.Println(err) // nil
}

Note

Trailing edge function invocation is not supported by this library. Only the leading edge is considered.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultMemoryThrottler = NewMemoryThrottler()

DefaultMemoryThrottler a default MemoryThrottler

View Source
var (
	ErrThrottled = errors.New("throttled")
)

Functions

This section is empty.

Types

type MemoryThrottler

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

MemoryThrottler in memory throttler

func NewMemoryThrottler

func NewMemoryThrottler() *MemoryThrottler

NewMemoryThrottler creates a new MemoryThrottler

func (*MemoryThrottler) Do

func (m *MemoryThrottler) Do(ctx context.Context, key string, duration time.Duration, fn func(ctx context.Context) error) error

Do inline in memory throttling

func (*MemoryThrottler) New

func (m *MemoryThrottler) New(key string, duration time.Duration, fn func(ctx context.Context) error) func(context.Context) error

New curried in memory throttling

type RedisThrottler

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

RedisThrottler redis throttler

func NewRedisThrottler

func NewRedisThrottler(redisClient redis.UniversalClient) *RedisThrottler

NewRedisThrottler creates a new RedisThrottler

func (*RedisThrottler) Do

func (r *RedisThrottler) Do(ctx context.Context, key string, duration time.Duration, fn func(ctx context.Context) error) error

New inline redis throttling

func (*RedisThrottler) New

func (r *RedisThrottler) New(key string, duration time.Duration, fn func(ctx context.Context) error) func(context.Context) error

New curried redis throttling

type Throttler

type Throttler interface {
	Do(ctx context.Context, key string, duration time.Duration, fn func(ctx context.Context) error) error
	New(key string, duration time.Duration, fn func(ctx context.Context) error) func(context.Context) error
}

Throttler is the throttling interface

Jump to

Keyboard shortcuts

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