Skip to content
Provides a simple interface for throttling function calls.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore
LICENSE
README.md
throttle.go
throttle_test.go

README.md

throttle

Provides a simple interface for throttling function calls.

Installation

$ go get -u github.com/codingconcepts/throttle

Usage

package main

import (
	"fmt"
	"sync/atomic"
	"time"

	"github.com/codingconcepts/throttle"
)

func main() {
	// Create a new throttle of 10 ops/s.
	r := throttle.New(10, time.Second)

	var sum int64
	f := func() {
		atomic.AddInt64(&sum, 1)
	}

	// Run 20 times (takes 2s because we're running 10 ops/s).
	r.Do(context.Background(), 20, f)
	fmt.Printf("sum: %d\n", sum)
	// Outputs: 20

	// Run for 3 seconds.
	r.DoFor(context.Background(), time.Second*3, f)
	fmt.Printf("sum: %d\n", sum)
	// Outputs: 50
}
You can’t perform that action at this time.