Skip to content

Commit

Permalink
Add enabled instances filter before weight-random load balance
Browse files Browse the repository at this point in the history
  • Loading branch information
HaojunRen committed Aug 11, 2024
1 parent 98fbcfb commit 93624c7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,47 +39,47 @@ private List<Server> getServerList(Object key) {

@Override
public Server choose(Object key) {
List<Server> serverList = getServerList(key);

boolean isTriggered = false;

WeightFilterEntity strategyWeightFilterEntity = strategyWeightRandomLoadBalance.getT();
if (strategyWeightFilterEntity != null && strategyWeightFilterEntity.hasWeight()) {
isTriggered = true;

List<Server> serverList = getServerList(key);
boolean isWeightChecked = strategyWeightRandomLoadBalance.checkWeight(serverList, strategyWeightFilterEntity);
if (isWeightChecked) {
try {
List<Server> filterServerList = filterEnabledServers(serverList);

return strategyWeightRandomLoadBalance.choose(filterServerList, strategyWeightFilterEntity);
} catch (Exception e) {
return filterChoose(serverList, key);
return filterChoose(key);
}
} else {
return filterChoose(serverList, key);
return filterChoose(key);
}
}

if (!isTriggered) {
WeightFilterEntity ruleWeightFilterEntity = ruleWeightRandomLoadBalance.getT();
if (ruleWeightFilterEntity != null && ruleWeightFilterEntity.hasWeight()) {
List<Server> serverList = getServerList(key);
boolean isWeightChecked = ruleWeightRandomLoadBalance.checkWeight(serverList, ruleWeightFilterEntity);
if (isWeightChecked) {
try {
List<Server> filterServerList = filterEnabledServers(serverList);

return ruleWeightRandomLoadBalance.choose(filterServerList, ruleWeightFilterEntity);
} catch (Exception e) {
return filterChoose(serverList, key);
return filterChoose(key);
}
} else {
return filterChoose(serverList, key);
return filterChoose(key);
}
}
}

return filterChoose(serverList, key);
return filterChoose(key);
}

public List<Server> filterEnabledServers(List<Server> servers) {
Expand All @@ -93,7 +93,9 @@ public List<Server> filterEnabledServers(List<Server> servers) {
return serverList;
}

public Server filterChoose(List<Server> servers, Object key) {
public Server filterChoose(Object key) {
List<Server> servers = getLoadBalancer().getAllServers();

List<Server> serverList = filterEnabledServers(servers);

Optional<Server> server = getPredicate().chooseRoundRobinAfterFiltering(serverList, key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,47 +39,47 @@ private List<Server> getServerList(Object key) {

@Override
public Server choose(Object key) {
List<Server> serverList = getServerList(key);

boolean isTriggered = false;

WeightFilterEntity strategyWeightFilterEntity = strategyWeightRandomLoadBalance.getT();
if (strategyWeightFilterEntity != null && strategyWeightFilterEntity.hasWeight()) {
isTriggered = true;

List<Server> serverList = getServerList(key);
boolean isWeightChecked = strategyWeightRandomLoadBalance.checkWeight(serverList, strategyWeightFilterEntity);
if (isWeightChecked) {
try {
List<Server> filterServerList = filterEnabledServers(serverList);

return strategyWeightRandomLoadBalance.choose(filterServerList, strategyWeightFilterEntity);
} catch (Exception e) {
return filterChoose(serverList, key);
return filterChoose(key);
}
} else {
return filterChoose(serverList, key);
return filterChoose(key);
}
}

if (!isTriggered) {
WeightFilterEntity ruleWeightFilterEntity = ruleWeightRandomLoadBalance.getT();
if (ruleWeightFilterEntity != null && ruleWeightFilterEntity.hasWeight()) {
List<Server> serverList = getServerList(key);
boolean isWeightChecked = ruleWeightRandomLoadBalance.checkWeight(serverList, ruleWeightFilterEntity);
if (isWeightChecked) {
try {
List<Server> filterServerList = filterEnabledServers(serverList);

return ruleWeightRandomLoadBalance.choose(filterServerList, ruleWeightFilterEntity);
} catch (Exception e) {
return filterChoose(serverList, key);
return filterChoose(key);
}
} else {
return filterChoose(serverList, key);
return filterChoose(key);
}
}
}

return filterChoose(serverList, key);
return filterChoose(key);
}

public List<Server> filterEnabledServers(List<Server> servers) {
Expand All @@ -93,7 +93,9 @@ public List<Server> filterEnabledServers(List<Server> servers) {
return serverList;
}

public Server filterChoose(List<Server> servers, Object key) {
public Server filterChoose(Object key) {
List<Server> servers = getLoadBalancer().getAllServers();

List<Server> serverList = filterEnabledServers(servers);

Optional<Server> server = getPredicate().chooseRoundRobinAfterFiltering(serverList, key);
Expand Down

0 comments on commit 93624c7

Please sign in to comment.