2014-05-15 15:21:37 +04:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type rateLimiter struct {
|
|
|
|
C chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRateLimiter(limit time.Duration, chanSize int) *rateLimiter {
|
|
|
|
rl := &rateLimiter{
|
|
|
|
C: make(chan struct{}, chanSize),
|
|
|
|
}
|
|
|
|
for i := 0; i < chanSize; i++ {
|
|
|
|
rl.C <- struct{}{}
|
|
|
|
}
|
|
|
|
go rl.loop(limit)
|
|
|
|
return rl
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rl *rateLimiter) loop(limit time.Duration) {
|
2014-09-04 11:22:44 +04:00
|
|
|
defer func() {
|
|
|
|
recover()
|
|
|
|
}()
|
2014-05-15 15:21:37 +04:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case rl.C <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
time.Sleep(limit)
|
|
|
|
}
|
|
|
|
}
|