Skip to content

fix(commands): add sliding-window rate limiting to system-modifying commands#51

Open
anshul23102 wants to merge 1 commit into
Kanwaljeet1:mainfrom
anshul23102:fix/48-rate-limit-commands
Open

fix(commands): add sliding-window rate limiting to system-modifying commands#51
anshul23102 wants to merge 1 commit into
Kanwaljeet1:mainfrom
anshul23102:fix/48-rate-limit-commands

Conversation

@anshul23102
Copy link
Copy Markdown
Contributor

Description

kill_process, apply_optimization, rollback_optimization, and apply_boot_optimization could be called as fast as a caller could issue them. A scripted flood could kill processes in a tight loop or repeatedly trigger optimizations, driving the system into an unstable state.

Related Issue

Closes #48

Root Cause

No throttling mechanism existed for any Tauri command. Every request was processed immediately.

Changes Made

File Change
src-tauri/src/system/rate_limiter.rs New RateLimiter type. Tracks call timestamps per command in a trailing window using Instant (monotonic, so unaffected by clock changes). Rejected calls do not record a timestamp.
src-tauri/src/system/mod.rs Registered and re-exported the new module
src-tauri/src/lib.rs Added rate_limiter to AppState and an enforce_rate_limit helper. kill_process is capped at one call per second; the optimization commands at five per minute. Each command checks the limiter before acquiring any other lock.

Testing Done

cargo test --lib rate_limiter::
running 5 tests
test ...::allows_calls_up_to_the_limit ... ok
test ...::rejects_calls_beyond_the_limit ... ok
test ...::tracks_each_command_independently ... ok
test ...::window_expiry_allows_new_calls ... ok
test ...::rejected_call_does_not_consume_a_slot ... ok
test result: ok. 5 passed

Checklist

  • Builds cleanly with cargo build
  • All new unit tests pass
  • No merge conflicts with the base branch
  • Changes are focused on the reported surface

@Kanwaljeet1
Copy link
Copy Markdown
Owner

Hi @anshul23102 , please resolve the conflicts.

…ommands

kill_process, apply_optimization, rollback_optimization, and
apply_boot_optimization could be called as fast as a caller could issue
them. A scripted flood of these commands could kill processes in a tight
loop or repeatedly trigger optimizations, driving the system into an
unstable state.

Added a RateLimiter that tracks call timestamps per command in a trailing
window using a monotonic clock, so it is unaffected by wall-clock
adjustments. kill_process is capped at one call per second; the
optimization commands are capped at five calls per minute. A rejected call
does not record a timestamp, so it cannot extend the window. Each command
checks the limiter before acquiring any other lock or modifying state.

Added unit tests covering the per-command limit, independent tracking
across commands, window expiry, and that rejected calls do not consume a
slot.

Closes Kanwaljeet1#48
@anshul23102 anshul23102 force-pushed the fix/48-rate-limit-commands branch from e052567 to 661342a Compare June 4, 2026 18:01
@anshul23102
Copy link
Copy Markdown
Contributor Author

Conflicts resolved. The rate limiting checks are now properly sequenced after validation (fail fast on invalid ID, then check rate limits). Both PR #50 and this PR complement each other: #50 validates the optimization_id, and this PR adds rate limiting. Force-pushed the rebased solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request Level:2 NSoC'26

Projects

None yet

Development

Successfully merging this pull request may close these issues.

No rate limiting on system-modifying commands allows rapid repeated calls causing resource exhaustion

2 participants