Skip to content

codingconcepts/throttle

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 

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
}

About

Provides a simple interface for throttling function calls.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages