Skip to content

Refactor: Use generalized LRU cache for rate limiter cache. - #249

Merged
anoop2811 merged 5 commits into
kubevela:mainfrom
kash2104:issue-247
Aug 27, 2026
Merged

anoop2811 merged 5 commits into
kubevela:mainfrom
kash2104:issue-247

Conversation

@kash2104

@kash2104 kash2104 commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

Description of your changes

This PR replaces the lru.Cache with the lru cache that will be introduced in kubevela/pkg PR to use the centralized package instead of declaring as per the current status.

Fixes #247

I have:

  • Read and followed KubeVela's contribution process.
  • Related Docs updated properly. In a new feature or configuration option, an update to the documentation is necessary.
  • Run make reviewable to ensure this PR is ready for review.
  • Added backport release-x.y labels to auto-backport this PR if necessary.

How has this code been tested

Since the PR for the generic cache hasn't been merged yet, used workspaces to test this features.

  1. Create a go workspace
go work init
go work use <workspace-folder-name>
go work use <kubevela-folder-name>
go work use <pkg-folder-name>
  1. Build the project to ensure the workspace dependency is used.
go build ./...

Note: Avoid running go mod tidy in any repo, as it updates the module graph and throws error saying that package cache doesn't exist in kubevela/pkg

  1. Update the sync-crds target in the KubeVela dependency.mk to disable the workspace when downloading the pinned module version for CRDs.
sync-crds: ## Copy CRD from pinned module version in go.mod
	@moddir=$$(GOWORK=off go mod download -json $(PKG_MODULE) 2>/dev/null | grep '"Dir"' | cut -d'"' -f4); \
	mkdir -p config/crd/base; \
	for file in $(COMMON_CRD_FILES); do \
		src="$$moddir/crds/$$file"; \
		cp -f "$$src" "config/crd/base/"; \
	done

This ensures that the CRDs are copied from the module version pinned in go.mod rather than from the local workspace checkout.

  1. Build and install the updated KubeVela controller.
  2. Deploy a sample application:
vela up -f https://kubevela.io/example/applications/first-app.yaml
  1. Verify that the application is successfully reconciled and deployed, confirming that the local workspace version of github.com/kubevela/pkg and github.com/kubevela/workflow is being used and that the changes work as expected.

Special notes for your reviewer

This change depends on the generic LRU cache implementation introduced in the corresponding github.com/kubevela/pkg PR, which has not been merged yet. As a result, the cache package is not available in the published module version.


Summary by cubic

Switches the HTTP rate limiter cache to the shared github.com/kubevela/pkg/cache LRU, makes initialization explicit and context-aware, and adds per-request rate limit and timeout parameters to the request definition.

  • API: ratelimiter.NewRateLimiter now has signature NewRateLimiter(ctx context.Context, size int) (*RateLimiter, error). Callers must pass a context and handle the error.
  • Initialization: cmd/main.go initializes once via pkg/providers/http.InitRateLimiter(rootCtx) and pkg/providers/legacy/http.InitRateLimiter(rootCtx); providers also lazily initialize with sync.Once.
  • Request definition: request now accepts a ratelimiter with limit > 0 and a Go-duration period, plus a timeout; invalid values fail when the step runs.
  • Behavior: eviction remains LRU by capacity with no TTL; github.com/kubevela/pkg is bumped to the version providing the shared LRU store.

Written for commit 9d30ce5. Summary will update on new commits.

Review in cubic

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 7 files

Tip: instead of fixing issues one by one fix them all with cubic

Re-trigger cubic

Comment thread pkg/providers/legacy/http/http.go Resolved
Comment thread pkg/providers/http/http.go Resolved
@roguepikachu

Copy link
Copy Markdown
Collaborator

Early pass only, I know pkg #138 still has open comments so this will need another look once that API settles. Direction is fine, swapping the http rate limiter onto the shared LRU is the right first consumer. A few things I would not ship as-is, left them inline.

Comment thread cmd/main.go Resolved
Comment thread pkg/providers/http/http.go Resolved

@roguepikachu roguepikachu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Request changes

Verified locally on k3d with this branch wired to a local build of kubevela/pkg#138. Once rateLimiter actually reaches the HTTP provider, the LRU-backed limiter works (first request allowed, second blocked with request exceeds the rate limiter).

Please address the items below before merge. This PR should wait on kubevela/pkg#138.

Required

  1. Do not ignore the NewLRUStore error in NewRateLimiter.
  2. Use TTL 0 for limiter entries (or otherwise avoid a fixed 5m TTL that can drop an active limiter while the rate window still matters). Entry-count LRU with MaxSize is enough here.
  3. End-to-end usability: the stock request WorkflowStepDefinition does not pass rateLimiter into http.#HTTPDo, so callers cannot exercise this path today. Please wire parameter.rateLimiter through the request step template (in this repo if workflow ships the definition, or coordinate the same change on the kubevela request WSD). Without that, the Go change is effectively dead for WorkflowRuns.

Happy to re-review once these are in.

@kash2104

kash2104 commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor Author

@roguepikachu , Have addressed all the changes

  1. Changed the return type of NewRateLimiter to return error.
  2. Used TTL 0 for limiter entries. Now depends on the number of entries.
  3. Made changes in the kubevela repo for the request WSD since it depends on the request.cue present in kubevela repo
  4. Initialized the rateLimiter cache in kubevela similar to stepStatusCache.

Also cubic-dev comments have been addressed since they overlap with the reviews suggested.

@kash2104
kash2104 requested a review from roguepikachu August 25, 2026 08:28
@codecov

codecov Bot commented Aug 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.00000% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 69.85%. Comparing base (d7db9c4) to head (9d30ce5).
⚠️ Report is 24 commits behind head on main.

Files with missing lines Patch % Lines
cmd/main.go 0.00% 4 Missing ⚠️
...g/providers/legacy/http/ratelimiter/ratelimiter.go 83.33% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #249      +/-   ##
==========================================
+ Coverage   62.49%   69.85%   +7.36%     
==========================================
  Files          62       73      +11     
  Lines        4415     5845    +1430     
==========================================
+ Hits         2759     4083    +1324     
- Misses       1324     1370      +46     
- Partials      332      392      +60     
Flag Coverage Δ
e2etests 24.73% <80.76%> (?)
unit-test 64.27% <80.00%> (+1.78%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Signed-off-by: kash2104 <kparikh1104@gmail.com>
Signed-off-by: kash2104 <kparikh1104@gmail.com>
Signed-off-by: kash2104 <kparikh1104@gmail.com>
@roguepikachu

Copy link
Copy Markdown
Collaborator

@cubic-dev-ai review

@cubic-dev-ai

cubic-dev-ai Bot commented Aug 26, 2026

Copy link
Copy Markdown

@cubic-dev-ai review

@roguepikachu I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

No issues found across 9 files

Re-trigger cubic

@roguepikachu roguepikachu left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Request: wire ratelimiter through the stock request WSD

kubevela already landed this on the request step template (kubevela/kubevela#7326: vela-templates/definitions/internal/workflowstep/request.cue and the generated chart request.yaml).

This repo's chart definition still does not forward it:

charts/vela-workflow/templates/definitions/request.yaml

Today http.#HTTPDo already accepts request.ratelimiter (pkg/providers/http/http.cue), but the stock request step never passes parameter.ratelimiter into $params.request. So on a workflow-only install, WorkflowRuns cannot exercise the LRU rate limiter path this PR introduces.

Please mirror the kubevela change here:

  1. In the http.#HTTPDo request block, add:
if parameter.ratelimiter != _|_ {
	ratelimiter: parameter.ratelimiter
}
  1. Add to parameter:
ratelimiter?: {
	limit:  int & >0
	period: string
}

(Optionally keep the same duration regex / usage comments as kubevela for consistency.)

Without this, the Go/LRU migration works, but stock request steps remain unable to use ratelimiter when definitions come from this chart.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 3 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Fix all with cubic | Re-trigger cubic

Comment thread pkg/providers/http/http.cue Resolved
Signed-off-by: kash2104 <kparikh1104@gmail.com>
@kash2104

Copy link
Copy Markdown
Contributor Author

@roguepikachu Updated the request.yaml to include the ratelimiter in request WSD.

@roguepikachu

Copy link
Copy Markdown
Collaborator

@cubic-dev-ai review

@cubic-dev-ai

cubic-dev-ai Bot commented Aug 26, 2026

Copy link
Copy Markdown

@cubic-dev-ai review

@roguepikachu I have started the AI code review. It will take a few minutes to complete.

@roguepikachu roguepikachu left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Two small cleanups before I can approve:

  1. Remove dead pkg/providers/http/ratelimiter package

Both HTTP providers import pkg/providers/legacy/http/ratelimiter after this refactor, but pkg/providers/http/ratelimiter/ still ships the old groupcache/lru implementation (and its test). Nothing references it anymore.

Please delete that package and run go mod tidy so github.com/golang/groupcache drops out of go.mod if nothing else needs it.

  1. Restore multiline CUE in request.yaml

Commit c9bf22e collapsed the template into one escaped JSON string. Functionally fine, but this chart has historically used a readable multiline template: | block, and that is much easier to review and diff going forward. Please restore the multiline format while keeping the new timeout and ratelimiter fields.

Happy to approve once these are in @kash2104

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 12 files

Requires human review: Auto-approval blocked because this review re-detected 1 unresolved issue already reported by Cubic.

Re-trigger cubic

Signed-off-by: kash2104 <kparikh1104@gmail.com>
@kash2104

Copy link
Copy Markdown
Contributor Author

@roguepikachu Removed the unused ratelimiter and formatted the template in request.yaml.

@kash2104
kash2104 requested a review from roguepikachu August 27, 2026 05:49

@roguepikachu roguepikachu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM. Thanks for the follow-ups on the WSD wiring, the dead groupcache package cleanup, and restoring the multiline request template. Nice work getting this across the finish line.

@anoop2811

anoop2811 commented Aug 27, 2026 •

Copy link
Copy Markdown
Member

Thank you for your contribution! One suggestion for the future, once you have resolved a comment, please mark it as resolved in github

@anoop2811
anoop2811 merged commit 6b7aedc into kubevela:main Aug 27, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor: Use generalized LRU cache for HTTP provider rate limiter

3 participants