Files
vpp-maglev/cmd/maglevc/shell.go
Pim van Pelt 1a1c48ef54 LB buckets column + health cascade; VPP dump fix; maglevc strictness
SPA (cmd/frontend/web):
- New "lb buckets" column backed by a 1s-debounced GetVPPLBState
  fetch loop with leading+trailing edge coalesce.
- Per-frontend health icon (/⚠️//‼️/) in the Zippy header,
  gated by a settling flag that suppresses ‼️ until the next lb-state
  reconciliation after a backend transition or weight change.
- In-place leaf merge on lb-state so stable bucket values (e.g. "0")
  don't retrigger the Flash animation on every refresh.
- Zippy cards remember open state in a cookie, default closed on
  fresh load; fixed-width frontend-title-name + reserved icon slot
  so headers line up across all cards.
- Clock-drift watchdog in sse.ts that forces a fresh EventSource on
  laptop-wake so the broker emits a resync instead of hanging on a
  dead half-open socket.

Frontend service (cmd/frontend):
- maglevClient.lbStateLoop, trigger on backend transitions +
  vpp-connect, best-effort fetch on refreshAll.
- Admin handlers explicitly wake the lb-state loop after lifecycle
  ops and set-weight (the latter emits no transition event on the
  maglevd side, so the WatchEvents path wouldn't have caught it).
- /favicon.ico served from embedded web/public IPng logo.

VPP integration:
- internal/vpp/lbstate.go: dumpASesForVIP drops Pfx from the dump
  request (setting it silently wipes IPv4 replies in the LB plugin)
  and filters results by prefix on the response side instead, which
  also demuxes multi-VIP-on-same-port cases correctly.

maglevc:
- Walk now returns the unconsumed token tail; dispatch and the
  question listener reject unknown commands with a targeted error
  instead of dumping the full command tree prefixed with garbage.
- On '?', echo the current line (including the '?') before the help
  list so the output reads like birdc.

Checker / prober:
- internal/checker: ±10% jitter on NextInterval so probes across
  restart don't all fire on the same tick.
- internal/prober: HTTP User-Agent now carries the build version
  and project URL.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-13 14:23:34 +02:00

124 lines
3.1 KiB
Go

// Copyright (c) 2026, Pim van Pelt <pim@ipng.ch>
package main
import (
"context"
"errors"
"fmt"
"io"
"strings"
"github.com/chzyer/readline"
"git.ipng.ch/ipng/vpp-maglev/internal/grpcapi"
)
// errQuit is a sentinel returned by runQuit to exit the REPL.
var errQuit = errors.New("quit")
// runShell runs the interactive REPL until the user types quit/exit or EOF.
func runShell(ctx context.Context, client grpcapi.MaglevClient) error {
root := buildTree()
comp := &Completer{root: root, client: client}
ql := &questionListener{root: root, client: client}
cfg := &readline.Config{
Prompt: "maglev> ",
AutoComplete: comp,
InterruptPrompt: "^C",
EOFPrompt: "exit",
Listener: ql,
}
rl, err := readline.NewEx(cfg)
if err != nil {
return fmt.Errorf("readline init: %w", err)
}
ql.rl = rl
defer rl.Close()
for {
line, err := rl.Readline()
if err == readline.ErrInterrupt {
continue
}
if err == io.EOF {
return nil
}
if err != nil {
return err
}
tokens := splitTokens(line)
if len(tokens) == 0 {
continue
}
if err := dispatch(ctx, root, client, tokens); err != nil {
if errors.Is(err, errQuit) {
return nil
}
fmt.Fprintf(rl.Stderr(), "%s\n", formatError(err))
}
}
}
// dispatch walks the tree and executes the matched command.
func dispatch(ctx context.Context, root *Node, client grpcapi.MaglevClient, tokens []string) error {
node, args, remaining := Walk(root, tokens)
if len(remaining) > 0 {
// One or more tokens couldn't be matched. Report the first
// offending token with the consumed prefix for context; don't
// dump the full command tree prefixed with garbage, which is
// what the previous code did and what prompted this fix.
consumed := tokens[:len(tokens)-len(remaining)]
return unknownCommandError(consumed, remaining[0])
}
if node.Run == nil {
showHelpAt(node, strings.Join(tokens, " "))
return nil
}
return node.Run(ctx, client, args)
}
// unknownCommandError builds the error returned by dispatch when the
// tree walk couldn't consume the full token list. The format differs
// slightly depending on whether any tokens were consumed, so the
// message always points at the first unknown token and its context.
func unknownCommandError(consumed []string, bad string) error {
if len(consumed) == 0 {
return fmt.Errorf("unknown command: %s", bad)
}
return fmt.Errorf("unknown subcommand %q after %q", bad, strings.Join(consumed, " "))
}
// showHelpAt prints the reachable leaves below node, each displayed
// with the given prefix. Split from dispatch so the caller can decide
// which node to anchor the help at without re-walking the tree.
func showHelpAt(node *Node, prefix string) {
lines := expandPaths(node, prefix, make(map[*Node]bool))
maxLen := 0
for _, l := range lines {
if len(l.path) > maxLen {
maxLen = len(l.path)
}
}
if len(lines) == 0 {
fmt.Println(" <no completions>")
return
}
for _, l := range lines {
if l.help != "" {
fmt.Printf("%-*s %s\n", maxLen+2, l.path, l.help)
} else {
fmt.Printf("%s\n", l.path)
}
}
}