Skip to content

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

License

Notifications You must be signed in to change notification settings

dillonstreator/throttle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 (
	"context"
	"fmt"
	"time"

	"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 (
	"context"
	"fmt"
	"time"

	"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.

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages