Skip to content

feat: add Durable Objects pattern for real-time state management #21

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

talkstream
Copy link
Owner

Summary

  • Implement base Durable Object with storage abstraction for platform-agnostic usage
  • Add WebSocket room for real-time collaboration features
  • Create distributed counter and named counters with atomic operations
  • Implement rate limiter with multiple algorithms (fixed window, sliding window, token bucket, leaky bucket)

Implementation Details

Base Durable Object (BaseDurableObject)

  • Provides common functionality and state management
  • Includes helper methods for storage operations (get, put, delete, list)
  • Implements response helpers (json, error, success)
  • Includes SimpleState for testing outside Cloudflare environment

WebSocket Room (WebSocketRoom)

  • Real-time communication capabilities
  • User management with metadata
  • Message history and broadcasting
  • Auto-cleanup on disconnect

Distributed Counters

  • Counter - Single atomic counter
  • NamedCounters - Multiple counters in one object
  • HTTP API for increment/decrement/reset operations

Rate Limiter

  • Fixed window rate limiting (basic)
  • Sliding window algorithm
  • Token bucket algorithm
  • Leaky bucket algorithm
  • Accurate distributed rate limiting

Test Coverage

  • Comprehensive tests for base class
  • Counter operations tests
  • Storage persistence tests
  • Transaction support tests

Documentation

  • Detailed guide in docs/DURABLE_OBJECTS.md
  • Working example in examples/durable-objects-example.ts
  • README updated with Durable Objects section

Use Cases

This enables building:

  • Chat rooms and collaborative editing
  • Real-time game servers
  • Distributed systems with strong consistency
  • Global rate limiting
  • Event aggregation
  • Leader election patterns

Performance Benefits

  • Edge location execution for low latency
  • Strong consistency guarantees
  • Automatic scaling and hibernation
  • Single-threaded JavaScript execution
  • Transactional storage operations

Test Plan

  • Run all tests locally
  • Verify TypeScript strict mode compliance
  • Check ESLint passes with no errors
  • Deploy example to Cloudflare Workers
  • Test WebSocket connections
  • Verify rate limiting accuracy
  • Test counter persistence

- Added contribution review checklist for maintainers
- Created successful contributions gallery with examples
- Enhanced contribute.ts with PR conflict detection
- Added GitHub Action for automated PR validation
- Created auto-labeling configuration for PRs
- Updated CONTRIBUTING.md with links to new resources

This improves the contribution workflow by:
1. Providing clear review criteria
2. Showcasing successful contributions
3. Preventing PR conflicts early
4. Automating validation checks
5. Auto-labeling PRs for better organization

Based on experience processing contributions from the community.
- Implement base Durable Object with storage abstraction
- Add WebSocket room for real-time collaboration
- Create distributed counter and named counters
- Implement rate limiter with multiple algorithms (fixed window, sliding window, token bucket, leaky bucket)
- Add comprehensive tests for base class and counter
- Create detailed documentation and working example
- Update README with Durable Objects section

This enables building real-time features like chat rooms, collaborative editing,
game servers, and distributed systems with strong consistency guarantees.
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