Skip to content
Merged
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
10 changes: 6 additions & 4 deletions src/NATS.Client.Core/NatsConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public partial class NatsConnection : INatsConnection
private TimeSpan _backoff = TimeSpan.Zero;
private string _lastAuthError = string.Empty;
private bool _stopRetries;
private Task? _publishEventsTask;
private Task? _reconnectLoopTask;

public NatsConnection()
: this(NatsOpts.Default)
Expand Down Expand Up @@ -97,7 +99,7 @@ public NatsConnection(NatsOpts opts)
SingleWriter = false,
SingleReader = true,
});
_ = Task.Run(PublishEventsAsync, _disposedCancellationTokenSource.Token);
_publishEventsTask = Task.Run(PublishEventsAsync, _disposedCancellationTokenSource.Token);
}

// events
Expand Down Expand Up @@ -410,7 +412,7 @@ private async ValueTask InitialConnectAsync()
_pingTimerCancellationTokenSource = new CancellationTokenSource();
StartPingTimer(_pingTimerCancellationTokenSource.Token);
_waitForOpenConnection.TrySetResult();
_ = Task.Run(ReconnectLoop);
_reconnectLoopTask = Task.Run(ReconnectLoop);
_eventChannel.Writer.TryWrite((NatsEvent.ConnectionOpened, new NatsEventArgs(url?.ToString() ?? string.Empty)));
}
}
Expand Down Expand Up @@ -709,7 +711,7 @@ private async void ReconnectLoop()
_pingTimerCancellationTokenSource = new CancellationTokenSource();
StartPingTimer(_pingTimerCancellationTokenSource.Token);
_waitForOpenConnection.TrySetResult();
_ = Task.Run(ReconnectLoop);
_reconnectLoopTask = Task.Run(ReconnectLoop);
_eventChannel.Writer.TryWrite((NatsEvent.ConnectionOpened, new NatsEventArgs(url.ToString())));
}
}
Expand Down Expand Up @@ -796,7 +798,7 @@ private async Task PublishEventsAsync()
{
_logger.LogError(NatsLogEvents.Connection, ex, "Error occured when publishing events");
if (!_disposedCancellationTokenSource.IsCancellationRequested)
_ = Task.Run(PublishEventsAsync, _disposedCancellationTokenSource.Token);
_publishEventsTask = Task.Run(PublishEventsAsync, _disposedCancellationTokenSource.Token);
}
}

Expand Down