Skip to content

monitor datastream connection and fallback to api flag check if not c…#72

Merged
cbrady merged 6 commits intomainfrom
chris/sch-3923-fallback-to-api-flag-check-if-datastream-not-connected
Jul 22, 2025
Merged

monitor datastream connection and fallback to api flag check if not c…#72
cbrady merged 6 commits intomainfrom
chris/sch-3923-fallback-to-api-flag-check-if-datastream-not-connected

Conversation

@cbrady
Copy link
Contributor

@cbrady cbrady commented Jul 17, 2025

…onnected

@cbrady cbrady requested a review from a team as a code owner July 17, 2025 18:12
@cbrady cbrady requested a review from Copilot July 17, 2025 18:23

This comment was marked as outdated.

@cbrady cbrady force-pushed the chris/sch-3923-fallback-to-api-flag-check-if-datastream-not-connected branch from ba68933 to 86d22ae Compare July 17, 2025 18:28
@cbrady cbrady force-pushed the chris/sch-3923-fallback-to-api-flag-check-if-datastream-not-connected branch from 86d22ae to 7a23db9 Compare July 17, 2025 18:35
@cbrady cbrady force-pushed the chris/sch-3923-fallback-to-api-flag-check-if-datastream-not-connected branch from 2008352 to b4d550e Compare July 22, 2025 17:55
@cbrady cbrady requested a review from Copilot July 22, 2025 18:00
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enhances the datastream connection monitoring functionality to provide better fallback handling when the connection is lost. The changes implement a robust monitoring system that continuously checks the datastream connection status and automatically falls back to API-based flag checks when the connection is unavailable.

  • Adds background connection monitoring with periodic status checks
  • Implements proper connection state tracking and fallback logic
  • Refactors connection handling to use callbacks instead of TaskCompletionSource

Reviewed Changes

Copilot reviewed 6 out of 7 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/SchematicHQ.Client/Schematic.cs Adds connection monitoring background task and improves fallback logic in CheckFlag method
src/SchematicHQ.Client/Datastream/DatastreamClientAdapter.cs Introduces ConnectionStateTracker class and connection status checking functionality
src/SchematicHQ.Client/Datastream/Client.cs Refactors connection handling to use callback-based state updates and improves error handling
src/SchematicHQ.Client.Test/Datastream/Mocks/DatastreamClientTestFactory.cs Updates test factory to use new callback-based connection monitoring
examples/DatastreamTestServer/Program.cs Updates example configuration for Redis cache and base URL
.github/workflows/claude-code.yml Fixes GitHub API membership check logic

Comment on lines +172 to +177
// Start a background task that periodically checks the connection status
Task.Run(async () =>
{
try
{
while (!_disposed)
Copy link

Copilot AI Jul 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using Task.Run for fire-and-forget background tasks can lead to unhandled exceptions. Consider storing the task reference to properly handle exceptions or use a more robust background service pattern.

Suggested change
// Start a background task that periodically checks the connection status
Task.Run(async () =>
{
try
{
while (!_disposed)
// Create a CancellationTokenSource for managing task cancellation
_connectionMonitoringCancellationTokenSource = new CancellationTokenSource();
var cancellationToken = _connectionMonitoringCancellationTokenSource.Token;
// Start a background task that periodically checks the connection status
_connectionMonitoringTask = Task.Run(async () =>
{
try
{
while (!cancellationToken.IsCancellationRequested)

Copilot uses AI. Check for mistakes.
Comment on lines +116 to +118
public void UpdateConnectionState(bool connected)
{
_stateLock.Wait();
Copy link

Copilot AI Jul 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using synchronous Wait() on SemaphoreSlim in an async context can cause deadlocks. Use WaitAsync() instead to maintain async patterns throughout the call chain.

Suggested change
public void UpdateConnectionState(bool connected)
{
_stateLock.Wait();
public async Task UpdateConnectionStateAsync(bool connected)
{
await _stateLock.WaitAsync();

Copilot uses AI. Check for mistakes.
@@ -141,17 +141,23 @@ private async Task ConnectAndReadAsync()
try
{
await _reconnectSemaphore.WaitAsync();
Copy link

Copilot AI Jul 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The semaphore is acquired but the corresponding Release() call is moved to a catch block far below. This makes the resource management pattern hard to follow and error-prone. Consider using a using statement or try-finally block immediately after acquisition.

Copilot uses AI. Check for mistakes.

if (_webSocket != null)
{
try { _webSocket.Dispose(); } catch { /* ignore */ }
Copy link

Copilot AI Jul 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty catch blocks that silently ignore exceptions make debugging difficult. Consider logging the exception or at minimum adding a comment explaining why the exception can be safely ignored.

Suggested change
try { _webSocket.Dispose(); } catch { /* ignore */ }
try { _webSocket.Dispose(); } catch (Exception disposeEx) { _logger.Error("Error disposing WebSocket: {0}", disposeEx.Message); }

Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@cbrady cbrady merged commit 0c1e62e into main Jul 22, 2025
5 checks passed
@cbrady cbrady deleted the chris/sch-3923-fallback-to-api-flag-check-if-datastream-not-connected branch July 22, 2025 23:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants