Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] - Fix race condition #2656

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions pkg/sources/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -849,14 +849,11 @@ func (s *Source) handleRateLimit(errIn error) bool {
return false
}

rateLimitMu.RLock()
resumeTime := rateLimitResumeTime
rateLimitMu.RUnlock()

var retryAfter time.Duration
if resumeTime.IsZero() || time.Now().After(resumeTime) {
rateLimitMu.Lock()
rateLimitMu.Lock()
defer rateLimitMu.Unlock()

if rateLimitResumeTime.IsZero() || time.Now().After(rateLimitResumeTime) {
var (
now = time.Now()

Expand All @@ -865,23 +862,23 @@ func (s *Source) handleRateLimit(errIn error) bool {
rateLimit *github.RateLimitError
abuseLimit *github.AbuseRateLimitError
)
if errors.As(errIn, &rateLimit) {
switch {
case errors.As(errIn, &rateLimit):
limitType = "primary"
rate := rateLimit.Rate
if rate.Remaining == 0 { // TODO: Will we ever receive a |RateLimitError| when remaining > 0?
retryAfter = rate.Reset.Sub(now)
}
} else if errors.As(errIn, &abuseLimit) {
case errors.As(errIn, &abuseLimit):
limitType = "secondary"
retryAfter = abuseLimit.GetRetryAfter()
} else {
rateLimitMu.Unlock()
default:
return false
}

jitter := time.Duration(rand.Intn(10)+1) * time.Second
if retryAfter > 0 {
retryAfter = retryAfter + jitter
retryAfter += jitter
rateLimitResumeTime = now.Add(retryAfter)
s.log.V(0).Info(fmt.Sprintf("exceeded %s rate limit", limitType), "retry_after", retryAfter.String(), "resume_time", rateLimitResumeTime.Format(time.RFC3339))
} else {
Expand All @@ -890,10 +887,8 @@ func (s *Source) handleRateLimit(errIn error) bool {
// TODO: Use exponential backoff instead of static retry time.
s.log.V(0).Error(errIn, "unexpected rate limit error", "retry_after", retryAfter.String(), "resume_time", rateLimitResumeTime.Format(time.RFC3339))
}

rateLimitMu.Unlock()
} else {
retryAfter = time.Until(resumeTime)
retryAfter = time.Until(rateLimitResumeTime)
}

githubNumRateLimitEncountered.WithLabelValues(s.name).Inc()
Expand Down
Loading