Files
zeekay 1eff004a9e 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

123 lines
3.2 KiB
Go

// Copyright (C) 2021-2025, Lux Industries Inc. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause
package server
import (
"context"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
log "github.com/luxfi/log"
"github.com/luxfi/netrunner/server"
"github.com/luxfi/netrunner/utils"
"github.com/luxfi/netrunner/utils/constants"
"github.com/spf13/cobra"
)
func init() {
cobra.EnablePrefixMatching = true
}
const serverRootDirPrefix = "server"
var (
logLevel string
logDir string
port string
gwPort string
dialTimeout time.Duration
disableNodesOutput bool
snapshotsDir string
)
func NewCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "server [options]",
Short: "Start a network runner server.",
RunE: serverFunc,
Args: cobra.ExactArgs(0),
}
cmd.PersistentFlags().StringVar(&logLevel, "log-level", log.InfoLevel.String(), "log level for server logs")
cmd.PersistentFlags().StringVar(&logDir, "log-dir", "", "log directory")
cmd.PersistentFlags().StringVar(&port, "port", ":9000", "ZAP server port")
cmd.PersistentFlags().StringVar(&gwPort, "gateway-port", "", "reserved for the optional ZIP HTTP edge — empty disables")
cmd.PersistentFlags().DurationVar(&dialTimeout, "dial-timeout", 10*time.Second, "server dial timeout")
cmd.PersistentFlags().BoolVar(&disableNodesOutput, "disable-nodes-output", false, "true to disable nodes stdout/stderr")
cmd.PersistentFlags().StringVar(&snapshotsDir, "snapshots-dir", "", "directory for snapshots")
return cmd
}
func serverFunc(*cobra.Command, []string) (err error) {
if logDir == "" {
anrRootDir := filepath.Join(os.TempDir(), constants.RootDirPrefix)
err = os.MkdirAll(anrRootDir, os.ModePerm)
if err != nil {
return err
}
serverRootDir := filepath.Join(anrRootDir, serverRootDirPrefix)
logDir, err = utils.MkDirWithTimestamp(serverRootDir)
if err != nil {
return err
}
}
logLevel, err := log.ToLevel(logLevel)
if err != nil {
return err
}
logFactory := log.NewFactoryWithConfig(log.Config{
RotatingWriterConfig: log.RotatingWriterConfig{
Directory: logDir,
},
DisplayLevel: logLevel,
LogLevel: logLevel,
})
logger, err := logFactory.Make(constants.LogNameMain)
if err != nil {
return err
}
s, err := server.New(server.Config{
Port: port,
GwPort: gwPort,
DialTimeout: dialTimeout,
RedirectNodesOutput: !disableNodesOutput,
SnapshotsDir: snapshotsDir,
LogLevel: logLevel,
}, logger)
if err != nil {
return err
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
errChan := make(chan error)
go func() {
errChan <- s.Run(ctx)
}()
// Relay SIGINT and SIGTERM to [sigChan]
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
select {
case sig := <-sigChan:
// Got a SIGINT or SIGTERM; stop the server and wait for it to finish.
logger.Warn("signal received: closing server", log.String("signal", sig.String()))
cancel()
waitForServerStop := <-errChan
logger.Warn("closed server", log.Err(waitForServerStop))
case serverClosed := <-errChan:
// The server stopped.
logger.Warn("server closed", log.Err(serverClosed))
}
return nil
}