mirror of
https://github.com/bisq-network/bisq.git
synced 2025-02-22 22:45:21 +01:00
Fix GrpcCallRateMeter method and variable name
- Change method isAllowed() -> checkAndIncrement(). - Change variable allowedCallsPerTimeUnit -> allowedCallsPerTimeWindow.
This commit is contained in:
parent
b8c5a29659
commit
10727fc083
4 changed files with 26 additions and 26 deletions
|
@ -66,7 +66,7 @@ public final class CallRateMeteringInterceptor implements ServerInterceptor {
|
|||
String methodName = rateMeterKV.getKey();
|
||||
GrpcCallRateMeter rateMeter = rateMeterKV.getValue();
|
||||
|
||||
if (!rateMeter.isAllowed())
|
||||
if (!rateMeter.checkAndIncrement())
|
||||
handlePermissionDeniedWarningAndCloseCall(methodName, rateMeter, serverCall);
|
||||
else
|
||||
log.info(rateMeter.getCallsCountProgress(methodName));
|
||||
|
@ -94,7 +94,7 @@ public final class CallRateMeteringInterceptor implements ServerInterceptor {
|
|||
String timeUnitName = StringUtils.chop(rateMeter.getTimeUnit().name().toLowerCase());
|
||||
return format("the maximum allowed number of %s calls (%d/%s) has been exceeded",
|
||||
methodName.toLowerCase(),
|
||||
rateMeter.getAllowedCallsPerTimeUnit(),
|
||||
rateMeter.getAllowedCallsPerTimeWindow(),
|
||||
timeUnitName);
|
||||
}
|
||||
|
||||
|
|
|
@ -16,7 +16,7 @@ import static java.lang.System.currentTimeMillis;
|
|||
public class GrpcCallRateMeter {
|
||||
|
||||
@Getter
|
||||
private final int allowedCallsPerTimeUnit;
|
||||
private final int allowedCallsPerTimeWindow;
|
||||
@Getter
|
||||
private final TimeUnit timeUnit;
|
||||
@Getter
|
||||
|
@ -27,20 +27,20 @@ public class GrpcCallRateMeter {
|
|||
|
||||
private transient final ArrayDeque<Long> callTimestamps;
|
||||
|
||||
public GrpcCallRateMeter(int allowedCallsPerTimeUnit, TimeUnit timeUnit) {
|
||||
this(allowedCallsPerTimeUnit, timeUnit, 1);
|
||||
public GrpcCallRateMeter(int allowedCallsPerTimeWindow, TimeUnit timeUnit) {
|
||||
this(allowedCallsPerTimeWindow, timeUnit, 1);
|
||||
}
|
||||
|
||||
public GrpcCallRateMeter(int allowedCallsPerTimeUnit, TimeUnit timeUnit, int numTimeUnits) {
|
||||
this.allowedCallsPerTimeUnit = allowedCallsPerTimeUnit;
|
||||
public GrpcCallRateMeter(int allowedCallsPerTimeWindow, TimeUnit timeUnit, int numTimeUnits) {
|
||||
this.allowedCallsPerTimeWindow = allowedCallsPerTimeWindow;
|
||||
this.timeUnit = timeUnit;
|
||||
this.numTimeUnits = numTimeUnits;
|
||||
this.timeUnitIntervalInMilliseconds = timeUnit.toMillis(1) * numTimeUnits;
|
||||
this.callTimestamps = new ArrayDeque<>();
|
||||
}
|
||||
|
||||
public boolean isAllowed() {
|
||||
if (getCallsCount() < allowedCallsPerTimeUnit) {
|
||||
public boolean checkAndIncrement() {
|
||||
if (getCallsCount() < allowedCallsPerTimeWindow) {
|
||||
incrementCallsCount();
|
||||
return true;
|
||||
} else {
|
||||
|
@ -60,7 +60,7 @@ public class GrpcCallRateMeter {
|
|||
callTimestamps.size(),
|
||||
callTimestamps.size() == 1 ? "" : "s",
|
||||
shortTimeUnitName,
|
||||
allowedCallsPerTimeUnit,
|
||||
allowedCallsPerTimeWindow,
|
||||
shortTimeUnitName);
|
||||
}
|
||||
|
||||
|
@ -83,7 +83,7 @@ public class GrpcCallRateMeter {
|
|||
@Override
|
||||
public String toString() {
|
||||
return "GrpcCallRateMeter{" +
|
||||
"allowedCallsPerTimeUnit=" + allowedCallsPerTimeUnit +
|
||||
"allowedCallsPerTimeWindow=" + allowedCallsPerTimeWindow +
|
||||
", timeUnit=" + timeUnit.name() +
|
||||
", timeUnitIntervalInMilliseconds=" + timeUnitIntervalInMilliseconds +
|
||||
", callsCount=" + callTimestamps.size() +
|
||||
|
|
|
@ -57,7 +57,7 @@ public class GrpcServiceRateMeteringConfig {
|
|||
|
||||
private static final String KEY_GRPC_SERVICE_CLASS_NAME = "grpcServiceClassName";
|
||||
private static final String KEY_METHOD_RATE_METERS = "methodRateMeters";
|
||||
private static final String KEY_ALLOWED_CALL_PER_TIME_UNIT = "allowedCallsPerTimeUnit";
|
||||
private static final String KEY_ALLOWED_CALL_PER_TIME_WINDOW = "allowedCallsPerTimeWindow";
|
||||
private static final String KEY_TIME_UNIT = "timeUnit";
|
||||
private static final String KEY_NUM_TIME_UNITS = "numTimeUnits";
|
||||
|
||||
|
@ -182,10 +182,10 @@ public class GrpcServiceRateMeteringConfig {
|
|||
@SuppressWarnings("unchecked")
|
||||
private static GrpcCallRateMeter getGrpcCallRateMeter(Map.Entry<String, Object> gsonEntry) {
|
||||
Map<String, Object> valueMap = (Map<String, Object>) gsonEntry.getValue();
|
||||
int allowedCallsPerTimeUnit = ((Number) valueMap.get(KEY_ALLOWED_CALL_PER_TIME_UNIT)).intValue();
|
||||
int allowedCallsPerTimeWindow = ((Number) valueMap.get(KEY_ALLOWED_CALL_PER_TIME_WINDOW)).intValue();
|
||||
TimeUnit timeUnit = TimeUnit.valueOf((String) valueMap.get(KEY_TIME_UNIT));
|
||||
int numTimeUnits = ((Number) valueMap.get(KEY_NUM_TIME_UNITS)).intValue();
|
||||
return new GrpcCallRateMeter(allowedCallsPerTimeUnit, timeUnit, numTimeUnits);
|
||||
return new GrpcCallRateMeter(allowedCallsPerTimeWindow, timeUnit, numTimeUnits);
|
||||
}
|
||||
|
||||
private static void verifyConfigFile(File configFile) {
|
||||
|
|
|
@ -107,19 +107,19 @@ public class GrpcServiceRateMeteringConfigTest {
|
|||
|
||||
// Check the rate meter config.
|
||||
GrpcCallRateMeter rateMeter = versionServiceInterceptor.serviceCallRateMeters.get("getVersion");
|
||||
assertEquals(3, rateMeter.getAllowedCallsPerTimeUnit());
|
||||
assertEquals(3, rateMeter.getAllowedCallsPerTimeWindow());
|
||||
assertEquals(SECONDS, rateMeter.getTimeUnit());
|
||||
assertEquals(2, rateMeter.getNumTimeUnits());
|
||||
assertEquals(2 * 1000, rateMeter.getTimeUnitIntervalInMilliseconds());
|
||||
|
||||
// Do as many calls as allowed within rateMeter.getTimeUnitIntervalInMilliseconds().
|
||||
doMaxIsAllowedChecks(true,
|
||||
rateMeter.getAllowedCallsPerTimeUnit(),
|
||||
rateMeter.getAllowedCallsPerTimeWindow(),
|
||||
rateMeter);
|
||||
|
||||
// The next 3 calls are blocked because we've exceeded the 3calls/2s limit.
|
||||
doMaxIsAllowedChecks(false,
|
||||
rateMeter.getAllowedCallsPerTimeUnit(),
|
||||
rateMeter.getAllowedCallsPerTimeWindow(),
|
||||
rateMeter);
|
||||
|
||||
// Let all of the rate meter's cached call timestamps become stale by waiting for
|
||||
|
@ -129,37 +129,37 @@ public class GrpcServiceRateMeteringConfigTest {
|
|||
assertEquals(0, rateMeter.getCallsCount());
|
||||
|
||||
doMaxIsAllowedChecks(true,
|
||||
rateMeter.getAllowedCallsPerTimeUnit(),
|
||||
rateMeter.getAllowedCallsPerTimeWindow(),
|
||||
rateMeter);
|
||||
// We've exceeded the call/second limit.
|
||||
assertFalse(rateMeter.isAllowed());
|
||||
assertFalse(rateMeter.checkAndIncrement());
|
||||
|
||||
// Let all of the call timestamps go stale again by waiting for 2001 ms.
|
||||
rest(1 + rateMeter.getTimeUnitIntervalInMilliseconds());
|
||||
|
||||
// Call twice, resting 0.5s after each call.
|
||||
for (int i = 0; i < 2; i++) {
|
||||
assertTrue(rateMeter.isAllowed());
|
||||
assertTrue(rateMeter.checkAndIncrement());
|
||||
rest(500);
|
||||
}
|
||||
// Call the 3rd time, then let one of the rate meter's timestamps go stale.
|
||||
assertTrue(rateMeter.isAllowed());
|
||||
assertTrue(rateMeter.checkAndIncrement());
|
||||
rest(1001);
|
||||
|
||||
// The call count was decremented by one because one timestamp went stale.
|
||||
assertEquals(2, rateMeter.getCallsCount());
|
||||
assertTrue(rateMeter.isAllowed());
|
||||
assertEquals(rateMeter.getAllowedCallsPerTimeUnit(), rateMeter.getCallsCount());
|
||||
assertTrue(rateMeter.checkAndIncrement());
|
||||
assertEquals(rateMeter.getAllowedCallsPerTimeWindow(), rateMeter.getCallsCount());
|
||||
|
||||
// We've exceeded the call limit again.
|
||||
assertFalse(rateMeter.isAllowed());
|
||||
assertFalse(rateMeter.checkAndIncrement());
|
||||
}
|
||||
|
||||
private void doMaxIsAllowedChecks(boolean expectedIsAllowed,
|
||||
int expectedCallsCount,
|
||||
GrpcCallRateMeter rateMeter) {
|
||||
for (int i = 1; i <= rateMeter.getAllowedCallsPerTimeUnit(); i++) {
|
||||
assertEquals(expectedIsAllowed, rateMeter.isAllowed());
|
||||
for (int i = 1; i <= rateMeter.getAllowedCallsPerTimeWindow(); i++) {
|
||||
assertEquals(expectedIsAllowed, rateMeter.checkAndIncrement());
|
||||
}
|
||||
assertEquals(expectedCallsCount, rateMeter.getCallsCount());
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue