svc
Service-level networking - create a round-robin load balancer over a list of backend URLs, pick the next backend, make GET and POST requests through the balancer, call a JSON-RPC 2.0 endpoint, call a gRPC-style JSON-over-HTTP endpoint, and build mesh propagation headers with a trace ID and service name.
Load with: use svc
What this module does
svc provides the primitives for talking to multi-instance backend
services. The round-robin balancer tracks a counter and cycles through the
backend list on each pick. The JSON-RPC and gRPC-style helpers encode the
request body and decode the JSON response, making it easy to call structured
APIs. The mesh header builder generates standard x-trace-id and
x-service headers for distributed tracing.
Quick example
use svc
# Round-robin over three backend instances
rr = svcrr(["http://10.0.0.1:8080", "http://10.0.0.2:8080", "http://10.0.0.3:8080"])
# Pick the next backend (cycles automatically)
b = svcrr1(rr) # "http://10.0.0.1:8080" first call
# GET/POST via round-robin
res = svcge(rr, "/api/v1/users")
res = svcpo(rr, "/api/v1/items", jsn.enc({name:"foo"}), "application/json")
# JSON-RPC 2.0
result = svcrp("http://api.example.com/rpc", "user.get", {id: 42})
# gRPC-style JSON-over-HTTP
result = svcgr("http://api.example.com", "UserService", "GetUser", {id: 42})
# Mesh headers for distributed tracing
hdrs = svcme("trace-abc-123", "auth-service", nil)
Functions
Load balancing
svcrr(backends)Creates a round-robin balancer state over a non-empty list of backend URL strings.
svcrr1(rr)Picks the next backend URL from the round-robin and advances the counter.
svcge(rr, path)Makes a GET request to the next backend URL + path via net.get.
svcpo(rr, path, body, opts)Makes a POST request to the next backend URL + path via net.pst.
RPC
svcrp(url, method, params)Sends a JSON-RPC 2.0 request {jsonrpc:"2.0", id:1, method, params} to url and returns the decoded JSON response.
svcgr(url, service, method, msg)Calls url/service/method with msg JSON-encoded as the POST body and returns the decoded JSON response. Mirrors gRPC HTTP/JSON transcoding.
Mesh headers
svcme(trace_id, service, extra)Returns an HTTP headers object with x-trace-id and x-service set. Merges any additional key-value pairs from extra[0] if provided.
Notes
- Requires
trl,txt,net,jsn, anderu.