Skip to content

Commit

Permalink
Refactor name server address update logic
Browse files Browse the repository at this point in the history
- Simplified the logic for checking if the name server address list needs to be updated.
- Moved channel closure logic into a new method `handleChannelClosureIfNeeded`.
- Improved maintainability and readability of the `updateNameServerAddressList` method.

No functional changes, just refactoring for better clarity and maintainability.
  • Loading branch information
asapple committed Dec 10, 2024
1 parent 564e55e commit 4f0c8ff
Showing 1 changed file with 56 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -500,41 +500,64 @@ public void closeChannel(final Channel channel) {
}

@Override
public void updateNameServerAddressList(List<String> addrs) {
List<String> old = this.namesrvAddrList.get();
boolean update = false;

if (!addrs.isEmpty()) {
if (null == old) {
update = true;
} else if (addrs.size() != old.size()) {
update = true;
} else {
for (String addr : addrs) {
if (!old.contains(addr)) {
update = true;
break;
}
}
public void updateNameServerAddressList(List<String> newAddresses) {
List<String> oldAddresses = this.namesrvAddrList.get();

// Check if the address list needs to be updated
if (shouldUpdateAddressList(newAddresses, oldAddresses)) {
Collections.shuffle(newAddresses);
LOGGER.info("name server address updated. NEW : {} , OLD: {}", newAddresses, oldAddresses);
this.namesrvAddrList.set(newAddresses);

// Handle channel closure if the chosen address is not in the new list
handleChannelClosureIfNeeded(newAddresses);
}
}

/**
* Check if the address list should be updated
*/
private static boolean shouldUpdateAddressList(List<String> newAddresses, List<String> oldAddresses) {
if (newAddresses.isEmpty()) {
return false;
}

if (oldAddresses == null || newAddresses.size() != oldAddresses.size()) {
return true;
}

for (String addr : newAddresses) {
if (!oldAddresses.contains(addr)) {
return true;
}
}

if (update) {
Collections.shuffle(addrs);
LOGGER.info("name server address updated. NEW : {} , OLD: {}", addrs, old);
this.namesrvAddrList.set(addrs);

// should close the channel if choosed addr is not exist.
String chosenNameServerAddr = this.namesrvAddrChoosed.get();
if (chosenNameServerAddr != null && !addrs.contains(chosenNameServerAddr)) {
namesrvAddrChoosed.compareAndSet(chosenNameServerAddr, null);
for (String addr : this.channelTables.keySet()) {
if (addr.contains(chosenNameServerAddr)) {
ChannelWrapper channelWrapper = this.channelTables.get(addr);
if (channelWrapper != null) {
channelWrapper.close();
}
}
}
return false;
}

/**
* Handle channel closure if the chosen address is no longer in the new list
*/
private void handleChannelClosureIfNeeded(List<String> newAddresses) {
String chosenNameServerAddr = this.namesrvAddrChoosed.get();
if (chosenNameServerAddr != null && !newAddresses.contains(chosenNameServerAddr)) {
// Set the chosen address to null
namesrvAddrChoosed.compareAndSet(chosenNameServerAddr, null);

// Close the channels associated with the chosen address
closeChannelsForAddress(chosenNameServerAddr);
}
}

/**
* Close channels associated with the given address
*/
private void closeChannelsForAddress(String chosenNameServerAddr) {
for (String addr : this.channelTables.keySet()) {
if (addr.contains(chosenNameServerAddr)) {
ChannelWrapper channelWrapper = this.channelTables.get(addr);
if (channelWrapper != null) {
channelWrapper.close();
}
}
}
Expand Down

0 comments on commit 4f0c8ff

Please sign in to comment.