Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions PowerSync/PowerSync.Common/Client/Sync/Stream/Remote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public class RequestDetails

public class Remote
{

private const int STREAMING_POST_TIMEOUT_MS = 30_000;

private readonly HttpClient httpClient;
protected IPowerSyncBackendConnector connector;

Expand Down Expand Up @@ -148,12 +151,22 @@ public async Task<T> Get<T>(string path, Dictionary<string, string>? headers = n
using var reader = new StreamReader(stream, Encoding.UTF8);
string? line;

using var timeoutCts = new CancellationTokenSource();
using var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(options.CancellationToken, timeoutCts.Token);

linkedCts.Token.Register(() =>
{
stream.Close();
});

while ((line = await reader.ReadLineAsync()) != null)
{
timeoutCts.CancelAfter(TimeSpan.FromMilliseconds(STREAMING_POST_TIMEOUT_MS));
yield return ParseStreamingSyncLine(JObject.Parse(line));
}
}


public static StreamingSyncLine? ParseStreamingSyncLine(JObject json)
{
// Determine the type based on available keys
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace PowerSync.Common.Client.Sync.Stream;

using System.Net.Sockets;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;

Expand Down Expand Up @@ -285,7 +286,14 @@ protected async Task StreamingSync(CancellationToken? signal, PowerSyncConnectio
}
catch (Exception ex)
{
logger.LogError("Caught exception in streaming sync: {message}", ex.Message);
var exMessage = ex.Message;
if (ex.InnerException != null && (ex.InnerException is ObjectDisposedException || ex.InnerException is SocketException))
{
exMessage = "Stream closed or timed out -" + ex.InnerException.Message;
}


logger.LogError("Caught exception in streaming sync: {message}", exMessage);

// Either:
// - A network request failed with a failed connection or not OKAY response code.
Expand Down