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

client don't try reconnect #169

Open
2547409617 opened this issue Feb 18, 2024 · 4 comments
Open

client don't try reconnect #169

2547409617 opened this issue Feb 18, 2024 · 4 comments
Labels
question Further information is requested

Comments

@2547409617
Copy link

hi,
I run ping pong example, first run server and then run client, all is ok.
I kill server, and run server again, client don't try reconnect.
I hope client will try reconnect if network is disconnect.

@hschneider
Copy link

Remove reconnect=5 from the example.

@lemunozm
Copy link
Owner

Hi, I guess you're using Tcp or FramedTcp right? The way it works is how TCP works.

TCP does know about reconnections. Once the channel is closed by one side (i.e, when you kill the server), the connection is considered closed for the other endpoint. If you want a reconnection mechanism, you should implement this on top of Tcp by yourself. i.e, trying to connect again after some incremental period or similar.

@lemunozm lemunozm added the question Further information is requested label Feb 19, 2024
@ReeceTrolley-HRS
Copy link

The way I got around this is to have the handler instantiation and listener.for_each into a loop block, you can then handle reconnection logic on exit.

e.g. the below will just wait 5 seconds, then try to reconnect

loop {
  let (handler, listener) = node::split::<()>();

  let server = handler.network().connect(...)?;

  listener.for_each(move |event| match event {
    NodeEvent::Network(net_event) => match net_event {
      NetEvent::Disconnected(_endpoint) => {
        // do anything you want to do before exiting the listener...
        handler.stop();
      }
      _ => {}
    }
  });

  // at this point, the client has disconnected, so do anything you want to do such as waiting a few seconds before reconnecting...
  std::thread::sleep(std::time::Duration::from_secs(5));
}

That way you can easily implement your own re-connection logic (exponential fallback, max # of retries etc.)

@lemunozm
Copy link
Owner

lemunozm commented Jan 7, 2025

Thanks for your solution!

In addition to the above answer, you can also use signals with a timer (send_with_timer) to create new connections inside the for_each and avoid disconnecting the listener.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants