Skip to content

Agent Mesh

address comments

Antonio Ojea88b6e58b880c
diff --git a/cmd/sam-node/node.go b/cmd/sam-node/node.go
index 03c49d69..371ec97f 100644
--- a/cmd/sam-node/node.go
+++ b/cmd/sam-node/node.go
@@ -749,13 +749,7 @@ func (n *SamNode) findProvidersByCID(ctx context.Context, c cid.Cid) ([]peer.Add
if n.DHT == nil {
return nil, fmt.Errorf("DHT not initialized")
}
- timeout := dhtLookupTimeout
- if deadline, ok := ctx.Deadline(); ok {
- if remaining := time.Until(deadline); remaining > 0 {
- timeout = remaining
- }
- }
- lookupCtx, cancel := context.WithTimeout(ctx, timeout)
+ lookupCtx, cancel := context.WithTimeout(ctx, dhtLookupTimeout)
defer cancel()
// FindProvidersAsync can emit the same peer multiple times when the
// DHT walk converges from different paths; dedupe so downstream
@@ -850,13 +844,7 @@ func (n *SamNode) DiscoverRemoteServicesStream(ctx context.Context, serviceType
return
}
- timeout := discoveryFanoutTimeout
- if deadline, ok := ctx.Deadline(); ok {
- if remaining := time.Until(deadline); remaining > 0 {
- timeout = remaining
- }
- }
- fanoutCtx, cancel := context.WithTimeout(ctx, timeout)
+ fanoutCtx, cancel := context.WithTimeout(ctx, discoveryFanoutTimeout)
defer cancel()
var wg sync.WaitGroup
@@ -922,13 +910,7 @@ func (n *SamNode) discoverServicesByType(ctx context.Context, serviceType api.Se
return nil, err
}
- timeout := discoveryFanoutTimeout
- if deadline, ok := ctx.Deadline(); ok {
- if remaining := time.Until(deadline); remaining > 0 {
- timeout = remaining
- }
- }
- fanoutCtx, cancel := context.WithTimeout(ctx, timeout)
+ fanoutCtx, cancel := context.WithTimeout(ctx, discoveryFanoutTimeout)
defer cancel()
type peerCatalog struct {
diff --git a/cmd/sam-node/sidecar.go b/cmd/sam-node/sidecar.go
index 81de19ce..2cc06c4c 100644
--- a/cmd/sam-node/sidecar.go
+++ b/cmd/sam-node/sidecar.go
@@ -277,6 +277,13 @@ func handleDiscoverService(node *SamNode, w http.ResponseWriter, r *http.Request
isStreaming := streamParam == "true" || acceptHeader == "text/event-stream"
if isStreaming {
+ out, err := node.DiscoverRemoteServicesStream(ctx, serviceType, serviceName)
+ if err != nil {
+ logger.Errorf("Failed to start streaming service discovery: %v", err)
+ http.Error(w, fmt.Sprintf("Failed to start streaming service discovery: %v", err), http.StatusInternalServerError)
+ return
+ }
+
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
@@ -287,16 +294,6 @@ func handleDiscoverService(node *SamNode, w http.ResponseWriter, r *http.Request
return
}
- out, err := node.DiscoverRemoteServicesStream(ctx, serviceType, serviceName)
- if err != nil {
- logger.Errorf("Failed to start streaming service discovery: %v", err)
- if _, err := fmt.Fprintf(w, "event: error\ndata: %s\n\n", err.Error()); err != nil {
- logger.Errorf("Failed to write SSE error: %v", err)
- }
- flusher.Flush()
- return
- }
-
for {
select {
case <-ctx.Done():
@@ -316,6 +313,7 @@ func handleDiscoverService(node *SamNode, w http.ResponseWriter, r *http.Request
}
if _, err := fmt.Fprintf(w, "data: %s\n\n", data); err != nil {
logger.Errorf("Failed to write SSE data: %v", err)
+ return
}
flusher.Flush()
}