Logo

0x5a.live

for different kinds of informations and explorations.

GitHub - yudppp/throttle: lodash throttle like Go library

lodash throttle like Go library. Contribute to yudppp/throttle development by creating an account on GitHub.

Visit SiteGitHub - yudppp/throttle: lodash throttle like Go library

GitHub - yudppp/throttle: lodash throttle like Go library

lodash throttle like Go library. Contribute to yudppp/throttle development by creating an account on GitHub.

Powered by 0x5a.live ๐Ÿ’—

Throttle

test workflow Go Report Card Coverage Status

Throttle is an object that will perform exactly one action per duration. Do call the function f if a specified duration has passed since the last function f was called for this instance of Throttle.

Examples

single thread

package main

import (
	"fmt"
	"time"

	"github.com/yudppp/throttle"
)

func main() {
	throttler := throttle.New(time.Second)
	throttler.Do(func() {
		fmt.Println("first call")
	})
	throttler.Do(func() {
		// this function called never.
		fmt.Println("second call")
	})
	time.Sleep(time.Second)
	throttler.Do(func() {
		fmt.Println("third call")
	})
	time.Sleep(time.Second)
}
$ go run -race main.go
first call
third call

multiple threads

package main

import (
	"fmt"
	"time"

	"github.com/yudppp/throttle"
)

func main() {
	throttler := throttle.New(time.Second)
	var wg sync.WaitGroup
	for i := 0; i < 64; i++ {
		wg.Add(1)
		go func(i int) {
			throttler.Do(func() {
				fmt.Println("called")
			})
			wg.Done()
		}(i)
	}
	wg.Wait()
}
$ go run -race main.go
called

License

The MIT License (MIT)

GoLang Resources

are all listed below.

Resources

listed to get explored on!!

Made with โค๏ธ

to provide different kinds of informations and resources.