GUACAMOLE-102: Make getConnectionWeight return int, clean up compare code.

This commit is contained in:
Nick Couchman
2017-06-02 20:05:15 -04:00
committed by Nick Couchman
parent aa4c134922
commit f77c50730d
3 changed files with 12 additions and 18 deletions

View File

@@ -184,16 +184,18 @@ public class ConnectionModel extends ChildObjectModel {
/** /**
* Returns the connection weight used in calculating the * Returns the connection weight used in calculating the
* WRR algorithm. * weighted algorithms.
* *
* @return * @return
* The connection weight. Null indicates no weight has been set, * The connection weight as an int. If the weight is
* -1 indicates that the system is unavailable. * 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) if (connectionWeight == null)
return 1; return 1;
return connectionWeight; return connectionWeight.intValue();
} }
/** /**

View File

@@ -418,7 +418,7 @@ public class ModeledConnection extends ModeledChildDirectoryObject<ConnectionMod
* The weight of the connection. * The weight of the connection.
* *
*/ */
public Integer getConnectionWeight() { public int getConnectionWeight() {
// Return the connection weight // Return the connection weight
return getModel().getConnectionWeight(); return getModel().getConnectionWeight();

View File

@@ -23,7 +23,6 @@ import com.google.common.collect.ConcurrentHashMultiset;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.Singleton; import com.google.inject.Singleton;
import java.util.Arrays; import java.util.Arrays;
import java.util.Iterator;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@@ -188,15 +187,8 @@ public class RestrictedGuacamoleTunnelService
@Override @Override
public int compare(ModeledConnection a, ModeledConnection b) { public int compare(ModeledConnection a, ModeledConnection b) {
// Get connection weight for the two systems being compared. return ((getActiveConnections(a).size() + 1) * b.getConnectionWeight() -
int weightA = a.getConnectionWeight().intValue(); (getActiveConnections(b).size() + 1) * a.getConnectionWeight());
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);
} }
@@ -209,8 +201,8 @@ public class RestrictedGuacamoleTunnelService
for (ModeledConnection connection : sortedConnections) { for (ModeledConnection connection : sortedConnections) {
// If connection weight is zero or negative, this host is disabled and should not be used. // If connection weight is zero or negative, this host is disabled and should not be used.
if (connection.getConnectionWeight() != null && connection.getConnectionWeight().intValue() < 1) { if (connection.getConnectionWeight() < 1) {
logger.warn("Weight for {} is non-null and < 1, connection will be skipped.", connection.getName()); logger.warn("Weight for {} is < 1, connection will be skipped.", connection.getName());
continue; continue;
} }