Skip to content

Agent Mesh

address review: drop the SetBackendProbeTimeout/probeTimeout setter+getter pair

Fernando Marino20469848166b
diff --git a/internal/node/identity_evidence_http_test.go b/internal/node/identity_evidence_http_test.go
index da12f74e..6a8f75bf 100644
--- a/internal/node/identity_evidence_http_test.go
+++ b/internal/node/identity_evidence_http_test.go
@@ -42,7 +42,7 @@ func TestIdentityEvidenceRoutesHaveOwnMetricClass(t *testing.T) {
func TestIdentityEvidenceTrailingSlashReturnsNotFound(t *testing.T) {
node := &SamNode{
BiscuitTimeout: 500 * time.Millisecond,
- services: NewServiceRegistry(&fakeDHT{}),
+ services: NewServiceRegistry(&fakeDHT{}, 0),
}
socketPath := filepath.Join(t.TempDir(), "sam.sock")
diff --git a/internal/node/mcp_discovery_test.go b/internal/node/mcp_discovery_test.go
index d936261f..ef63e5a9 100644
--- a/internal/node/mcp_discovery_test.go
+++ b/internal/node/mcp_discovery_test.go
@@ -71,8 +71,8 @@ func (f *fakeToolService) Tools(_ context.Context) ([]string, error) {
func TestDiscoverySource(t *testing.T) {
node := &SamNode{
- services: NewServiceRegistry(&fakeDHT{}),
- nodeConfig: &NodeConfigComplete{Labels: map[string]string{"region": "EU"}},
+ services: NewServiceRegistry(&fakeDHT{}, 0),
+ config: Options{Labels: map[string]string{"region": "EU"}},
}
ctx := context.Background()
diff --git a/internal/node/node.go b/internal/node/node.go
index 039084b9..f402e053 100644
--- a/internal/node/node.go
+++ b/internal/node/node.go
@@ -518,9 +518,8 @@ func (n *SamNode) Start(ctx context.Context) error {
}
n.DHT = kdht
- n.services = NewServiceRegistry(n.DHT)
+ n.services = NewServiceRegistry(n.DHT, n.config.BackendProbeTimeout)
n.services.reprovideNow = n.triggerReprovide
- n.services.SetBackendProbeTimeout(n.config.BackendProbeTimeout)
var authenticated bool
var fatalAuthErr error
diff --git a/internal/node/service_registry.go b/internal/node/service_registry.go
index 37872d1f..398583ce 100644
--- a/internal/node/service_registry.go
+++ b/internal/node/service_registry.go
@@ -77,44 +77,26 @@ type ServiceRegistry struct {
// advertised. Optional; nil outside a running node.
reprovideNow func()
- // backendProbeTimeout bounds each backend probe in advertisable. Defaults
- // to defaultDHTProbeTimeout; override with SetBackendProbeTimeout.
+ // backendProbeTimeout bounds each backend probe in advertisable. Set once
+ // at construction (see NewServiceRegistry); never mutated afterwards, so
+ // reading it needs no lock.
backendProbeTimeout time.Duration
}
-func NewServiceRegistry(d dhtProvider) *ServiceRegistry {
+// NewServiceRegistry constructs a registry bounding backend probes by
+// backendProbeTimeout. A zero or negative value falls back to
+// defaultDHTProbeTimeout, so callers can pass an unset
+// Options.BackendProbeTimeout straight through without an explicit
+// zero-check.
+func NewServiceRegistry(d dhtProvider, backendProbeTimeout time.Duration) *ServiceRegistry {
+ if backendProbeTimeout <= 0 {
+ backendProbeTimeout = defaultDHTProbeTimeout
+ }
return &ServiceRegistry{
services: map[string]Service{},
dht: d,
- backendProbeTimeout: defaultDHTProbeTimeout,
- }
-}
-
-// SetBackendProbeTimeout overrides the default backend probe timeout
-// (defaultDHTProbeTimeout). A zero or negative duration is a no-op, so callers can
-// pass an unset Options.BackendProbeTimeout straight through without an
-// explicit zero-check.
-func (r *ServiceRegistry) SetBackendProbeTimeout(d time.Duration) {
- if d <= 0 {
- return
- }
- r.mu.Lock()
- defer r.mu.Unlock()
- r.backendProbeTimeout = d
-}
-
-// probeTimeout returns the current backend probe timeout, falling back to
-// defaultDHTProbeTimeout for a zero-initialized registry (e.g. a struct
-// literal built directly in a test, bypassing NewServiceRegistry) so it
-// behaves the same as a properly constructed one rather than timing out
-// every probe immediately.
-func (r *ServiceRegistry) probeTimeout() time.Duration {
- r.mu.RLock()
- defer r.mu.RUnlock()
- if r.backendProbeTimeout <= 0 {
- return defaultDHTProbeTimeout
+ backendProbeTimeout: backendProbeTimeout,
}
- return r.backendProbeTimeout
}
// Register initialises a service, advertises it on the DHT, and inserts it
@@ -143,7 +125,7 @@ func (r *ServiceRegistry) Register(ctx context.Context, svc Service) error {
return err
}
- probeErr := advertisable(ctx, svc, r.probeTimeout())
+ probeErr := advertisable(ctx, svc, r.backendProbeTimeout)
if probeErr != nil {
logger.Warnf("[ServiceRegistry] Registered %s/%s but not advertising it: backend did not answer: %v", info.Type, info.Name, probeErr)
} else {
@@ -268,7 +250,7 @@ Loop:
}()
info := svc.Info()
- if err := advertisable(ctx, svc, r.probeTimeout()); err != nil {
+ if err := advertisable(ctx, svc, r.backendProbeTimeout); err != nil {
withheld.Add(1)
// On shutdown every service fails this way, and saying so
// would blame backends for the node stopping.
diff --git a/internal/node/service_registry_test.go b/internal/node/service_registry_test.go
index a07a3c17..f813e06b 100644
--- a/internal/node/service_registry_test.go
+++ b/internal/node/service_registry_test.go
@@ -71,8 +71,9 @@ func newFakeSvc(name string, st api.ServiceType) *fakeService {
// newServiceRegistryForTest builds a registry against the fake DHT for tests.
func newServiceRegistryForTest(d dhtProvider) *ServiceRegistry {
return &ServiceRegistry{
- services: map[string]Service{},
- dht: d,
+ services: map[string]Service{},
+ dht: d,
+ backendProbeTimeout: defaultDHTProbeTimeout,
}
}
@@ -306,16 +307,16 @@ func (p *slowProbingService) Probe(ctx context.Context) error {
// The bug behind #376: defaultDHTProbeTimeout was a hard-coded 2s with no way to
// raise it, so a backend whose own cold-start cost alone exceeds that -
// measured in practice for moderately-featured MCP server stacks - could
-// never be advertised on its first registration. SetBackendProbeTimeout
-// (wired from --backend-probe-timeout) is the fix: the same slow backend
-// must fail to advertise under the default and succeed once given more time.
+// never be advertised on its first registration. NewServiceRegistry's
+// backendProbeTimeout parameter (wired from --backend-probe-timeout) is the
+// fix: the same slow backend must fail to advertise under the default and
+// succeed once constructed with more time.
func TestServiceRegistry_BackendProbeTimeoutIsConfigurable(t *testing.T) {
const probeDelay = 60 * time.Millisecond
t.Run("default timeout is too short for a slow backend", func(t *testing.T) {
dht := &fakeDHT{}
- r := NewServiceRegistry(dht)
- r.SetBackendProbeTimeout(10 * time.Millisecond) // shorter than probeDelay
+ r := NewServiceRegistry(dht, 10*time.Millisecond) // shorter than probeDelay
svc := newSlowProbingSvc("slow", probeDelay)
if err := r.Register(context.Background(), svc); err != nil {
@@ -328,8 +329,7 @@ func TestServiceRegistry_BackendProbeTimeoutIsConfigurable(t *testing.T) {
t.Run("raising the timeout lets the same backend advertise", func(t *testing.T) {
dht := &fakeDHT{}
- r := NewServiceRegistry(dht)
- r.SetBackendProbeTimeout(probeDelay * 5) // comfortably longer than probeDelay
+ r := NewServiceRegistry(dht, probeDelay*5) // comfortably longer than probeDelay
svc := newSlowProbingSvc("slow", probeDelay)
if err := r.Register(context.Background(), svc); err != nil {
@@ -340,19 +340,12 @@ func TestServiceRegistry_BackendProbeTimeoutIsConfigurable(t *testing.T) {
}
})
- t.Run("NewServiceRegistry defaults to defaultDHTProbeTimeout unchanged", func(t *testing.T) {
- r := NewServiceRegistry(&fakeDHT{})
- if got := r.probeTimeout(); got != defaultDHTProbeTimeout {
- t.Errorf("default probe timeout = %v, want %v (unchanged default behaviour)", got, defaultDHTProbeTimeout)
- }
- })
-
- t.Run("SetBackendProbeTimeout ignores zero and negative durations", func(t *testing.T) {
- r := NewServiceRegistry(&fakeDHT{})
- r.SetBackendProbeTimeout(0)
- r.SetBackendProbeTimeout(-1 * time.Second)
- if got := r.probeTimeout(); got != defaultDHTProbeTimeout {
- t.Errorf("probe timeout after no-op sets = %v, want unchanged %v", got, defaultDHTProbeTimeout)
+ t.Run("zero or negative backendProbeTimeout falls back to defaultDHTProbeTimeout", func(t *testing.T) {
+ for _, d := range []time.Duration{0, -1 * time.Second} {
+ r := NewServiceRegistry(&fakeDHT{}, d)
+ if got := r.backendProbeTimeout; got != defaultDHTProbeTimeout {
+ t.Errorf("NewServiceRegistry(dht, %v).backendProbeTimeout = %v, want %v", d, got, defaultDHTProbeTimeout)
+ }
}
})
}
diff --git a/internal/node/sidecar_auth_test.go b/internal/node/sidecar_auth_test.go
index fc8b311b..0fc04e19 100644
--- a/internal/node/sidecar_auth_test.go
+++ b/internal/node/sidecar_auth_test.go
@@ -66,7 +66,7 @@ func TestConstantTimeEqual(t *testing.T) {
func TestMetricsGatedOnTCPButNotOnTheSocket(t *testing.T) {
node := &SamNode{
BiscuitTimeout: 500 * time.Millisecond,
- services: NewServiceRegistry(&fakeDHT{}),
+ services: NewServiceRegistry(&fakeDHT{}, 0),
}
socketPath := filepath.Join(t.TempDir(), "sam.sock")
diff --git a/internal/node/sidecar_test.go b/internal/node/sidecar_test.go
index e8d8986e..bbbe2990 100644
--- a/internal/node/sidecar_test.go
+++ b/internal/node/sidecar_test.go
@@ -70,7 +70,7 @@ func waitForSocket(t *testing.T, path string) *http.Client {
func TestSidecarSocketAuthorizesWithoutToken(t *testing.T) {
node := &SamNode{
BiscuitTimeout: 500 * time.Millisecond,
- services: NewServiceRegistry(&fakeDHT{}),
+ services: NewServiceRegistry(&fakeDHT{}, 0),
}
socketPath := filepath.Join(t.TempDir(), "sam.sock")
@@ -120,7 +120,7 @@ func TestSidecarSocketAuthorizesWithoutToken(t *testing.T) {
func TestSidecarSocketOnly(t *testing.T) {
node := &SamNode{
BiscuitTimeout: 500 * time.Millisecond,
- services: NewServiceRegistry(&fakeDHT{}),
+ services: NewServiceRegistry(&fakeDHT{}, 0),
}
socketPath := filepath.Join(t.TempDir(), "sam.sock")
@@ -138,7 +138,7 @@ func TestSidecarSocketOnly(t *testing.T) {
}
func TestStartSidecarServerRequiresAListener(t *testing.T) {
- node := &SamNode{services: NewServiceRegistry(&fakeDHT{})}
+ node := &SamNode{services: NewServiceRegistry(&fakeDHT{}, 0)}
if _, err := StartSidecarServer(node, "", "", "token", "", "", ""); err == nil {
t.Fatal("expected an error when neither a TCP address nor a socket is configured")
}
@@ -156,7 +156,7 @@ func TestSidecarSocketFailureKeepsTCPServing(t *testing.T) {
node := &SamNode{
BiscuitTimeout: 500 * time.Millisecond,
- services: NewServiceRegistry(&fakeDHT{}),
+ services: NewServiceRegistry(&fakeDHT{}, 0),
}
srv, err := StartSidecarServer(node, "127.0.0.1:0", socketPath, "test-token", "", "", "")
if err != nil {
@@ -170,7 +170,7 @@ func TestSidecarSocketFailureKeepsTCPServing(t *testing.T) {
t.Errorf("BoundSocketPath = %q, want empty after a failed socket", node.BoundSocketPath)
}
- socketOnly := &SamNode{services: NewServiceRegistry(&fakeDHT{})}
+ socketOnly := &SamNode{services: NewServiceRegistry(&fakeDHT{}, 0)}
if _, err := StartSidecarServer(socketOnly, "", socketPath, "", "", "", ""); err == nil {
t.Error("expected an error when the socket is the only configured listener")
}
@@ -363,7 +363,7 @@ func TestWithAuth(t *testing.T) {
func TestSidecarServerAuthEnforcement(t *testing.T) {
node := &SamNode{
BiscuitTimeout: 500 * time.Millisecond,
- services: NewServiceRegistry(&fakeDHT{}),
+ services: NewServiceRegistry(&fakeDHT{}, 0),
}
// We use a dummy token
token := "test-token"
@@ -453,7 +453,7 @@ func TestSidecarServerAuthEnforcement(t *testing.T) {
func TestSidecarAuthorizationFallbackScope(t *testing.T) {
node := &SamNode{
BiscuitTimeout: 500 * time.Millisecond,
- services: NewServiceRegistry(&fakeDHT{}),
+ services: NewServiceRegistry(&fakeDHT{}, 0),
}
token := "test-token"
@@ -546,7 +546,7 @@ func TestRegisterService(t *testing.T) {
time.Sleep(100 * time.Millisecond)
node := &SamNode{BiscuitTimeout: 500 * time.Millisecond,
- services: NewServiceRegistry(d),
+ services: NewServiceRegistry(d, 0),
DHT: d,
}
@@ -575,7 +575,7 @@ func TestRegisterService(t *testing.T) {
func TestUnregisterService(t *testing.T) {
node := &SamNode{BiscuitTimeout: 500 * time.Millisecond,
- services: NewServiceRegistry(&fakeDHT{}),
+ services: NewServiceRegistry(&fakeDHT{}, 0),
}
node.services.insertService(&testService{info: &api.ServiceInfo{Name: "test-service"}})
@@ -601,7 +601,7 @@ func TestHandleDiscoverService(t *testing.T) {
defer func() { _ = d.Close() }()
node := &SamNode{BiscuitTimeout: 500 * time.Millisecond,
- services: NewServiceRegistry(d),
+ services: NewServiceRegistry(d, 0),
DHT: d,
Host: h,
BoundHTTPAddr: "127.0.0.1:8080",
@@ -660,7 +660,7 @@ func TestHandleDiscoverService(t *testing.T) {
func TestListLocalServices(t *testing.T) {
node := &SamNode{BiscuitTimeout: 500 * time.Millisecond,
- services: NewServiceRegistry(&fakeDHT{}),
+ services: NewServiceRegistry(&fakeDHT{}, 0),
}
service1 := &api.ServiceInfo{Type: api.ServiceType_SERVICE_TYPE_MCP, Name: "service1"}
@@ -678,7 +678,7 @@ func TestListLocalServices(t *testing.T) {
func TestListLocalServices_TypeFilter(t *testing.T) {
node := &SamNode{BiscuitTimeout: 500 * time.Millisecond,
- services: NewServiceRegistry(&fakeDHT{}),
+ services: NewServiceRegistry(&fakeDHT{}, 0),
}
mcpA := &api.ServiceInfo{Type: api.ServiceType_SERVICE_TYPE_MCP, Name: "mcp-a"}
mcpB := &api.ServiceInfo{Type: api.ServiceType_SERVICE_TYPE_MCP, Name: "mcp-b"}
@@ -777,7 +777,7 @@ func TestServiceKeyToCID_Equivalence(t *testing.T) {
func TestRegisterService_Validation(t *testing.T) {
node := &SamNode{BiscuitTimeout: 500 * time.Millisecond,
- services: NewServiceRegistry(&fakeDHT{}),
+ services: NewServiceRegistry(&fakeDHT{}, 0),
}
tests := []struct {
@@ -858,7 +858,7 @@ func TestDiscoverService_Pagination(t *testing.T) {
node := &SamNode{
BiscuitTimeout: 500 * time.Millisecond,
- services: NewServiceRegistry(d),
+ services: NewServiceRegistry(d, 0),
DHT: d,
Host: h,
BoundHTTPAddr: "127.0.0.1:8080",