carlos / platform Public

host: keep routeReady's route reads in place around the guard

The connect and HTTP probe move into readyConnect and readyProbe,
which take the dialer; routeReady keeps every route read, so the
census sees a body change at its existing authority disposition
rather than reads moving to a new unit.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev fb18cdfadc05fb2c46e61c3167a9a82f1695e99f parent eb4d512
2 files changed, +27 −21
  • internal/host/unitsettle.go +24 −19
  • internal/host/unitsettle_guard_test.go +3 −2
diff --git a/internal/host/unitsettle.go b/internal/host/unitsettle.go
index cf82a9793..6dfe95d79 100644
--- a/internal/host/unitsettle.go
+++ b/internal/host/unitsettle.go
@@ -295,16 +295,9 @@ func unitSettleTimeout(getenv func(string) string) time.Duration {
// routeReady is the readiness check for rt's effective upstream (spec §2.4):
// Socket wins over Addr. With a configured probe it is an HTTP GET; without
// one, a connect. nil when the route has no upstream: settle then requires
-// the unit to stay active for its Hold.
+// the unit to stay active for its Hold. Both dial through upstreamguard
+// (readyConnect, readyProbe).
func routeReady(rt registry.Route, timeout time.Duration) func() error {
- return routeReadyVia(net.Dialer{}, rt, timeout)
-}
-
-// routeReadyVia is routeReady dialing with base (a test sets its Resolver)
-// behind upstreamguard: a TCP upstream is route-supplied, so both the
-// connect and the probe refuse a link-local or IMDS destination after
-// resolution and before connect. A unix socket path passes the guard.
-func routeReadyVia(base net.Dialer, rt registry.Route, timeout time.Duration) func() error {
network, address := "unix", rt.Socket
if address == "" {
network, address = "tcp", rt.Addr
@@ -313,16 +306,7 @@ func routeReadyVia(base net.Dialer, rt registry.Route, timeout time.Duration) fu
return nil
}
if rt.Health == "" {
- d := base
- d.Timeout = timeout
- dialer := upstreamguard.Dialer(d)
- return func() error {
- c, err := dialer.Dial(network, address)
- if err != nil {
- return fmt.Errorf("connect %s: %w", address, err)
- }
- return c.Close()
- }
+ return readyConnect(net.Dialer{}, network, address, timeout)
}
var h provision.Health
if err := json.Unmarshal([]byte(rt.Health), &h); err != nil {
@@ -333,6 +317,27 @@ func routeReadyVia(base net.Dialer, rt registry.Route, timeout time.Duration) fu
if hostHdr == "" {
hostHdr = rt.Host
}
+ return readyProbe(net.Dialer{}, network, address, h, hostHdr, timeout)
+}
+
+// readyConnect is routeReady's connect check, dialing with base (a test sets
+// its Resolver) behind upstreamguard: a TCP upstream is route-supplied, so a
+// link-local or IMDS destination is refused after resolution and before
+// connect. A unix socket path passes the guard.
+func readyConnect(base net.Dialer, network, address string, timeout time.Duration) func() error {
+ base.Timeout = timeout
+ dialer := upstreamguard.Dialer(base)
+ return func() error {
+ c, err := dialer.Dial(network, address)
+ if err != nil {
+ return fmt.Errorf("connect %s: %w", address, err)
+ }
+ return c.Close()
+ }
+}
+
+// readyProbe is routeReady's HTTP probe, dialing as readyConnect does.
+func readyProbe(base net.Dialer, network, address string, h provision.Health, hostHdr string, timeout time.Duration) func() error {
dialer := upstreamguard.Dialer(base)
client := &http.Client{Timeout: timeout, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }, Transport: &http.Transport{
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
diff --git a/internal/host/unitsettle_guard_test.go b/internal/host/unitsettle_guard_test.go
index 55e63cca3..dcce93f51 100644
--- a/internal/host/unitsettle_guard_test.go
+++ b/internal/host/unitsettle_guard_test.go
@@ -9,6 +9,7 @@ import (
"testing"
"time"
+ "github.com/carlosframework/platform/internal/provision"
"github.com/carlosframework/platform/internal/registry"
"github.com/carlosframework/platform/internal/upstreamguard"
"github.com/carlosframework/platform/internal/upstreamguard/guardtest"
@@ -31,8 +32,8 @@ func TestRouteReadyRefusesLinkLocal(t *testing.T) {
name string
ready func() error
}{
- {"connect, name resolving to IMDS", routeReadyVia(base, registry.Route{Host: "x", Addr: "imds.test:80"}, time.Second)},
- {"probe, name resolving to IMDS", routeReadyVia(base, registry.Route{Host: "x", Addr: "imds.test:80", Health: probe}, time.Second)},
+ {"connect, name resolving to IMDS", readyConnect(base, "tcp", "imds.test:80", time.Second)},
+ {"probe, name resolving to IMDS", readyProbe(base, "tcp", "imds.test:80", provision.Health{Path: "/healthz", Status: 200}, "x", time.Second)},
{"connect, IMDS literal, production dialer", routeReady(registry.Route{Host: "x", Addr: "169.254.169.254:80"}, time.Second)},
{"probe, IMDS literal, production dialer", routeReady(registry.Route{Host: "x", Addr: "169.254.169.254:80", Health: probe}, time.Second)},
{"proof, IMDS among Addrs", proofRouteReady(registry.Route{Host: "x", Addrs: ln.Addr().String() + ",169.254.169.254:80"}, time.Second)},