Skip to content

Commit 50f612c

Browse files
authored
Fix duplicate connection verification logs: add debounce and state-ch… (#413)
* Fix duplicate connection verification logs: add debounce and state-change-only logging * Address CodeRabbit feedback: use status constants, fix comments, remove redundant code
1 parent b57a2ec commit 50f612c

File tree

2 files changed

+67
-9
lines changed

2 files changed

+67
-9
lines changed

MCPForUnity/Editor/Windows/Components/Connection/McpConnectionSection.cs

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ private enum TransportProtocol
4444
private Button testConnectionButton;
4545

4646
private bool connectionToggleInProgress;
47+
private Task verificationTask;
48+
private string lastHealthStatus;
49+
50+
// Health status constants
51+
private const string HealthStatusUnknown = "Unknown";
52+
private const string HealthStatusHealthy = "Healthy";
53+
private const string HealthStatusPingFailed = "Ping Failed";
54+
private const string HealthStatusUnhealthy = "Unhealthy";
4755

4856
// Events
4957
public event Action OnManualConfigUpdateRequested;
@@ -173,7 +181,7 @@ public void UpdateConnectionStatus()
173181
statusIndicator.AddToClassList("disconnected");
174182
connectionToggleButton.text = "Start Session";
175183

176-
healthStatusLabel.text = "Unknown";
184+
healthStatusLabel.text = HealthStatusUnknown;
177185
healthIndicator.RemoveFromClassList("healthy");
178186
healthIndicator.RemoveFromClassList("warning");
179187
healthIndicator.AddToClassList("unknown");
@@ -408,15 +416,33 @@ private async void OnTestConnectionClicked()
408416
}
409417

410418
public async Task VerifyBridgeConnectionAsync()
419+
{
420+
// Prevent concurrent verification calls
421+
if (verificationTask != null && !verificationTask.IsCompleted)
422+
{
423+
return;
424+
}
425+
426+
verificationTask = VerifyBridgeConnectionInternalAsync();
427+
await verificationTask;
428+
}
429+
430+
private async Task VerifyBridgeConnectionInternalAsync()
411431
{
412432
var bridgeService = MCPServiceLocator.Bridge;
413433
if (!bridgeService.IsRunning)
414434
{
415-
healthStatusLabel.text = "Disconnected";
435+
healthStatusLabel.text = HealthStatusUnknown;
416436
healthIndicator.RemoveFromClassList("healthy");
417437
healthIndicator.RemoveFromClassList("warning");
418438
healthIndicator.AddToClassList("unknown");
419-
McpLog.Warn("Cannot verify connection: Bridge is not running");
439+
440+
// Only log if state changed
441+
if (lastHealthStatus != HealthStatusUnknown)
442+
{
443+
McpLog.Warn("Cannot verify connection: Bridge is not running");
444+
lastHealthStatus = HealthStatusUnknown;
445+
}
420446
return;
421447
}
422448

@@ -426,23 +452,45 @@ public async Task VerifyBridgeConnectionAsync()
426452
healthIndicator.RemoveFromClassList("warning");
427453
healthIndicator.RemoveFromClassList("unknown");
428454

455+
string newStatus;
429456
if (result.Success && result.PingSucceeded)
430457
{
431-
healthStatusLabel.text = "Healthy";
458+
newStatus = HealthStatusHealthy;
459+
healthStatusLabel.text = newStatus;
432460
healthIndicator.AddToClassList("healthy");
433-
McpLog.Debug($"Connection verification successful: {result.Message}");
461+
462+
// Only log if state changed
463+
if (lastHealthStatus != newStatus)
464+
{
465+
McpLog.Debug($"Connection verification successful: {result.Message}");
466+
lastHealthStatus = newStatus;
467+
}
434468
}
435469
else if (result.HandshakeValid)
436470
{
437-
healthStatusLabel.text = "Ping Failed";
471+
newStatus = HealthStatusPingFailed;
472+
healthStatusLabel.text = newStatus;
438473
healthIndicator.AddToClassList("warning");
439-
McpLog.Warn($"Connection verification warning: {result.Message}");
474+
475+
// Log once per distinct warning state
476+
if (lastHealthStatus != newStatus)
477+
{
478+
McpLog.Warn($"Connection verification warning: {result.Message}");
479+
lastHealthStatus = newStatus;
480+
}
440481
}
441482
else
442483
{
443-
healthStatusLabel.text = "Unhealthy";
484+
newStatus = HealthStatusUnhealthy;
485+
healthStatusLabel.text = newStatus;
444486
healthIndicator.AddToClassList("warning");
445-
McpLog.Error($"Connection verification failed: {result.Message}");
487+
488+
// Log once per distinct error state
489+
if (lastHealthStatus != newStatus)
490+
{
491+
McpLog.Error($"Connection verification failed: {result.Message}");
492+
lastHealthStatus = newStatus;
493+
}
446494
}
447495
}
448496
}

MCPForUnity/Editor/Windows/MCPForUnityEditorWindow.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public class MCPForUnityEditorWindow : EditorWindow
2121

2222
private static readonly HashSet<MCPForUnityEditorWindow> OpenWindows = new();
2323
private bool guiCreated = false;
24+
private double lastRefreshTime = 0;
25+
private const double RefreshDebounceSeconds = 0.5;
2426

2527
public static void ShowWindow()
2628
{
@@ -181,6 +183,14 @@ private void OnEditorUpdate()
181183

182184
private void RefreshAllData()
183185
{
186+
// Debounce rapid successive calls (e.g., from OnFocus being called multiple times)
187+
double currentTime = EditorApplication.timeSinceStartup;
188+
if (currentTime - lastRefreshTime < RefreshDebounceSeconds)
189+
{
190+
return;
191+
}
192+
lastRefreshTime = currentTime;
193+
184194
connectionSection?.UpdateConnectionStatus();
185195

186196
if (MCPServiceLocator.Bridge.IsRunning)

0 commit comments

Comments
 (0)