All Projects → boz → go-throttle

boz / go-throttle

Licence: other
Go throttle package for rate limiting arbitrary code.

Programming Languages

go
31211 projects - #10 most used programming language
Makefile
30231 projects

Labels

Projects that are alternatives of or similar to go-throttle

RateLimiting.NET
Rate Limiting (debounce, throttle) for C# Portable Class Library
Stars: ✭ 20 (-33.33%)
Mutual labels:  throttle
laravel-alert-notifications
Send alert to email, microsoft teams from laravel app, when an exception occurs. Throttle is enabled by default.
Stars: ✭ 22 (-26.67%)
Mutual labels:  throttle
asyncio-throttle
Simple, easy-to-use throttler for asyncio.
Stars: ✭ 95 (+216.67%)
Mutual labels:  throttle
stream-throttle
Rust Stream combinator, to limit the rate at which items are produced
Stars: ✭ 19 (-36.67%)
Mutual labels:  throttle
rush
rush.readthedocs.io/en/latest/
Stars: ✭ 42 (+40%)
Mutual labels:  throttle
MyDemos
💾 Demo 集合 . 黑发不知勤学早,白首方悔读书迟.
Stars: ✭ 64 (+113.33%)
Mutual labels:  throttle
anim-event
Event Manager for Animation
Stars: ✭ 25 (-16.67%)
Mutual labels:  throttle
together
Group things together!
Stars: ✭ 35 (+16.67%)
Mutual labels:  throttle
defense
🔮 A Crystal HTTP handler for throttling, blocking and tracking malicious requests.
Stars: ✭ 51 (+70%)
Mutual labels:  throttle
DebounceMonitoring
📑 Add debounce logic for any method in a single line.
Stars: ✭ 44 (+46.67%)
Mutual labels:  throttle
Node Rate Limiter Flexible
Node.js rate limit requests by key with atomic increments in single process or distributed environment.
Stars: ✭ 1,950 (+6400%)
Mutual labels:  throttle
raf-perf
RAF loop with an adaptive fps and performance ratio calculated from either a sample count or a sample duration. Typically used when doing intensive graphics computation in canvas.
Stars: ✭ 40 (+33.33%)
Mutual labels:  throttle
debounced-notifications
Basecamp style notification debouncing / throttling for Laravel notifications.
Stars: ✭ 30 (+0%)
Mutual labels:  throttle
adaptive throttler
manages multiple throttlers with ability to ramp up and down
Stars: ✭ 31 (+3.33%)
Mutual labels:  throttle

go-throttle GoDoc Build Status

Package throttle provides functionality to limit the frequency with which code is called

Throttling is of the Trigger() method and depends on the parameters passed (period, trailing).

The period parameter defines how often the throttled code can run. A period of one second means that the throttled code will run at most once per second.

The trailing parameter defines what hapens if Trigger() is called after the throttled code has been started, but before the period is finished. If trailing is false then these triggers are ignored. If trailing is true then the throttled code is executed one more time at the beginning of the next period.

Example with period = time.Second and trailing = false:

Whole seconds after first trigger...|0|0|0|0|1|1|1|1|
Trigger() gets called...............|X| |X| | |X| | |
Throttled code gets called..........|X| | | | |X| | |

Note that the second Trigger() had no effect. The third Trigger() caused immediate execution of the throttled code.

Example with period = time.Second and trailing = true:

Whole seconds after first trigger...|0|0|0|0|1|1|1|1|
Trigger() gets called...............|X| |X| | |X| | |
Throttled code gets called..........|X| | | |X| | | |

Note that the second Trigger() causes the throttled code to get called once the first period is over. The third Trigger() will do the same.

Usage

Throttling execution of a function:

throttle := throttle.ThrottleFunc(period, false, func() {
  fmt.Println("fun, throttled.")
})

go func() {
  for i := 0; i < 5; i++ {
    throttle.Trigger()
    time.Sleep(period / 6)
  }
}()

time.Sleep(2 * period)
throttle.Stop()

// Output: fun, throttled.

Throttling arbitrary code:

package cache

import (
	"time"
	"github.com/boz/go-throttle"
)

type CacheRebuilder struct {
	throttle throttle.Throttle
}

// Create a cache rebuilder which will rebuild the cache at most once every 5 minutes, regardless
// of how often a rebuild is requested.
func NewRebuilder() *CacheRebuilder {
	cr := &CacheRebuilder{NewThrottle(5*time.Minute, true)}

	go func() {
		for cr.throttle.Next() {
			cr.doRebuild()
		}
	}()

	return cr
}

func (cr *CacheRebuilder) Stop() {
	cr.throttle.Stop()
}

func (cr *CacheRebuilder) Rebuild() {
	cr.throttle.Trigger()
}

func (cr *CacheRebuilder) doRebuild() {
	// actually rebuild the cache.
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].