ripple-api/app/rate_limiter.go
2017-02-02 11:56:32 +01:00

37 lines
565 B
Go

package app
import (
"strconv"
"time"
"zxq.co/ripple/rippleapi/limit"
)
const reqsPerSecond = 5000
const sleepTime = time.Second / reqsPerSecond
var limiter = make(chan struct{}, reqsPerSecond)
func setUpLimiter() {
for i := 0; i < reqsPerSecond; i++ {
limiter <- struct{}{}
}
go func() {
for {
limiter <- struct{}{}
time.Sleep(sleepTime)
}
}()
}
func rateLimiter() {
<-limiter
}
func perUserRequestLimiter(uid int, ip string) {
if uid == 0 {
limit.Request("ip:"+ip, 200)
} else {
limit.Request("user:"+strconv.Itoa(uid), 3000)
}
}