Skip to content

Modern Docker Management, Designed for Everyone

fix: only log environment connect/disconnect events on real state transitions (#3564)

Kyle Mendellf6203a4d8862
diff --git a/backend/internal/bootstrap/edge_bootstrap.go b/backend/internal/bootstrap/edge_bootstrap.go
index f1f18f6e9..c03ecba90 100644
--- a/backend/internal/bootstrap/edge_bootstrap.go
+++ b/backend/internal/bootstrap/edge_bootstrap.go
@@ -39,28 +39,7 @@ func registerEdgeTunnelRoutes(
// Status callback to update environment status when agent connects/disconnects
statusCallback := func(ctx context.Context, envID string, connected bool) {
- envName := envID
- env, getErr := environmentService.GetEnvironmentByID(ctx, envID)
- if getErr != nil {
- slog.WarnContext(ctx, "Failed to load environment before edge status update", "environment_id", envID, "error", getErr)
- } else if env != nil && env.Name != "" {
- envName = env.Name
- }
-
- if err := environmentService.UpdateEnvironmentConnectionState(ctx, envID, connected); err != nil {
- slog.WarnContext(ctx, "Failed to update environment status on edge connect/disconnect", "environment_id", envID, "connected", connected, "error", err)
- } else {
- slog.InfoContext(ctx, "Updated edge environment connection state", "environment_id", envID, "connected", connected)
- }
-
- if err := createEdgeConnectionEvent(ctx, eventService, envID, envName, connected); err != nil {
- slog.WarnContext(ctx, "Failed to create edge connection event", "environment_id", envID, "connected", connected, "error", err)
- }
-
- // This is the only funnel for "an edge tunnel came up or went down"
- // (register, unregister and stale reaping all route through it), so it
- // is where open status streams learn about it without polling.
- environmentService.NotifyRuntimeStateChanged()
+ handleEdgeStatusChange(ctx, environmentService, eventService, envID, connected)
}
eventCallback := func(ctx context.Context, envID string, evt *edge.TunnelEvent) error {
@@ -218,6 +197,42 @@ func optionalStringPtr(value string) *string {
return &value
}
+// handleEdgeStatusChange records a tunnel up/down transition: it updates the
+// stored connection state, logs an event when the state actually changed, and
+// wakes any open status streams.
+func handleEdgeStatusChange(ctx context.Context, environmentService *environment.EnvironmentService, eventService *event.EventService, envID string, connected bool) {
+ envName := envID
+ env, getErr := environmentService.GetEnvironmentByID(ctx, envID)
+ if getErr != nil {
+ slog.WarnContext(ctx, "Failed to load environment before edge status update", "environment_id", envID, "error", getErr)
+ } else if env != nil && env.Name != "" {
+ envName = env.Name
+ }
+
+ if err := environmentService.UpdateEnvironmentConnectionState(ctx, envID, connected); err != nil {
+ slog.WarnContext(ctx, "Failed to update environment status on edge connect/disconnect", "environment_id", envID, "connected", connected, "error", err)
+ } else {
+ slog.InfoContext(ctx, "Updated edge environment connection state", "environment_id", envID, "connected", connected)
+ }
+
+ // Only log an event on an actual state transition; poll-mode tunnels can
+ // re-register without the environment ever having gone offline (session
+ // replacement, transport reconnects), and those are not worth an event.
+ alreadyInState := env != nil &&
+ ((connected && env.Status == string(models.EnvironmentStatusOnline)) ||
+ (!connected && env.Status == string(models.EnvironmentStatusOffline)))
+ if !alreadyInState {
+ if err := createEdgeConnectionEvent(ctx, eventService, envID, envName, connected); err != nil {
+ slog.WarnContext(ctx, "Failed to create edge connection event", "environment_id", envID, "connected", connected, "error", err)
+ }
+ }
+
+ // This is the only funnel for "an edge tunnel came up or went down"
+ // (register, unregister and stale reaping all route through it), so it
+ // is where open status streams learn about it without polling.
+ environmentService.NotifyRuntimeStateChanged()
+}
+
func createEdgeConnectionEvent(ctx context.Context, eventService *event.EventService, envID, envName string, connected bool) error {
if eventService == nil {
return nil
diff --git a/backend/internal/environment/environment_health.go b/backend/internal/environment/environment_health.go
index 31b6dc95b..2bb791bdd 100644
--- a/backend/internal/environment/environment_health.go
+++ b/backend/internal/environment/environment_health.go
@@ -220,6 +220,10 @@ func (s *EnvironmentService) TestConnection(ctx context.Context, id string, cust
// testEdgeConnection tests connection to an edge agent via its tunnel
func (s *EnvironmentService) testEdgeConnection(ctx context.Context, id string) (string, error) {
+ // The health check itself is standing demand for the tunnel; refreshing here
+ // keeps idle poll-mode tunnels open across check cycles instead of letting
+ // the demand TTL expire and the tunnel flap between checks.
+ edge.TouchTunnelDemand(id, edge.DefaultTunnelDemandTTL)
if !edge.HasActiveTunnel(id) {
if _, ok := edge.RequestTunnelAndWait(ctx, id, edge.DefaultTunnelDemandTTL, edge.DefaultTunnelAcquireTimeout()).Get(); !ok {
_ = s.updateEnvironmentStatusInternal(ctx, id, string(models.EnvironmentStatusOffline))
diff --git a/backend/pkg/libarcane/edge/poll_control.go b/backend/pkg/libarcane/edge/poll_control.go
index 73630bf12..0b2a73945 100644
--- a/backend/pkg/libarcane/edge/poll_control.go
+++ b/backend/pkg/libarcane/edge/poll_control.go
@@ -23,8 +23,11 @@ const (
// runtime status reporting when no live tunnel is currently open.
DefaultPollRuntimeTTL = 6 * time.Second
// DefaultTunnelDemandTTL is how long the manager should keep an edge tunnel
- // marked as required after a user/API request touches the environment.
- DefaultTunnelDemandTTL = 2 * time.Minute
+ // marked as required after a user/API request touches the environment. It is
+ // deliberately longer than the default environment health interval (2m) so
+ // health checks refreshing the demand keep idle poll-mode tunnels open
+ // instead of letting them flap between checks.
+ DefaultTunnelDemandTTL = 5 * time.Minute
// TunnelStatusIdle indicates that no reverse tunnel is currently needed.
TunnelStatusIdle = "IDLE"