diff --git a/pkg/ratelimit/ratelimit.go b/pkg/ratelimit/ratelimit.go index d32fad50..6f0a353d 100644 --- a/pkg/ratelimit/ratelimit.go +++ b/pkg/ratelimit/ratelimit.go @@ -3,8 +3,6 @@ package ratelimit import ( "context" "time" - - log "github.com/sirupsen/logrus" ) // Limiter .. @@ -14,13 +12,5 @@ type Limiter interface { // Take .. func Take(ctx context.Context, l Limiter) { - throttled := l.Take(ctx) - - if throttled.Milliseconds() > 10 { - log.WithFields( - log.Fields{ - "for": throttled.String(), - }, - ).Debug("throttled GitLab requests") - } + l.Take(ctx) } diff --git a/pkg/ratelimit/redis.go b/pkg/ratelimit/redis.go index 6ba4cca7..8544e60d 100644 --- a/pkg/ratelimit/redis.go +++ b/pkg/ratelimit/redis.go @@ -29,14 +29,25 @@ func NewRedisLimiter(redisClient *redis.Client, maxRPS int) Limiter { func (r Redis) Take(ctx context.Context) time.Duration { start := time.Now() - res, err := r.Allow(ctx, redisKey, redis_rate.PerSecond(r.MaxRPS)) - if err != nil { - log.WithContext(ctx). - WithError(err). - Fatal() + for { + res, err := r.Allow(ctx, redisKey, redis_rate.PerSecond(r.MaxRPS)) + if err != nil { + log.WithContext(ctx). + WithError(err). + Fatal() + } + + if res.Allowed > 0 { + break + } else { + log.WithFields( + log.Fields{ + "for": res.RetryAfter.String(), + }, + ).Debug("throttled GitLab requests") + time.Sleep(res.RetryAfter) + } } - time.Sleep(res.RetryAfter) - return start.Sub(time.Now()) }