nats.net icon indicating copy to clipboard operation
nats.net copied to clipboard

Add RetryOnFailedConnect

Open ColinSullivan1 opened this issue 3 years ago • 10 comments

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;

ColinSullivan1 avatar Mar 04 '21 20:03 ColinSullivan1

So, is this feature coming to the library? Are there commits adding this?

ronnieoverby avatar Oct 25 '23 20:10 ronnieoverby

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.

scottf avatar Oct 26 '23 00:10 scottf

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();
}

ronnieoverby avatar Oct 26 '23 12:10 ronnieoverby

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);

scottf avatar Oct 26 '23 14:10 scottf

@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 avatar Oct 30 '23 14:10 scottf

@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 avatar Jan 15 '24 22:01 ronnieoverby

@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 avatar Jan 16 '24 13:01 scottf

@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 avatar Jan 16 '24 15:01 ronnieoverby

@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.

scottf avatar Jan 16 '24 17:01 scottf

It took me a lot longer than expected to get a review. I'm just publishing it now.

scottf avatar Jan 16 '24 21:01 scottf