Provides a simple implementation of requestlimits in a sliding time window.
go get github.com/wiggisser/slidingwindow
package main
import (
"github.com/wiggisser/slidingwindow"
"fmt"
"time"
}
func main() {
//allow 10 requests per 10 seconds
limit := slidingwindow.NewLimit(10, 10)
if limit.Check(10) {
fmt.Println("allowed")
} else {
fmt.Println("quota exceeded")
}
limit.Reset()
if limit.Check(10) {
fmt.Println("allowed")
} else {
fmt.Println("quota exceeded")
}
time.Sleep(10 * time.Second)
if limit.Check(10) {
fmt.Println("allowed")
} else {
fmt.Println("quota exceeded")
}
}
package main
import (
"github.com/wiggisser/slidingwindow"
"fmt"
"time"
}
func main() {
if e := slidingwindow.NewNamedLimit("limit1", 10, 10); e != nil {
fmt.Println(e)
} else {
fmt.Println("named limit 'limit1' created")
}
if b, e := slidingwindow.Check("limit1", 10); e != nil {
fmt.Println(e);
} else if b {
fmt.Println("allowed")
} else {
fmt.Println("quota exceeded")
}
}