Skip to content

Patrolavia/ratelimit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 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, // bucket capacity, tune this if needed.
	0, // allocate this much tokens a time, 0 means capacity/2
)
r := ratelimit.NewReader(resp.Body, bucket)
io.Copy(dst, r)

See example folder for more.

Total transfer rate

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

License

LGPL v3 or later.

About

rate limiter for reader using Token-Bucket algorithm

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages