Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RetryOnFailedConnect #443

Closed
ColinSullivan1 opened this issue Mar 4, 2021 · 10 comments
Closed

Add RetryOnFailedConnect #443

ColinSullivan1 opened this issue Mar 4, 2021 · 10 comments

Comments

@ColinSullivan1
Copy link
Member

Currently, initial connect behavior attempts the server list and then gives up. Traverse the connection list and retry honoring reconnect attempt values.

See: nats-io/nats.go#581

Option to mirror go:
Options.RetryOnFailedConnect = true;

@ColinSullivan1 ColinSullivan1 changed the title Traverse connection list on connect API Add RetryOnFailedConnect Mar 4, 2021
@scottf scottf closed this as completed Oct 17, 2023
@ronnieoverby
Copy link

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

@scottf
Copy link
Collaborator

scottf commented Oct 26, 2023

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 scottf reopened this Oct 26, 2023
@ronnieoverby
Copy link

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

@scottf
Copy link
Collaborator

scottf commented Oct 26, 2023

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
Copy link
Collaborator

scottf commented Oct 30, 2023

@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

@ronnieoverby
Copy link

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

@scottf
Copy link
Collaborator

scottf commented Jan 16, 2024

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

@ronnieoverby
Copy link

ronnieoverby commented Jan 16, 2024

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

@scottf
Copy link
Collaborator

scottf commented Jan 16, 2024

@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
Copy link
Collaborator

scottf commented Jan 16, 2024

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

@scottf scottf closed this as completed Jun 12, 2024
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

No branches or pull requests

3 participants