2016-07-06 14:33:58 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"time"
|
2016-09-06 15:04:22 +00:00
|
|
|
|
|
|
|
"git.zxq.co/ripple/rippleapi/limit"
|
2016-07-06 14:33:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
2016-09-06 15:04:22 +00:00
|
|
|
limit.Request("ip:"+ip, 60)
|
2016-07-06 14:33:58 +00:00
|
|
|
} else {
|
2016-09-06 15:04:22 +00:00
|
|
|
limit.Request("user:"+strconv.Itoa(uid), 2000)
|
2016-07-06 14:33:58 +00:00
|
|
|
}
|
|
|
|
}
|