Overview
The GatewayService provides a robust gateway to a custom language service with advanced resilience patterns including circuit breaker, retry budget, and idempotency support. This service demonstrates production-grade reliability patterns for external service integration. Base Path:/gateway.v1.GatewayService
Authentication: Required - Bearer token via Authorization header
Middleware: CORS, rate limiting
RPCs
InvokeCustom
Invokes the custom language service with the provided name, returning a custom-generated message. Includes circuit breaker protection, smart retry logic with retry budget, and synchronous database logging for both successes and failures. Endpoint:POST /gateway.v1.GatewayService/InvokeCustom
Signature:
Request
The name to pass to the custom language service. If empty, defaults to “World”.
Response
The custom message generated by the downstream language service
Connect-Query Example with Idempotency
cURL Example (h2c)
Example Response
Implementation Details
Circuit Breaker Pattern
The GatewayService uses the gobreaker library to implement circuit breaker protection: Circuit Breaker Configuration:- Name:
custom-lang-service - Max Requests (half-open): 3
- Interval: 10 seconds (failure count reset window)
- Timeout: 30 seconds (time to wait before half-open state)
- Trip Threshold: 5 consecutive failures
- Closed (Normal): All requests pass through
- Open (Tripped): Requests fail immediately with
CodeUnavailableerror - Half-Open (Testing): Limited requests (max 3) to test service recovery
Retry Budget Pattern
The service implements a token bucket-based retry budget to prevent retry storms: Retry Budget Configuration:- Capacity: 20 tokens
- Refill Rate: 10 tokens per second
- The error is retryable (see Retryable Errors below)
- A retry token is available in the budget
429 Too Many Requests502 Bad Gateway503 Service Unavailable504 Gateway Timeout- Network timeout errors
400 Bad Request401 Unauthorized403 Forbidden404 Not Found- Context deadline exceeded
Database Integration
The InvokeCustom RPC performs synchronous database writes to theinvocations table for both successes and failures:
- Success/failure rate analysis
- Error pattern detection
- Debugging failed invocations
Downstream Communication
The service makes HTTP POST requests to the custom language service: Endpoint:POST /invoke
Request Body:
2xx: Success - extract message from response4xx/5xx: Error - wrapped with appropriate Connect error code
Error Code Mapping
HTTP status codes are mapped to Connect RPC error codes:| HTTP Status | Connect Code | Description |
|---|---|---|
| 400 | CodeInvalidArgument | Bad request |
| 401 | CodeUnauthenticated | Authentication failed |
| 403 | CodePermissionDenied | Forbidden |
| 404 | CodeNotFound | Not found |
| 409 | CodeAlreadyExists | Conflict |
| 429 | CodeResourceExhausted | Too many requests |
| 502, 503 | CodeUnavailable | Service unavailable |
| 504 | CodeDeadlineExceeded | Gateway timeout |
| 5xx (other) | CodeInternal | Internal server error |
Authentication
All requests to the GatewayService must include a valid JWT token in theAuthorization header:
401 Unauthorized response.
Idempotency Support
The GatewayService supports idempotency through theIdempotency-Key header:
Service Dependencies
- Custom Language Service: Target service at
http://custom-lang-service.microservices:3000 - PostgreSQL: Stores invocation records in the
gateway_db.invocationstable
Service Configuration
| Environment Variable | Default | Description |
|---|---|---|
PORT | 8082 | Service port |
CUSTOM_LANG_BASE_URL | http://custom-lang-service.microservices:3000 | Custom language service endpoint |
DATABASE_URL | - | PostgreSQL connection string (optional) |
OTEL_EXPORTER_OTLP_ENDPOINT | - | OpenTelemetry collector endpoint |
High Availability
Pod Disruption Budget
The GatewayService is protected by a PodDisruptionBudget to ensure availability during cluster maintenance:Resilience Features
- Circuit Breaker: Prevents cascading failures when downstream service is unhealthy
- Retry Budget: Limits retry attempts to prevent retry storms
- Timeout Control: 1-second timeout per downstream request
- OpenTelemetry: Full distributed tracing for debugging
- Database Logging: Complete audit trail of all invocations
Monitoring & Observability
Key Metrics to Monitor
- Circuit breaker state (closed/open/half-open)
- Retry budget token availability
- Success/failure rates from
invocationstable - Request latency (p50, p95, p99)
- Downstream service response times
Circuit Breaker State
The circuit breaker state can be monitored through OpenTelemetry metrics. When the circuit opens, all requests will fail immediately until the timeout period (30 seconds) elapses.Rate Limiting
The GatewayService is protected by rate limiting middleware configured in Traefik. Excessive requests may receive a429 Too Many Requests response at the ingress layer.