Skip to content
This repository has been archived by the owner on Jul 17, 2023. It is now read-only.

Commit

Permalink
GUACAMOLE-102: Make getConnectionWeight return int, clean up compare …
Browse files Browse the repository at this point in the history
…code.
  • Loading branch information
necouchman committed Jun 5, 2017
1 parent aa4c134 commit f77c507
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 18 deletions.
Expand Up @@ -184,16 +184,18 @@ public void setConnectionWeight(Integer connectionWeight) {

/**
* Returns the connection weight used in calculating the
* WRR algorithm.
* weighted algorithms.
*
* @return
* The connection weight. Null indicates no weight has been set,
* -1 indicates that the system is unavailable.
* The connection weight as an int. If the weight is
* null a default weight of 1 is returned. Zero and
* negative numbers are used to indicate the system is
* unavailable.
*/
public Integer getConnectionWeight() {
public int getConnectionWeight() {
if (connectionWeight == null)
return 1;
return connectionWeight;
return connectionWeight.intValue();
}

/**
Expand Down
Expand Up @@ -418,7 +418,7 @@ public GuacamoleProxyConfiguration getGuacamoleProxyConfiguration()
* The weight of the connection.
*
*/
public Integer getConnectionWeight() {
public int getConnectionWeight() {

// Return the connection weight
return getModel().getConnectionWeight();
Expand Down
Expand Up @@ -23,7 +23,6 @@
import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -188,15 +187,8 @@ protected ModeledConnection acquire(RemoteAuthenticatedUser user,
@Override
public int compare(ModeledConnection a, ModeledConnection b) {

// Get connection weight for the two systems being compared.
int weightA = a.getConnectionWeight().intValue();
int weightB = b.getConnectionWeight().intValue();

// Get current active connections, add 1 to both to avoid calculations with 0.
int connsA = getActiveConnections(a).size() + 1;
int connsB = getActiveConnections(b).size() + 1;

return (connsA * weightB) - (connsB * weightA);
return ((getActiveConnections(a).size() + 1) * b.getConnectionWeight() -
(getActiveConnections(b).size() + 1) * a.getConnectionWeight());

}

Expand All @@ -209,8 +201,8 @@ public int compare(ModeledConnection a, ModeledConnection b) {
for (ModeledConnection connection : sortedConnections) {

// If connection weight is zero or negative, this host is disabled and should not be used.
if (connection.getConnectionWeight() != null && connection.getConnectionWeight().intValue() < 1) {
logger.warn("Weight for {} is non-null and < 1, connection will be skipped.", connection.getName());
if (connection.getConnectionWeight() < 1) {
logger.warn("Weight for {} is < 1, connection will be skipped.", connection.getName());
continue;
}

Expand Down

0 comments on commit f77c507

Please sign in to comment.