Files
Hanzo AI 301a8e2601 rip gRPC, USE ZAP: netrunner control-plane now luxfi/zap-native (FORWARDS ONLY)
Per the project rule 'ZAP internal, ZIP edge': netrunner's 26-method
ControlService + PingService move off google.golang.org/grpc and
grpc-gateway onto a luxfi/zap envelope. Default builds now have ZERO
google.golang.org/grpc transitive deps.

New package: netrunner/zaprpc/
  - protocol.go: MsgType (uint8 range, encoded in ZAP flags upper byte).
    One stable wire ID per method. Append-only.
  - dispatch.go: typed Bind[Req, Resp](d, msg, handler) — register a
    'func(ctx, *Req) (*Resp, error)' handler. JSON encode/decode +
    envelope framing happen here, handlers stay business-logic-only.
  - server.go: thin wrapper around zap.Node hosting a Dispatcher.
  - client.go: typed Call[Req, Resp](ctx, c, msg, req) — same DX as the
    old gRPC client but transport is ZAP. Includes connect-retry around
    the boot race.

server/bind.go: one-shot registration of all 26 RPC methods onto a
Dispatcher. The canonical map of 'what this server exposes'.

server/server.go: gRPC server + grpc-gateway HTTP bridge are gone.
Run() now starts the ZAP server and waits for rootCtx.Done. The
'soldier-on' isClientCanceled helper goes with the gRPC stream code.

client/client.go: full rewrite. Same Client interface (28 methods),
implementation is now one-line zaprpc.Call per method. StreamStatus
becomes a client-driven Status() poll on the requested interval —
ZAP has no native server-streaming, but the observable contract
(channel of ClusterInfo) is unchanged.

Deleted:
  - rpcpb/rpc_grpc.pb.go (gRPC service stubs)
  - rpcpb/rpc.pb.gw.go (grpc-gateway HTTP bridge)

The rpcpb message types (rpc.pb.go) stay — they're plain Go structs
with json: tags, used as the JSON payload format inside ZAP envelopes.
A future cleanup pass can replace them with hand-written structs to
drop google.golang.org/protobuf from the dep tree too.

go.mod cleanup: drop github.com/grpc-ecosystem/grpc-gateway/v2,
google.golang.org/grpc, google.golang.org/genproto/googleapis/api.
Pin luxfi/proto v1.0.0 (was luxfi/protocol v0.0.4 — stale rename
artifact). Add luxfi/zap v0.2.0.

Tests: zaprpc/zaprpc_test.go round-trips request/response and proves
the error path propagates through the envelope.

No backwards compat — forwards perfection per the project rule.
2026-05-20 19:15:40 -07:00

82 lines
2.3 KiB
Go

// Copyright (C) 2021-2026, Lux Industries Inc. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause
package zaprpc
import (
"context"
"fmt"
"github.com/luxfi/zap"
)
// Dispatcher routes incoming ZAP messages by MsgType to typed handlers.
type Dispatcher struct {
handlers map[MsgType]zap.Handler
}
// NewDispatcher returns an empty dispatcher.
func NewDispatcher() *Dispatcher {
return &Dispatcher{handlers: make(map[MsgType]zap.Handler)}
}
// Bind registers a typed handler for the given MsgType. The handler signature
// is `func(ctx, *Req) (*Resp, error)`; JSON encode/decode and ZAP framing
// happen here, so handlers stay business-logic-only.
func Bind[Req, Resp any](d *Dispatcher, msgType MsgType, h func(context.Context, *Req) (*Resp, error)) {
d.handlers[msgType] = func(ctx context.Context, from string, msg *zap.Message) (*zap.Message, error) {
var req Req
if err := Decode(msg, &req); err != nil {
return errorResponse(msgType, fmt.Errorf("decode %s request: %w", msgType, err))
}
resp, err := h(ctx, &req)
var (
body any
errStr string
)
if err != nil {
errStr = err.Error()
} else {
body = resp
}
raw, encErr := Encode(msgType, body, errStr)
if encErr != nil {
return nil, encErr
}
return zap.Parse(raw)
}
}
// HandlerFor returns the ZAP handler for the given MsgType, or nil if unbound.
// The returned function plugs straight into zap.Node.Handle.
func (d *Dispatcher) HandlerFor(msgType MsgType) zap.Handler {
return d.handlers[msgType]
}
// AttachTo registers every bound handler onto the given ZAP node.
func (d *Dispatcher) AttachTo(n *zap.Node) {
for mt, h := range d.handlers {
n.Handle(uint16(mt), h)
}
}
// MsgTypes returns the set of method IDs bound on this dispatcher.
func (d *Dispatcher) MsgTypes() []MsgType {
out := make([]MsgType, 0, len(d.handlers))
for mt := range d.handlers {
out = append(out, mt)
}
return out
}
// errorResponse builds a ZAP message carrying just an RPC-level error.
// Returned to the client as the response to a request that failed before
// the handler could produce a typed body.
func errorResponse(msgType MsgType, err error) (*zap.Message, error) {
raw, encErr := Encode(msgType, nil, err.Error())
if encErr != nil {
return nil, encErr
}
return zap.Parse(raw)
}