closedhumanPR #109Registry

hackathon/tanisha: implemented a custom Rate Limiter layer and reference plugins

Implementation of a custom Rate Limiting Layer (introducing the rate_limit layer, TokenBucketRateLimiter, and LeakyBucketRateLimiter reference plugins) to enforce traffic and request boundaries per agent.

Author

tanishaacodes avatar

@tanishaacodes

github profile →
Status
Closed without merging
Opened on
Jul 8
Branch
hackathon/tani-rate-limiter

Description

The pitch.

# Summary
Implementation of a custom **Rate Limiting Layer** (introducing the `rate_limit` layer, `TokenBucketRateLimiter`, and `LeakyBucketRateLimiter` reference plugins) to enforce traffic and request boundaries per agent.

All changes are fully verified, formatted, typechecked, and pass the local CI checklist.

---

## 1. File Changes & Architecture

### Layer Definition
* `packages/nest-core/nest_core/layers/rate_limiter.py`:
  - Defines the `RateLimiter` Protocol interface.
  - Exposes `consume(client, *, now, tokens)` and `allowance(client, *, now)`. All calculations take logical simulation time `now` as a keyword-only argument to ensure determinism.
* `packages/nest-core/nest_core/layers/__init__.py`:
  - Imports and exports `RateLimiter`.

### Reference Plugins
* `packages/nest-plugins-reference/nest_plugins_reference/rate_limit/token_bucket.py`:
  - Class `TokenBucketRateLimiter` refilling tokens linearly at a constant rate up to a max capacity limit.
* `packages/nest-plugins-reference/nest_plugins_reference/rate_limit/leaky_bucket.py`:
  - Class `LeakyBucketRateLimiter` modeling a queue/water level that leaks constant capacity over logical time.
* `packages/nest-plugins-reference/nest_plugins_reference/rate_limit/__init__.py`:
  - Exports both plugins.
* `packages/nest-core/nest_core/plugins.py`:
  - Registers the custom plugins in the built-in registry (`("rate_limit", "token_bucket")` and `("rate_limit", "leaky_bucket")`).

### Unit Tests
* `packages/nest-plugins-reference/tests/test_rate_limiter.py`:
  - Unit tests verifying token replenishment, capacity limits, drop behavior, and independent client rate isolation.

---

## 2. Verification

Run local checks:
```bash
# Run rate limiter unit tests
uv run pytest packages/nest-plugins-reference/tests/test_rate_limiter.py -v

# Run full project checks
make ci-local

Try it

Open PR on GitHubView diff

Checkout locally

git fetch origin pull/109/head:pr-109
git checkout pr-109