Gate based on destination rather than random.

This commit is contained in:
Jon Chambers 2021-01-15 17:48:09 -05:00 committed by Jon Chambers
parent affb219d72
commit 59eb6d10c1
2 changed files with 15 additions and 1 deletions

View File

@ -37,4 +37,12 @@ public class AmbiguousIdentifier {
public boolean hasNumber() { public boolean hasNumber() {
return number != null; return number != null;
} }
public int sendingGateHash() {
if (uuid != null) {
return (int)(uuid.getLeastSignificantBits() & 0xff);
} else {
return number.hashCode() & 0xff;
}
}
} }

View File

@ -124,7 +124,7 @@ public class MessageController {
@Valid IncomingMessageList messages) @Valid IncomingMessageList messages)
throws RateLimitExceededException throws RateLimitExceededException
{ {
if (random.nextDouble() <= getSuccessPercentage()) { if (shouldSend(destinationName)) {
if (!source.isPresent() && !accessKey.isPresent()) { if (!source.isPresent() && !accessKey.isPresent()) {
throw new WebApplicationException(Response.Status.UNAUTHORIZED); throw new WebApplicationException(Response.Status.UNAUTHORIZED);
} }
@ -199,6 +199,12 @@ public class MessageController {
} }
} }
private boolean shouldSend(final AmbiguousIdentifier destination) {
final double hash = destination.sendingGateHash();
return (hash / 255.0) <= getSuccessPercentage();
}
private double getSuccessPercentage() { private double getSuccessPercentage() {
if (featureFlagsManager.isFeatureFlagActive("SEND_MESSAGE_1_PERCENT")) { if (featureFlagsManager.isFeatureFlagActive("SEND_MESSAGE_1_PERCENT")) {
return 0.01; return 0.01;