Skip to content

rate limiter for reader using Token-Bucket algorithm

Notifications You must be signed in to change notification settings

raohwork/ratelimit

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ratelimit

ratelimit implements Token-Bucket algorithm to help you limit transfer rate. It is rewrote from and inspired by juju/ratelimit.

Synopsis

resp, err := http.Get("http://example.com/")
if err != nil {
	// error process
}
defer resp.Body.Close()

bucket := ratelimit.NewFromRate(
	100*1024, // limit transfer rate to 10kb/s
	100*1024, // burst rate (bucket capacity), tune this if needed.
	0, // allocate this much tokens a time, 0 or lesser means rate/10
)
r := bucket.NewReader(resp.Body)
io.Copy(dst, r)

See example folder for more.

Total transfer rate

Bucket is thread-safe. You can share same Bucket between readers/writers to limit the total transfer rate.

License

LGPL v3

About

rate limiter for reader using Token-Bucket algorithm

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%