Examples¶
Practical examples and code snippets demonstrating common Pyvider RPC Plugin patterns and use cases.
๐ค AI-Generated Content
This documentation was generated with AI assistance and is still being audited. Some, or potentially a lot, of this information may be inaccurate. Learn more.
Quick Reference¶
| Example | Description | Complexity | Lines |
|---|---|---|---|
| Quick Start | Focused code samples for specific features | ๐ข Beginner | ~20-30 each |
| Echo Service | Complete RPC service from basic to production | ๐ข-๐ก Beginner to Advanced | ~570 |
Getting Started¶
New to pyvider-rpcplugin?¶
Start here to learn the fundamentals:
- Quick Start Examples - Six focused examples covering:
- Basic client and server setup
- Health checks and rate limiting
- TCP transport configuration
-
Custom protocol implementation
-
Echo Service Example - Complete service demonstrating:
- Unary RPC patterns
- Streaming (server, client, bidirectional)
- Error handling and retry logic
- Production features (health, rate limiting, metrics, security)
Learning Paths¶
Goal: Understand core plugin lifecycle and basic patterns
- Quick Start: Basic Server - Minimal server (15 lines)
- Quick Start: Basic Client - Minimal client (20 lines)
- Echo Service: Basic Setup - Complete RPC service
- Quick Start: Health Checks - Production monitoring
- Quick Start: Rate Limiting - Service protection
Next: Explore streaming patterns in the Echo Service example
Goal: Master streaming, transports, and custom protocols
- Echo Service: Streaming Patterns - Server/client/bidirectional streaming
- Quick Start: TCP Transport - Cross-platform communication
- Quick Start: Custom Protocol - Integrate your gRPC services
- Echo Service: Error Handling - Robust error management
Next: Study production deployment in the Configuration Guide
Goal: Build production-ready, observable, secure services
- Echo Service: Production Features - Health, rate limiting, metrics, mTLS
- Echo Service: Testing - Unit and integration test patterns
- Advanced Topics Guide - Observability, performance tuning
- Security Guide - Comprehensive mTLS setup
Next: Implement custom middleware and observability
Example Structure¶
Quick Start Examples¶
Six focused examples (15-30 lines each) demonstrating specific features:
# Basic Server (15 lines)
async def main():
protocol = plugin_protocol()
handler = object()
server = plugin_server(protocol=protocol, handler=handler)
await server.serve()
See Quick Start Examples for all patterns.
Echo Service Example¶
Comprehensive example (576 lines) covering:
- Basic Setup - Unary RPC with client/server implementation
- Streaming Patterns - Server, client, and bidirectional streaming
- Error Handling - Validation, retry, graceful degradation
- Production Features - Health, rate limiting, metrics, mTLS, testing
See Echo Service Example for complete details.
Running Examples¶
Installation¶
# Clone the repository
git clone https://github.com/provide-io/pyvider-rpcplugin.git
cd pyvider-rpcplugin
# Install dependencies
uv sync
Quick Start Examples¶
# Basic client (launches basic_server.py automatically)
python examples/short/basic_client.py
# Health check server
python examples/short/health_check.py
# Rate limited server
python examples/short/rate_limiting.py
# TCP transport server
python examples/short/tcp_transport.py
# Custom protocol server
python examples/short/custom_protocol.py
Echo Service Example¶
Expected output:
2025-01-15 10:30:45.123 [info ] Client will use server script: .../examples/echo_server.py
2025-01-15 10:30:45.200 [info ] Starting Echo Plugin Server...
2025-01-15 10:30:45.201 [info ] EchoService handler registered with gRPC server
2025-01-15 10:30:45.250 [info ] Client started and connected successfully
2025-01-15 10:30:45.251 [info ] Sending Echo request to server: 'Hello from pyvider client!'
Example Files Reference¶
Documentation vs. Actual Code¶
Simplified Examples vs. Runnable Files
Documentation examples are simplified for teaching. Actual files in examples/ include:
example_utils.configure_for_example()for environment setup- Comprehensive error handling and logging
- Production-ready patterns and best practices
Here's the mapping:
| Documentation | Actual File | Notes |
|---|---|---|
| Basic plugin | dummy_server.py |
Minimal server with BasicRPCPluginProtocol |
| Basic client | quick_start_client.py |
Client that launches dummy_server.py |
| Echo server | echo_server.py โ |
Matches documentation (production patterns) |
| Echo client | echo_client.py โ |
Matches documentation (class-based) |
Available Files¶
examples/
โโโ short/ # Quick start examples (15-30 lines)
โ โโโ basic_client.py # Minimal client connection
โ โโโ basic_server.py # Minimal server setup
โ โโโ health_check.py # Health check implementation
โ โโโ rate_limiting.py # Rate limiting example
โ โโโ tcp_transport.py # TCP transport configuration
โ โโโ custom_protocol.py # Custom protocol example
โโโ echo_server.py # Echo service server (comprehensive)
โโโ echo_client.py # Echo service client (production-ready)
โโโ quick_start_client.py # Basic client launching dummy_server.py
โโโ dummy_server.py # Minimal plugin server
โโโ proto/ # Protocol Buffer definitions
โ โโโ echo.proto # Echo service definition
โโโ example_utils.py # Shared utilities for examples
โโโ run_all_examples.py # Script to run all examples
See the repository for additional advanced examples.
Common Issues¶
Import Errors¶
If you get "ModuleNotFoundError":
- Ensure you're running from the project root directory
- Run uv sync to install dependencies
- The example_utils.configure_for_example() call should handle path setup
Connection Issues¶
If client can't connect to server:
- Check server logs for errors (logs go to stderr)
- Verify no other process is using the socket/port
- Try increasing timeout: await asyncio.wait_for(client.start(), timeout=30.0)
Transport Issues¶
If Unix sockets or TCP transport fails:
- Unix sockets: Not available on Windows - use TCP instead
- TCP: Check firewall allows the port
- Port conflicts: Try different port or use port=0 for automatic assignment
- Verify port availability: lsof -i :50051
Next Steps¶
Explore Guides¶
- Server Guide - Server-side patterns and optimization
- Client Guide - Client-side patterns and error handling
- Configuration Guide - Environment-driven configuration
- Security Guide - Comprehensive mTLS setup
- Advanced Topics - Observability, performance, custom protocols
Study Concepts¶
- RPC Architecture - Understanding the plugin model
- Transport Configuration - Unix sockets vs TCP
- Security Model - Authentication and encryption
Get Help¶
- API Reference - Technical API documentation
- GitHub Issues - Report bugs
- GitHub Discussions - Ask questions
Contributing Examples¶
We welcome contributions! Please:
- Follow the established structure and naming conventions
- Include comprehensive documentation and comments
- Add appropriate error handling and logging
- Test examples on multiple platforms (Linux, macOS, Windows where applicable)
- Submit a pull request with your example
See the Contributing Guide for details.