A high-performance RPC gateway that provides load balancing and health checking for multiple RPC providers.
- Load balancing across multiple RPC providers
- Health checking with automatic failover
- Support for multiple chains
- Prometheus metrics
- Configurable timeouts and intervals
The gateway is configured using a YAML file. Here's an example configuration:
port: 3000 # port for RPC gateway
metrics:
port: 9010 # port for prometheus metrics, served on /metrics and /
proxies:
- path: "eth" # directory path for the proxy (e.g. http://localhost:3000/eth)
timeout: "1s" # timeout for both upstream requests and health checks
healthChecks:
interval: "12s" # how often to do healthchecks
blockDiffThreshold: 2 # how many blocks behind the max block number to taint
targets:
- name: "provider1"
connection:
http:
url: "https://provider1.example.com"
- name: "provider2"
connection:
http:
url: "https://provider2.example.com"
port
: The port on which the RPC gateway will listenmetrics.port
: The port on which Prometheus metrics will be servedproxies
: List of proxy configurations for different chainspath
: The URL path for this proxy (e.g., "eth" for Ethereum)timeout
: How long to wait for both upstream responses and health checkshealthChecks
: Health check configurationinterval
: How often to perform health checksblockDiffThreshold
: Maximum allowed block difference between providers
targets
: List of RPC providersname
: Provider identifierconnection.http.url
: Provider's HTTP URL
The gateway performs health checks on each RPC provider:
- Block number check: Verifies the provider can return the latest block number
- Gas left check: Verifies the provider can execute
eth_call
requests
A provider is marked as unhealthy if:
- Any health check fails
- The provider's block number is too far behind other providers
- The provider fails to respond within the configured timeout
A provider is marked as healthy again as soon as it passes a health check.
The gateway exposes Prometheus metrics at /metrics
:
rpc_provider_info
: Information about each RPC providerrpc_provider_status
: Current health status of each providerrpc_provider_block_number
: Latest block number reported by each providerrpc_provider_gas_left
: Gas left in each provider
go build -o rpc-gateway
./rpc-gateway -config config.yml
MIT
sequenceDiagram
Alice->>RPC Gateway: eth_call
loop Healthcheck
RPC Gateway->>Alchemy: Check health
RPC Gateway->>Infura: Check health
end
Note right of RPC Gateway: Routes only to healthy targets
loop Configurable Retries
RPC Gateway->>Alchemy: eth_call?
Alchemy-->>RPC Gateway: ERROR
end
Note right of RPC Gateway: RPC Call is rerouted after failing retries
RPC Gateway->>Infura: eth_call?
Infura-->>RPC Gateway: {"result":[...]}
RPC Gateway-->>Alice: {"result":[...]}
Make sure the test pass
go test -v ./...
To run the app locally
# Set log level (optional, defaults to "warn")
export LOG_LEVEL=debug # Available levels: debug, info, warn, error
go run . --config example_config.yml