Repository navigation
fix: add per-address rate limiting and reject-when-busy for gen_call #1443
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -911,6 +911,39 @@ async def health_database() -> Dict[str, Any]: | |||||
| return {"status": "error", "error": str(e)} | ||||||
|
|
||||||
|
|
||||||
| @health_router.get("/health/ratelimit") | ||||||
| async def health_ratelimit() -> Dict[str, Any]: | ||||||
| """Show per-address gen_call rate limit state.""" | ||||||
| from backend.protocol_rpc.endpoints import ( | ||||||
| _address_request_log, | ||||||
| _RATE_LIMIT_WINDOW, | ||||||
| _RATE_LIMIT_MAX, | ||||||
| _genvm_semaphore, | ||||||
| _GENVM_CONCURRENCY, | ||||||
| ) | ||||||
| import time as _time | ||||||
|
|
||||||
| now = _time.monotonic() | ||||||
| cutoff = now - _RATE_LIMIT_WINDOW | ||||||
| addresses = {} | ||||||
| for addr, timestamps in _address_request_log.items(): | ||||||
| recent = [t for t in timestamps if t > cutoff] | ||||||
| if recent: | ||||||
| addresses[addr] = { | ||||||
| "requests_in_window": len(recent), | ||||||
| "limit": _RATE_LIMIT_MAX, | ||||||
| "oldest_in_window_age_s": round(now - min(recent), 1), | ||||||
| } | ||||||
| return { | ||||||
| "window_seconds": _RATE_LIMIT_WINDOW, | ||||||
| "max_per_window": _RATE_LIMIT_MAX, | ||||||
| "genvm_concurrency_limit": _GENVM_CONCURRENCY, | ||||||
| "genvm_semaphore_available": _genvm_semaphore._value, # noqa: SLF001 | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🌐 Web query:
💡 Result:
If you need supported behavior, rely on Sources: [1] [2] 🏁 Script executed: # Check linting configuration to see if SLF001 is enabled
find . -name "*.toml" -o -name "*.cfg" -o -name ".ruff*" | head -20Repository: genlayerlabs/genlayer-studio Length of output: 83 🏁 Script executed: # Examine the actual file and surrounding context
head -950 backend/protocol_rpc/health.py | tail -50Repository: genlayerlabs/genlayer-studio Length of output: 1753 🏁 Script executed: # Check imports at the top of the file
head -30 backend/protocol_rpc/health.pyRepository: genlayerlabs/genlayer-studio Length of output: 1088 🏁 Script executed: # Check ruff.toml to see if SLF001 is enabled or disabled
cat ruff.tomlRepository: genlayerlabs/genlayer-studio Length of output: 978 Remove unused The Fix: remove unused noqa- "genvm_semaphore_available": _genvm_semaphore._value, # noqa: SLF001
+ "genvm_semaphore_available": _genvm_semaphore._value,For a more robust solution, consider computing availability from the concurrency limit minus an in-use counter, or exposing it from the semaphore wrapper. 📝 Committable suggestion
Suggested change
🧰 Tools🪛 Ruff (0.15.0)[warning] 941-941: Unused Remove unused (RUF100) 🤖 Prompt for AI Agents |
||||||
| "active_addresses": len(addresses), | ||||||
| "addresses": addresses, | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| @health_router.get("/health/memory") | ||||||
| async def health_memory() -> Dict[str, Any]: | ||||||
| """Show detailed memory usage statistics.""" | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_address_request_loggrows without bound for inactive addresses.Entries are only pruned when the same address is checked again. Addresses that go idle leave stale entries in the dict forever. Over time in production (many distinct contract addresses), this is a slow memory leak.
Consider periodically evicting addresses whose timestamps have all expired — for example, by cleaning during each
_check_rate_limitcall (amortised), or via a background task.♻️ Lightweight amortised cleanup sketch
🤖 Prompt for AI Agents