mirror of
https://github.com/alice-lg/birdwatcher.git
synced 2025-03-09 00:00:05 +01:00
added experimental rate limit
This commit is contained in:
parent
2bd0e33a32
commit
f680b6e842
2 changed files with 28 additions and 0 deletions
27
bird/bird.go
27
bird/bird.go
|
@ -9,6 +9,9 @@ import (
|
|||
|
||||
var BirdCmd string
|
||||
|
||||
var rateLimit = 0
|
||||
var MAX_RATE = 5
|
||||
|
||||
var Cache = struct {
|
||||
sync.RWMutex
|
||||
m map[string]Parsed
|
||||
|
@ -34,11 +37,35 @@ func Run(args string) ([]byte, error) {
|
|||
return exec.Command(BirdCmd, argsList...).Output()
|
||||
}
|
||||
|
||||
func InstallRateLimitReset() {
|
||||
go func() {
|
||||
c := time.Tick(time.Second)
|
||||
|
||||
for _ = range c {
|
||||
rateLimit = 0
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func checkRateLimit() bool {
|
||||
if rateLimit > MAX_RATE {
|
||||
return false
|
||||
}
|
||||
|
||||
rateLimit += 1
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func RunAndParse(cmd string, parser func([]byte) Parsed) (Parsed, bool) {
|
||||
if val, ok := fromCache(cmd); ok {
|
||||
return val, true
|
||||
}
|
||||
|
||||
if !checkRateLimit() {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
out, err := Run(cmd)
|
||||
|
||||
if err != nil {
|
||||
|
|
|
@ -38,6 +38,7 @@ func main() {
|
|||
flag.Parse()
|
||||
|
||||
bird.BirdCmd = *birdc
|
||||
bird.InstallRateLimitReset()
|
||||
|
||||
r := makeRouter()
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue