Skip to content

Conversation

@Sharathvc23
Copy link

Summary

This PR adds two production-ready infrastructure utilities to nanda_core.utils:

Rate Limiter

  • Token bucket algorithm for DOS protection and request throttling
  • Per-client rate limiting for multi-tenant agent systems
  • Zero dependencies - uses only Python stdlib
  • Configurable via constructor or environment variables

Event Bus

  • Async pub/sub event dispatcher with pluggable transports
  • NATS connector for distributed agent coordination
  • Webhook connector for HTTP event delivery
  • In-memory subscribers via async iterators
  • Sync/async bridge for compatibility with both paradigms

Integration Points

Rate Limiter Use Cases

  • Protect A2A message handlers from message floods
  • Rate limit agent registration requests
  • Throttle MCP query requests per client

Event Bus Use Cases

  • Agent lifecycle events (registration, heartbeat, shutdown)
  • Telemetry and metrics collection
  • Distributed agent coordination via NATS
  • External monitoring via webhooks

Changes

New Files

  • nanda_core/utils/rate_limiter.py - Token bucket rate limiter (203 lines)
  • nanda_core/utils/event_bus.py - Async event bus (344 lines)
  • nanda_core/utils/README.md - Comprehensive documentation with NEST-specific examples

Modified Files

  • nanda_core/utils/__init__.py - Exports new utilities
  • setup.py - Added httpx dependency, optional nats-py support

Usage Examples

Rate Limiter

from nanda_core.utils import RateLimiter, RateLimitError

limiter = RateLimiter(rate_per_sec=50.0, burst=100.0)

try:
    limiter.check(client_id="user-123")
    # Process request
except RateLimitError:
    # Return 429 Too Many Requests

Event Bus

from nanda_core.utils import event_bus

# Publish event
await event_bus.publish({
    "type": "agent.registered",
    "agent_id": "compliance-advisor"
})

# Subscribe to events
async for event in event_bus.subscribe():
    print(f"Event: {event['type']}")

Dependencies

  • Added: httpx (required for webhook connector)
  • Optional: nats-py (via pip install nest[events])

Both httpx and nats-py are mature, well-maintained libraries used extensively in async Python applications.

Testing

Both utilities include working example code that can be run:

python -m nanda_core.utils.rate_limiter
python -m nanda_core.utils.event_bus

Documentation

Comprehensive README included with:

  • API reference for both utilities
  • Configuration options
  • NEST-specific integration examples
  • Use cases and patterns
  • Testing examples

Compatibility

  • Python: 3.8+ (matches NEST requirement)
  • Async: Compatible with NEST's existing async patterns
  • No conflicts: No existing rate limiting or event bus in NEST

Contributor

Astrocity Foundation - Space Industry Standards Initiative

  • License: MIT
  • Based on standard algorithms (token bucket, pub/sub pattern)

🤖 Generated with Claude Code

This contribution adds two generic infrastructure utilities to NANDA's
nanda_core.utils module:

## Rate Limiter
- Token bucket algorithm for DOS protection
- Per-client rate limiting for multi-tenant systems
- Configurable via constructor or environment variables
- Zero external dependencies (stdlib only)
- Use case: Protect A2A message handling from floods

## Event Bus
- Async pub/sub event dispatcher
- Pluggable transports: NATS, webhooks, custom connectors
- In-memory subscribers via async iterators
- Sync/async bridge for non-async code
- Auto-configuration from environment variables
- Use cases: Agent lifecycle events, telemetry, distributed coordination

## Integration
- Added to nanda_core.utils with proper exports
- Updated setup.py to include httpx dependency
- Added optional NATS support via pip install nest[events]
- Comprehensive documentation with NEST-specific examples

## Dependencies
- httpx (added to setup.py for webhook support)
- nats-py (optional, for NATS connector)

Contributor: Astrocity Foundation - Space Industry Standards Initiative
License: MIT

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
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.

1 participant