revoke-gateway
High-performance API gateway built on Cloudflare Pingora for the Revoke microservices framework.
Overview
revoke-gateway is a production-ready API gateway that provides intelligent routing, load balancing, and resilience features for microservices. Built on top of Cloudflare's Pingora framework, it offers exceptional performance and reliability.
Features
- High Performance: Built on Pingora's async architecture
- Service Discovery: Automatic integration with service registries
- Load Balancing: Uses Pingora's round-robin load balancer
- Health Checks: TCP health monitoring for backend services
- Request Routing: Path-based service routing
- Service Discovery: Dynamic backend discovery from registry
- Header Forwarding: X-Forwarded-For and custom headers
Quick Start
Basic Usage
use ;
use MemoryRegistry;
use ;
use Arc;
async
Configuration
The gateway is configured using the GatewayConfig struct:
let config = GatewayConfig ;
Or via command line arguments:
Architecture
Request Flow
- Incoming Request → Gateway receives HTTP request
- Service Extraction → Extracts service name from URL path (e.g.,
/users/123→users) - Service Discovery → Queries registry for service instances
- Load Balancing → Creates or reuses Pingora LoadBalancer with round-robin selection
- Health Checking → Ensures only healthy backends receive traffic
- Request Proxying → Forwards request to selected backend
- Header Injection → Adds X-Gateway and X-Forwarded-For headers
- Response Processing → Returns response with X-Gateway-Version header
Components
Service Discovery Integration
The gateway automatically discovers services from the registry:
// Services register themselves with the registry
let service = ServiceInfo ;
registry.register.await?;
// Gateway discovers services by name from URL path
// Request to /users/123 → looks up "users" service
Load Balancer
The gateway uses Pingora's built-in load balancing:
// Gateway automatically creates load balancers for each service
// with Pingora's round-robin selection algorithm
// When a request comes in:
// 1. Extract service name from path
// 2. Get or create LoadBalancer for that service
// 3. Select a backend using round-robin
// 4. Proxy request to selected backend
The gateway caches load balancers per service for performance.
Health Checks
When enabled, the gateway uses Pingora's TCP health checks:
let config = GatewayConfig ;
Advanced Usage
Multiple Service Instances
Register multiple instances for load balancing:
// Register 3 instances of user-service
for i in 0..3
// Gateway will automatically load balance requests across all instances
Router Module
The gateway includes a basic router for custom routing logic:
use ;
let mut router = new;
router.add_route;
// Match routes
if let Some = router.match_route
Integration
With Service Registry
// Memory registry (for testing)
use MemoryRegistry;
let registry = new;
// Consul registry
use ConsulRegistry;
let registry = new;
Best Practices
- Service Naming: Use consistent service names that match URL paths
- Health Checks: Enable health checks for production deployments
- Multiple Instances: Deploy multiple instances of each service for reliability
- Monitoring: Monitor gateway logs for errors and performance issues
- Backend Refresh: Set appropriate backend refresh intervals
Troubleshooting
Common Issues
- Services Not Found: Check service registry connectivity and service names
- 503 Service Unavailable: No healthy backends available - check service health
- Connection Refused: Ensure backend services are running on registered ports
- Health Check Failures: Verify backend services are responding to TCP connections
Debug Logging
Enable debug logging:
init_from_env;
Examples
See the examples directory for complete examples:
simple_gateway.rs- Basic gateway with single servicepingora_load_balancing.rs- Multiple service instances with load balancing