nats.net
nats.net copied to clipboard
Add RetryOnFailedConnect
Currently, initial connect behavior attempts the server list and then gives up. Traverse the connection list and retry honoring reconnect attempt values.
See: https://github.com/nats-io/nats.go/pull/581
Option to mirror go:
Options.RetryOnFailedConnect = true;
So, is this feature coming to the library? Are there commits adding this?
There is a feature in progress that will allow you to retry on the initial connect. Currently any failure on initial connect does not try to retry. There should be a PR any day now and a pre-release by start of next week.
That will be great! I'm building a mobile app that I want to remain connected as much as possible to react to events in real-time.
Since the initial connection behaves differently than auto-reconnection, it's a bit awkward for me and I have to do something like this:
using NATS.Client;
public class MyNatsConnectionWrapper : IDisposable
{
public bool Connected => Connection?.State == ConnState.CONNECTED;
public IConnection? Connection { get; private set; }
public MyNatsConnectionWrapper(Options options, CancellationToken cancelToken)
{
Task.Run(async () =>
{
while (!cancelToken.IsCancellationRequested)
{
try
{
Connection = new ConnectionFactory().CreateConnection(options);
}
catch (NATSConnectionException ex)
{
//LogThat(ex);
}
await Task.Delay(3333, cancelToken).SuppressCanceledException(cancelToken);
}
}, cancelToken);
}
public void Dispose() => Connection?.Dispose();
}
Yeah, it will be changed to this, adding a bool flag to the signature (bool reconnectOnConnect = true) to the create connection, true means reconnect on connect.
Connection = new ConnectionFactory().CreateConnection(options, true);
@ronnieoverby Fixed in https://github.com/nats-io/nats.net/pull/833 and pre-released https://github.com/nats-io/nats.net/releases/tag/1.1.1-pre1
@scottf I couldn't get this to work at all (package version 1.1.1)
My connection just sits in a Closed
state with no apparent attempt to ever connect again. I'll post a reproduction of the problem, but could you tell me if there are any other configuration required to get this new behavior?
@ronnieoverby Please get me some details and I will try to reproduce this. Do you know what kind of error or what is happening where the connection is broken? More details will definitely help me track this down.
@scottf Here you are, sir:
https://github.com/ronnieoverby/nats.net/blob/13e25e4f46b4232761243d1dd22c89c73036a617/src/ConsoleApp1/Program.cs
There aren't errors. It just never seems to connect. I left some comments in the repro program.
@ronnieoverby Fixed here: https://github.com/nats-io/nats.net/pull/854 I will have a pre-release available in about an hour. 1.1.2-pre3 Thanks so much for your help.
It took me a lot longer than expected to get a review. I'm just publishing it now.