Skip to content

Conversation

@mdelapenya
Copy link
Member

What does this PR do?

Moves the delay before any call, instead of after the call. It also sets a bigger backoff intervals.

Why is it important?

Have a more resilient build

Related issues

@mdelapenya mdelapenya requested a review from a team as a code owner November 25, 2025 11:54
@mdelapenya mdelapenya added chore Changes that do not impact the existing functionality github_actions Pull requests that update GitHub Actions code labels Nov 25, 2025
@mdelapenya mdelapenya self-assigned this Nov 25, 2025
@mdelapenya mdelapenya added chore Changes that do not impact the existing functionality github_actions Pull requests that update GitHub Actions code labels Nov 25, 2025
@netlify
Copy link

netlify bot commented Nov 25, 2025

Deploy Preview for testcontainers-go ready!

Name Link
🔨 Latest commit eb68dc9
🔍 Latest deploy log https://app.netlify.com/projects/testcontainers-go/deploys/692598e759d41e000826d9d0
😎 Deploy Preview https://deploy-preview-3500--testcontainers-go.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@coderabbitai
Copy link

coderabbitai bot commented Nov 25, 2025

Summary by CodeRabbit

  • Chores
    • Optimized metrics collection timing and retry logic to improve reliability and reduce server load during queries.

✏️ Tip: You can customize this high-level summary in your review settings.

Walkthrough

The PR modifies the usage metrics collection timing and retry logic. It introduces a 7-second pre-query delay when metrics are already collected, removes the 2-second per-request delay, and reworks the retry backoff strategy to use uniform 60-second intervals instead of the previous exponential ramp.

Changes

Cohort / File(s) Summary
Retry and Query Timing Adjustments
usage-metrics/collect-metrics.go
Modified inter-request delays and retry backoff sequence: introduces 7-second pre-query delay, removes 2-second per-request delay, replaces exponential backoff (5s, 10s, 20s, 40s, 60s) with fixed 60-second backoff intervals (3× 60s), resulting in longer wait times between retries.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Collector as collect-metrics
    participant GitHub as GitHub API

    rect rgb(220, 240, 255)
        note over Collector: Metrics already collected
        Collector->>Collector: Wait 7 seconds
    end

    rect rgb(200, 255, 200)
        note over Client,Collector: Query sequence
        loop Until success or max retries
            Collector->>GitHub: Query usage metrics
            alt Success
                GitHub-->>Collector: Return metrics
                Collector->>Client: Process metrics
            else Failure (retry needed)
                Collector->>Collector: Wait 60 seconds
            end
        end
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Timing logic changes: Verify the 7-second and 60-second intervals are intentional and aligned with GitHub API rate limits and system requirements.
  • Retry behavior impact: Confirm that the uniform 60-second backoff doesn't cause excessive delays or API throttling issues compared to the previous exponential ramp.
  • Removal of 2-second delay: Ensure that eliminating the per-request delay doesn't increase request volume unexpectedly.

Possibly related PRs

Poem

🐰 Seven seconds, then we leap,
No more delays in every sweep,
Sixty seconds, steady beat,
Retries keeping rhythm neat,
Metrics flowing, timings sweet!

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The PR title 'chore(metrics): even better rate limit strategy' accurately describes the main change: improving the rate limit handling strategy for metrics collection with better timing and backoff intervals.
Description check ✅ Passed The PR description is directly related to the changeset, explaining what was moved (delay timing), why (more resilient build), and linking to related issue #3499.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
usage-metrics/collect-metrics.go (1)

108-112: Consider differentiating backoff strategies for rate-limit vs. other errors.

The uniform 60-second backoff is well-suited for rate limit errors, as it allows the rolling window to reset. However, for non-rate-limit errors (detected at lines 141-142), waiting 60 seconds might be excessive. These could be transient network issues, temporary GitHub API hiccups, or other recoverable errors that might succeed sooner.

While the current approach is simple and conservative (aligning with the PR's goal of resilience), you might consider implementing a shorter backoff sequence for non-rate-limit errors to reduce total execution time when encountering transient failures.

Example approach:

 func queryGitHubUsageWithRetry(version string) (int, error) {
 	var lastErr error
-	// Backoff intervals: wait longer for rate limit to reset (rolling window)
-	backoffIntervals := []time.Duration{
+	// Backoff intervals for rate limit errors: wait for rolling window to reset
+	rateLimitBackoffs := []time.Duration{
 		60 * time.Second, // Wait for rolling window
 		60 * time.Second,
 		60 * time.Second,
 	}
+	// Shorter backoff for other transient errors
+	otherBackoffs := []time.Duration{
+		10 * time.Second,
+		20 * time.Second,
+		30 * time.Second,
+	}
 
-	// maxRetries includes the initial attempt plus one retry per backoff interval
-	maxRetries := len(backoffIntervals) + 1
+	maxRetries := len(rateLimitBackoffs) + 1
 
 	for attempt := 0; attempt < maxRetries; attempt++ {
 		if attempt > 0 {
-			// Use predefined backoff intervals
-			waitTime := backoffIntervals[attempt-1]
+			// Determine which backoff to use based on last error
+			var waitTime time.Duration
+			if strings.Contains(lastErr.Error(), "rate limit") ||
+				strings.Contains(lastErr.Error(), "403") ||
+				strings.Contains(lastErr.Error(), "429") {
+				waitTime = rateLimitBackoffs[attempt-1]
+			} else {
+				waitTime = otherBackoffs[attempt-1]
+			}
 			log.Printf("Retrying version %s in %v (attempt %d/%d)", version, waitTime, attempt+1, maxRetries)
 			time.Sleep(waitTime)
 		}
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a735911 and eb68dc9.

📒 Files selected for processing (1)
  • usage-metrics/collect-metrics.go (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
usage-metrics/collect-metrics.go (1)
log/logger.go (1)
  • Printf (47-49)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: lint (usage-metrics) / lint: usage-metrics
  • GitHub Check: Analyze (go)
🔇 Additional comments (1)
usage-metrics/collect-metrics.go (1)

67-71: Rate limit assumption verified as correct.

Authenticated requests to the GitHub Code Search API are limited to 10 requests per minute, confirming the math in the comment (10 requests per 60 seconds = 6 seconds minimum). The implementation using 7 seconds is appropriately conservative and correct.

@mdelapenya mdelapenya merged commit 0c412ea into testcontainers:main Nov 25, 2025
17 checks passed
@mdelapenya mdelapenya deleted the fix-metrics-rate-limits branch November 25, 2025 12:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

chore Changes that do not impact the existing functionality github_actions Pull requests that update GitHub Actions code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant