Skip to content

octu0/priorate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

priorate

Apache License GoDoc Go Report Card Releases

priorate provides rate limiter with priority using golang.org/x/time/rate.
Priority can be defined as a ratio from 0.01 to 0.99, and rate limit can be performed according to priority on a given limit.
Fairly gets limit according to priority.

Installation

go get github.com/octu0/priorate

Example

Here's a quick example for using priorate.NewLimiter.

import(
	"fmt"
	"time"

	"github.com/octu0/priorate"
)

func main() {
	limit := priorate.NewLimiter(100,
		priorate.Priority(priorate.High, 0.7),
		priorate.Priority(priorate.Low, 0.3),
	)
	for i := 0; i < 10; i += 1 {
		if i < 5 {
			high := limit.ReserveN(priorate.High, time.Now(), 30)
			printDelay(high.Delay())
		} else {
			low := limit.ReserveN(priorate.Low, time.Now(), 30)
			printDelay(low.Delay())
		}
	}

	// Output:
	// 0s
	// 0s
	// 299ms
	// 1.099s
	// 1.899s
	// 1.199s
	// 1.966s
	// 3.233s
	// 4.499s
	// 5.766s
}

func printDelay(d time.Duration) {
	fmt.Println(d.Truncate(time.Millisecond))
}

License

MIT, see LICENSE file for details.