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

ConnectivityStateManager Status notification problem #11615

Open
hlx502 opened this issue Oct 15, 2024 · 3 comments
Open

ConnectivityStateManager Status notification problem #11615

hlx502 opened this issue Oct 15, 2024 · 3 comments
Labels

Comments

@hlx502
Copy link
Contributor

hlx502 commented Oct 15, 2024

  1. Why does notifyWhenStateChanged() execute callback immediately when state != source?
    image
  2. Do we need to clear listeners for each state change?
    image
@ejona86
Copy link
Member

ejona86 commented Oct 15, 2024

From the Javadoc:

Registers a one-off callback that will be run if the connectivity state of the channel diverges from the given source, which is typically what has just been returned by getState(boolean). If the states are already different, the callback will be called immediately.

What do you hope the passed source is used for? source is what you think the current state is, and the callback is called when the channel is no longer that state. The API is inherently (and purposefully) racy, and accounts for that race by immediately notifying if source is out-of-date.

@hlx502
Copy link
Contributor Author

hlx502 commented Oct 16, 2024

Understood. Our client uses a domain name to connect to the server, but the server IP may change, such as in the k8s environment. When reconnecting through the automatic reconnection mechanism, the IP cannot be resolved based on the domain name. We will manually rebuild the channel based on its status, but it seems that notifyWhenStateChanged cannot be used. Do you have any suggestions on your end? Thanks

@ejona86
Copy link
Member

ejona86 commented Oct 16, 2024

the IP cannot be resolved based on the domain name

Why not? The client will re-resolve DNS when a connection is dropped/failed. So it will do the same as you're wanting to do.

If there's only one IP, then a regular k8s service should be fine; you wouldn't benefit from a headless service.

but it seems that notifyWhenStateChanged cannot be used

You can learn about TRANSIENT_FAILURE...

private void handleStateChange() {
  ConnectivityState state = channel.getState(false);
  if (state == TRANSIENT_FAILURE) {
    // react
  } else {
    channel.notifyWhenStateChanged(state, this::handleStateChange);
  }
}

channel.notifyWhenStateChanged(ConnectivityState.IDLE, this::handleStateChange);

Not that I'd encourage doing that to tear down the channel. If you do that, at the very least double-check that the address has actually changed before throwing away the TRANSIENT_FAILURE channel.

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

No branches or pull requests

2 participants