Skip to content

sec: replace math/rand v1 with math/rand/v2 for rate-limiter jitter (closes #419) - #856

Merged
cristim merged 2 commits into
feat/multicloud-web-frontendfrom
fix/419-wave16
Jun 9, 2026
Merged

sec: replace math/rand v1 with math/rand/v2 for rate-limiter jitter (closes #419)#856
cristim merged 2 commits into
feat/multicloud-web-frontendfrom
fix/419-wave16

Conversation

@cristim

@cristim cristim commented May 30, 2026

Copy link
Copy Markdown
Member

Summary

  • Replaces "math/rand" (v1) with "math/rand/v2" in providers/aws/recommendations/ratelimiter.go -- the only v1 call site found in providers/, internal/, and pkg/.
  • math/rand/v2 is already the project standard for non-crypto jitter (see pkg/retry/exponential.go with an explanatory comment); this aligns the rate-limiter with that convention.
  • The call site (rand.Float64()) is API-identical in v2; no logic changes.

Test plan

  • go build ./... passes
  • go test github.com/LeanerCloud/CUDly/providers/aws/recommendations -- all 281 tests pass
  • New TestWait_JitterRange: 200 iterations assert jitter stays within [0, 20%] of base delay and spans more than 5% (confirms the source is not stuck at zero)

Closes #419

Summary by CodeRabbit

  • Refactor

    • Updated rate limiter's random number generation and refactored jitter calculation logic for improved maintainability.
  • Tests

    • Added deterministic unit tests for rate limiter jitter computation to improve test reliability and coverage.

…419)

math/rand/v2 (Go 1.22+) is the project standard for non-crypto jitter
(already used in pkg/retry/exponential.go). Swaps the package-level
import in providers/aws/recommendations/ratelimiter.go; the call-site
(rand.Float64()) is identical in v2.

Adds TestWait_JitterRange: 200 iterations verify jitter stays within
[0, 20%] of the base delay and spans more than 5% (non-stuck source).
@cristim cristim added triaged Item has been triaged priority/p3 Polish / idea / may never ship severity/low Minor harm urgency/eventually No deadline impact/internal Team-internal only effort/xs Trivial / one-liner type/security Security finding labels May 30, 2026
@cristim

cristim commented May 30, 2026

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented May 30, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 04b65418-1a10-4d03-8e93-832aebcd9f9f

📥 Commits

Reviewing files that changed from the base of the PR and between 4956d66 and 840c5e5.

📒 Files selected for processing (2)
  • providers/aws/recommendations/ratelimiter.go
  • providers/aws/recommendations/ratelimiter_test.go

📝 Walkthrough

Walkthrough

This PR upgrades the rate limiter's jitter system from math/rand v1 to math/rand/v2 for API consistency and extracts the jitter logic into a pure, unit-testable function. The implementation is validated by a deterministic test covering 10,000 inputs.

Changes

Rate Limiter Jitter Upgrade

Layer / File(s) Summary
Jitter extraction and math/rand/v2 upgrade
providers/aws/recommendations/ratelimiter.go
Import math/rand/v2, extract inline jitter computation into a pure computeJitter(delay, r) helper that computes up to 20% of delay as jitter, and update Wait to call the helper with rand.Float64().
Deterministic jitter unit test
providers/aws/recommendations/ratelimiter_test.go
Add TestComputeJitter to verify the helper function directly across 10,000 evenly spaced uniform inputs, ensuring jitter bounds are honored and max spread reaches at least 95% of limit.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A limiter hops with jitter anew,
From rand v1 to v2 so true,
Pure function extracted, no sleeps in sight,
Ten thousand tests verify it's right!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title clearly and concisely describes the main change: replacing math/rand v1 with math/rand/v2 in the rate-limiter jitter implementation, which is the core focus of this changeset.
Linked Issues check ✅ Passed The PR fully addresses issue #419 by replacing math/rand v1 with math/rand/v2 in ratelimiter.go, updating jitter logic with extracted helper function, and adding deterministic unit tests that validate jitter bounds without wall-clock timing.
Out of Scope Changes check ✅ Passed All changes are narrowly scoped to address issue #419: import replacement, jitter refactoring with extracted helper, and deterministic test addition; no unrelated modifications are present.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/419-wave16

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

@coderabbitai

coderabbitai Bot commented May 30, 2026

Copy link
Copy Markdown
Contributor
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@cristim

cristim commented Jun 4, 2026

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jun 4, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

…k flakiness (refs #419)

Extract computeJitter(delay, r) as a pure function and replace the
wall-clock-based TestWait_JitterRange with TestComputeJitter, which
drives the function with 10,000 evenly-spaced r values in [0,1) and
asserts the output is within [0, 0.2*delay]. No sleeping means OS
scheduling overhead cannot blow the upper-bound check. The spread
assertion (max observed >= 95% of cap) still catches a regressed
always-zero jitter source.
@cristim

cristim commented Jun 5, 2026

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jun 5, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@cristim
cristim merged commit 0c10f1a into feat/multicloud-web-frontend Jun 9, 2026
4 checks passed
@cristim
cristim deleted the fix/419-wave16 branch July 27, 2026 11:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

effort/xs Trivial / one-liner impact/internal Team-internal only priority/p3 Polish / idea / may never ship severity/low Minor harm triaged Item has been triaged type/security Security finding urgency/eventually No deadline

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant