mirror of
https://github.com/sipeed/picoclaw.git
synced 2026-06-12 18:08:54 +00:00
feat(host): complete launcher and gateway multi-host binding support
- add shared netbind planning for strict tcp4/tcp6 bind semantics - support launcher/gateway host env overrides and launcher-to-gateway forwarding - cover host binding and forwarding with network and subprocess env tests
This commit is contained in:
+39
-8
@@ -44,6 +44,7 @@ import (
|
||||
"github.com/sipeed/picoclaw/pkg/heartbeat"
|
||||
"github.com/sipeed/picoclaw/pkg/logger"
|
||||
"github.com/sipeed/picoclaw/pkg/media"
|
||||
"github.com/sipeed/picoclaw/pkg/netbind"
|
||||
"github.com/sipeed/picoclaw/pkg/pid"
|
||||
"github.com/sipeed/picoclaw/pkg/providers"
|
||||
"github.com/sipeed/picoclaw/pkg/state"
|
||||
@@ -161,13 +162,30 @@ func Run(debug bool, homePath, configPath string, allowEmptyStartup bool) (runEr
|
||||
logger.Infof("Log level set to %q", effectiveLogLevel)
|
||||
}
|
||||
|
||||
bindPlan, listenResult, err := openGatewayListeners(cfg.Gateway.Host, cfg.Gateway.Port)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error opening gateway listeners: %w", err)
|
||||
}
|
||||
|
||||
// Enforce singleton: write PID file with generated token.
|
||||
pidData, err := pid.WritePidFile(homePath, cfg.Gateway.Host, cfg.Gateway.Port)
|
||||
pidData, err := pid.WritePidFile(homePath, bindPlan.ProbeHost, cfg.Gateway.Port)
|
||||
if err != nil {
|
||||
logger.Warnf("write pid file failed: %v", err)
|
||||
for _, ln := range listenResult.Listeners {
|
||||
_ = ln.Close()
|
||||
}
|
||||
return fmt.Errorf("singleton check failed: %w", err)
|
||||
}
|
||||
defer pid.RemovePidFile(homePath)
|
||||
closeListeners := true
|
||||
defer func() {
|
||||
if !closeListeners {
|
||||
return
|
||||
}
|
||||
for _, ln := range listenResult.Listeners {
|
||||
_ = ln.Close()
|
||||
}
|
||||
}()
|
||||
|
||||
provider, modelID, err := createStartupProvider(cfg, allowEmptyStartup)
|
||||
if err != nil {
|
||||
@@ -195,10 +213,11 @@ func Run(debug bool, homePath, configPath string, allowEmptyStartup bool) (runEr
|
||||
"skills_available": skillsInfo["available"],
|
||||
})
|
||||
|
||||
runningServices, err := setupAndStartServices(cfg, agentLoop, msgBus, pidData.Token)
|
||||
runningServices, err := setupAndStartServices(cfg, agentLoop, msgBus, pidData.Token, listenResult)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
closeListeners = false
|
||||
|
||||
// Setup manual reload channel for /reload endpoint
|
||||
manualReloadChan := make(chan struct{}, 1)
|
||||
@@ -219,8 +238,9 @@ func Run(debug bool, homePath, configPath string, allowEmptyStartup bool) (runEr
|
||||
runningServices.HealthServer.SetReloadFunc(reloadTrigger)
|
||||
agentLoop.SetReloadFunc(reloadTrigger)
|
||||
|
||||
listenAddr := net.JoinHostPort(cfg.Gateway.Host, strconv.Itoa(cfg.Gateway.Port))
|
||||
fmt.Printf("✓ Gateway started on %s\n", listenAddr)
|
||||
for _, bindHost := range listenResult.BindHosts {
|
||||
fmt.Printf("✓ Gateway started on %s\n", net.JoinHostPort(bindHost, strconv.Itoa(cfg.Gateway.Port)))
|
||||
}
|
||||
fmt.Println("Press Ctrl+C to stop")
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
@@ -323,6 +343,7 @@ func setupAndStartServices(
|
||||
agentLoop *agent.AgentLoop,
|
||||
msgBus *bus.MessageBus,
|
||||
authToken string,
|
||||
listenResult netbind.OpenResult,
|
||||
) (*services, error) {
|
||||
runningServices := &services{}
|
||||
|
||||
@@ -393,10 +414,20 @@ func setupAndStartServices(
|
||||
fmt.Println("⚠ Warning: No channels enabled")
|
||||
}
|
||||
|
||||
addr := net.JoinHostPort(cfg.Gateway.Host, strconv.Itoa(cfg.Gateway.Port))
|
||||
runningServices.authToken = authToken
|
||||
runningServices.HealthServer = health.NewServer(cfg.Gateway.Host, cfg.Gateway.Port, authToken)
|
||||
runningServices.ChannelManager.SetupHTTPServer(addr, runningServices.HealthServer)
|
||||
runningServices.HealthServer = health.NewServer(listenResult.ProbeHost, cfg.Gateway.Port, authToken)
|
||||
|
||||
listenAddr := ""
|
||||
if len(listenResult.Listeners) > 0 {
|
||||
listenAddr = listenResult.Listeners[0].Addr().String()
|
||||
} else {
|
||||
listenAddr = net.JoinHostPort(listenResult.ProbeHost, strconv.Itoa(cfg.Gateway.Port))
|
||||
}
|
||||
runningServices.ChannelManager.SetupHTTPServerListeners(
|
||||
listenResult.Listeners,
|
||||
listenAddr,
|
||||
runningServices.HealthServer,
|
||||
)
|
||||
|
||||
if err = runningServices.ChannelManager.StartAll(context.Background()); err != nil {
|
||||
return nil, fmt.Errorf("error starting channels: %w", err)
|
||||
@@ -412,7 +443,7 @@ func setupAndStartServices(
|
||||
voiceAgent.Start(vaCtx)
|
||||
}
|
||||
|
||||
healthAddr := net.JoinHostPort(cfg.Gateway.Host, strconv.Itoa(cfg.Gateway.Port))
|
||||
healthAddr := net.JoinHostPort(listenResult.ProbeHost, strconv.Itoa(cfg.Gateway.Port))
|
||||
fmt.Printf(
|
||||
"✓ Health endpoints available at http://%s/health, /ready and /reload (POST)\n",
|
||||
healthAddr,
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/sipeed/picoclaw/pkg/netbind"
|
||||
)
|
||||
|
||||
func openGatewayListeners(host string, port int) (netbind.Plan, netbind.OpenResult, error) {
|
||||
plan, err := netbind.BuildPlan(host, netbind.DefaultLoopback)
|
||||
if err != nil {
|
||||
return netbind.Plan{}, netbind.OpenResult{}, err
|
||||
}
|
||||
|
||||
result, err := netbind.OpenPlan(plan, strconv.Itoa(port))
|
||||
if err != nil {
|
||||
return netbind.Plan{}, netbind.OpenResult{}, err
|
||||
}
|
||||
|
||||
return plan, result, nil
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/sipeed/picoclaw/pkg/netbind"
|
||||
)
|
||||
|
||||
func TestOpenGatewayListeners_HonorsIPv6OnlyHost(t *testing.T) {
|
||||
hasIPv4, hasIPv6 := netbind.DetectIPFamilies()
|
||||
if !hasIPv6 {
|
||||
t.Skip("IPv6 is unavailable in this environment")
|
||||
}
|
||||
|
||||
_, result, err := openGatewayListeners("::", 0)
|
||||
if err != nil {
|
||||
t.Fatalf("openGatewayListeners() error = %v", err)
|
||||
}
|
||||
startGatewayTestHTTPServer(t, result.Listeners)
|
||||
port := mustGatewayAtoi(t, result.Port)
|
||||
|
||||
requireGatewayHTTPReachable(t, "::1", port)
|
||||
if hasIPv4 {
|
||||
requireGatewayHTTPUnreachable(t, "127.0.0.1", port)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenGatewayListeners_SupportsExplicitMultiHost(t *testing.T) {
|
||||
hasIPv4, hasIPv6 := netbind.DetectIPFamilies()
|
||||
if !hasIPv4 || !hasIPv6 {
|
||||
t.Skip("dual-stack loopback is unavailable in this environment")
|
||||
}
|
||||
|
||||
_, result, err := openGatewayListeners("127.0.0.1,::1", 0)
|
||||
if err != nil {
|
||||
t.Fatalf("openGatewayListeners() error = %v", err)
|
||||
}
|
||||
startGatewayTestHTTPServer(t, result.Listeners)
|
||||
port := mustGatewayAtoi(t, result.Port)
|
||||
|
||||
requireGatewayHTTPReachable(t, "127.0.0.1", port)
|
||||
requireGatewayHTTPReachable(t, "::1", port)
|
||||
}
|
||||
|
||||
func startGatewayTestHTTPServer(t *testing.T, listeners []net.Listener) {
|
||||
t.Helper()
|
||||
|
||||
server := &http.Server{
|
||||
Handler: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
_, _ = io.WriteString(w, "ok")
|
||||
}),
|
||||
}
|
||||
|
||||
errCh := make(chan error, len(listeners))
|
||||
for _, listener := range listeners {
|
||||
ln := listener
|
||||
go func() {
|
||||
errCh <- server.Serve(ln)
|
||||
}()
|
||||
}
|
||||
|
||||
t.Cleanup(func() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
_ = server.Shutdown(ctx)
|
||||
for range listeners {
|
||||
err := <-errCh
|
||||
if err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
t.Fatalf("server.Serve() error = %v", err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func requireGatewayHTTPReachable(t *testing.T, host string, port int) {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(2 * time.Second)
|
||||
for {
|
||||
err := gatewayHTTPGet(host, port)
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatalf("expected %s:%d to be reachable: %v", host, port, err)
|
||||
}
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
func requireGatewayHTTPUnreachable(t *testing.T, host string, port int) {
|
||||
t.Helper()
|
||||
if err := gatewayHTTPGet(host, port); err == nil {
|
||||
t.Fatalf("expected %s:%d to be unreachable", host, port)
|
||||
}
|
||||
}
|
||||
|
||||
func gatewayHTTPGet(host string, port int) error {
|
||||
client := &http.Client{
|
||||
Timeout: 300 * time.Millisecond,
|
||||
Transport: &http.Transport{
|
||||
Proxy: nil,
|
||||
},
|
||||
}
|
||||
|
||||
resp, err := client.Get("http://" + net.JoinHostPort(host, strconv.Itoa(port)))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return errors.New(resp.Status)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func mustGatewayAtoi(t *testing.T, value string) int {
|
||||
t.Helper()
|
||||
n, err := strconv.Atoi(value)
|
||||
if err != nil {
|
||||
t.Fatalf("Atoi(%q) error = %v", value, err)
|
||||
}
|
||||
return n
|
||||
}
|
||||
Reference in New Issue
Block a user