A native L7 reverse proxy / load balancer runtime: host/path routing, five balancing policies, forwarding header policy, passive health checks and failover.
Dependencies
moon run cmd/main --target native# weighted round-robin across the three backends
curl -s http://127.0.0.1:18080/
# routed to the api pool
curl -s http://127.0.0.1:18080/api/users
# a request body is forwarded
curl -s -X POST http://127.0.0.1:18080/ -d 'hello'let web = BackendPool::new("web", policy=WeightedRR)
.add(Backend::new("10.0.0.1", 8080, weight=2))
.add(Backend::new("10.0.0.2", 8080, weight=1))
let config = ProxyConfig::new(host="0.0.0.0", port=80)
.add_route(Route::prefix("root", "/", web))
let runtime = @runtime.Runtime::new(config)
runtime.serve()moon fmt
moon check --target all --deny-warn
moon build --target all
moon test --target allpub struct Backend {
host : String
port : Int
weight : Int
scheme : Scheme
healthy : Bool
failures : Int
active_conns : Int
}pub struct HealthPolicy {
unhealthy_threshold : Int
healthy_threshold : Int
}pub(all) enum HostMatch {
Any
Host(String)
}pub(all) enum LBPolicy {
RoundRobin
WeightedRR
LeastConn
Random
IPHash
}pub(all) enum PathMatch {
Exact(String)
Prefix(String)
}pub struct Route {
id : String
host : HostMatch
path : PathMatch
pool : BackendPool
options : RouteOptions
}fn Route::new(id : String, host : HostMatch, path : PathMatch, pool : BackendPool, options? : RouteOptions) -> Routepub struct RouteOptions {
preserve_host : Bool
forward_headers : Bool
failover : Bool
}fn RouteOptions::new(preserve_host? : Bool, forward_headers? : Bool, failover? : Bool) -> RouteOptionspub(all) enum Scheme {
Http
Https
}fn can_retry_status(meth : String, status : Int) -> Boolfn forward_request_headers(incoming : Array[(String, String)], client_ip : String, scheme : String, original_host : String, upstream_host : String, options : RouteOptions) -> Array[(String, String)]fn is_idempotent(meth : String) -> Boolfn is_retryable_status(status : Int) -> Boolfn normalize_host(host : String) -> StringInstall
Download zipA native L7 reverse proxy / load balancer runtime: host/path routing, five balancing policies, forwarding header policy, passive health checks and failover.
Dependencies