Improve interceptor's rate metering key definition and lookup

This change replaces the hard coded strings used as keys in
interceptor rate-metering lookup maps.

Now, the fullMethodName defined in each bisq.proto.grpc.* class'
io.grpc.MethodDescriptor is used, not a hard coded String.

For example, the rate metering lookup key for 'GetBalances',
in 'GrpcWalletsService', is the

   fullMethodName = SERVICE_NAME + '/' + "GetBalances",

   where SERVICE_NAME = "io.bisq.protobuffer.Wallets".

Also adjusted affected tests, and tidy'd up interceptor logging.
This commit is contained in:
ghubstan 2021-02-28 17:10:51 -03:00
parent 19aed84910
commit 3f84246f59
No known key found for this signature in database
GPG Key ID: E35592D6800A861E
12 changed files with 83 additions and 71 deletions

View File

@ -32,6 +32,8 @@ import org.junit.jupiter.api.TestInfo;
import static bisq.apitest.config.BisqAppConfig.alicedaemon;
import static bisq.apitest.config.BisqAppConfig.arbdaemon;
import static bisq.apitest.config.BisqAppConfig.bobdaemon;
import static bisq.proto.grpc.DisputeAgentsGrpc.getRegisterDisputeAgentMethod;
import static bisq.proto.grpc.GetVersionGrpc.getGetVersionMethod;
import static java.net.InetAddress.getLoopbackAddress;
import static java.util.Arrays.stream;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
@ -150,14 +152,19 @@ public class ApiTestCase {
protected static File defaultRateMeterInterceptorConfig() {
GrpcServiceRateMeteringConfig.Builder builder = new GrpcServiceRateMeteringConfig.Builder();
builder.addCallRateMeter(GrpcVersionService.class.getSimpleName(),
"getVersion",
getGetVersionMethod().getFullMethodName(),
1,
SECONDS);
// Only GrpcVersionService is @VisibleForTesting, so we hardcode the class names.
// Only GrpcVersionService is @VisibleForTesting, so we need to
// hardcode other grpcServiceClassName parameter values used in
// builder.addCallRateMeter(...).
builder.addCallRateMeter("GrpcDisputeAgentsService",
"registerDisputeAgent",
getRegisterDisputeAgentMethod().getFullMethodName(),
10, // Same as default.
SECONDS);
// Define rate meters for non-existent method 'disabled', to override other grpc
// services' default rate meters -- defined in their rateMeteringInterceptor()
// methods.
String[] serviceClassNames = new String[]{
"GrpcGetTradeStatisticsService",
"GrpcHelpService",

View File

@ -2,7 +2,6 @@ package bisq.daemon.grpc;
import bisq.core.api.CoreApi;
import bisq.proto.grpc.DisputeAgentsGrpc;
import bisq.proto.grpc.RegisterDisputeAgentReply;
import bisq.proto.grpc.RegisterDisputeAgentRequest;
@ -17,6 +16,8 @@ import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import static bisq.daemon.grpc.interceptor.GrpcServiceRateMeteringConfig.getCustomRateMeteringInterceptor;
import static bisq.proto.grpc.DisputeAgentsGrpc.DisputeAgentsImplBase;
import static bisq.proto.grpc.DisputeAgentsGrpc.getRegisterDisputeAgentMethod;
import static java.util.concurrent.TimeUnit.SECONDS;
@ -25,7 +26,7 @@ import bisq.daemon.grpc.interceptor.CallRateMeteringInterceptor;
import bisq.daemon.grpc.interceptor.GrpcCallRateMeter;
@Slf4j
class GrpcDisputeAgentsService extends DisputeAgentsGrpc.DisputeAgentsImplBase {
class GrpcDisputeAgentsService extends DisputeAgentsImplBase {
private final CoreApi coreApi;
private final GrpcExceptionHandler exceptionHandler;
@ -61,7 +62,7 @@ class GrpcDisputeAgentsService extends DisputeAgentsGrpc.DisputeAgentsImplBase {
new HashMap<>() {{
// Do not limit devs' ability to test agent registration
// and call validation in regtest arbitration daemons.
put("registerDisputeAgent", new GrpcCallRateMeter(10, SECONDS));
put(getRegisterDisputeAgentMethod().getFullMethodName(), new GrpcCallRateMeter(10, SECONDS));
}}
)));
}

View File

@ -3,7 +3,6 @@ package bisq.daemon.grpc;
import bisq.core.api.CoreApi;
import bisq.core.trade.statistics.TradeStatistics3;
import bisq.proto.grpc.GetTradeStatisticsGrpc;
import bisq.proto.grpc.GetTradeStatisticsReply;
import bisq.proto.grpc.GetTradeStatisticsRequest;
@ -19,6 +18,8 @@ import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import static bisq.daemon.grpc.interceptor.GrpcServiceRateMeteringConfig.getCustomRateMeteringInterceptor;
import static bisq.proto.grpc.GetTradeStatisticsGrpc.GetTradeStatisticsImplBase;
import static bisq.proto.grpc.GetTradeStatisticsGrpc.getGetTradeStatisticsMethod;
import static java.util.concurrent.TimeUnit.SECONDS;
@ -27,7 +28,7 @@ import bisq.daemon.grpc.interceptor.CallRateMeteringInterceptor;
import bisq.daemon.grpc.interceptor.GrpcCallRateMeter;
@Slf4j
class GrpcGetTradeStatisticsService extends GetTradeStatisticsGrpc.GetTradeStatisticsImplBase {
class GrpcGetTradeStatisticsService extends GetTradeStatisticsImplBase {
private final CoreApi coreApi;
private final GrpcExceptionHandler exceptionHandler;
@ -64,7 +65,7 @@ class GrpcGetTradeStatisticsService extends GetTradeStatisticsGrpc.GetTradeStati
return getCustomRateMeteringInterceptor(coreApi.getConfig().appDataDir, this.getClass())
.or(() -> Optional.of(CallRateMeteringInterceptor.valueOf(
new HashMap<>() {{
put("getTradeStatistics", new GrpcCallRateMeter(1, SECONDS));
put(getGetTradeStatisticsMethod().getFullMethodName(), new GrpcCallRateMeter(1, SECONDS));
}}
)));
}

View File

@ -21,7 +21,6 @@ import bisq.core.api.CoreApi;
import bisq.proto.grpc.GetMethodHelpReply;
import bisq.proto.grpc.GetMethodHelpRequest;
import bisq.proto.grpc.HelpGrpc;
import io.grpc.ServerInterceptor;
import io.grpc.stub.StreamObserver;
@ -34,6 +33,8 @@ import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import static bisq.daemon.grpc.interceptor.GrpcServiceRateMeteringConfig.getCustomRateMeteringInterceptor;
import static bisq.proto.grpc.HelpGrpc.HelpImplBase;
import static bisq.proto.grpc.HelpGrpc.getGetMethodHelpMethod;
import static java.util.concurrent.TimeUnit.SECONDS;
@ -42,7 +43,7 @@ import bisq.daemon.grpc.interceptor.CallRateMeteringInterceptor;
import bisq.daemon.grpc.interceptor.GrpcCallRateMeter;
@Slf4j
class GrpcHelpService extends HelpGrpc.HelpImplBase {
class GrpcHelpService extends HelpImplBase {
private final CoreApi coreApi;
private final GrpcExceptionHandler exceptionHandler;
@ -76,7 +77,7 @@ class GrpcHelpService extends HelpGrpc.HelpImplBase {
return getCustomRateMeteringInterceptor(coreApi.getConfig().appDataDir, this.getClass())
.or(() -> Optional.of(CallRateMeteringInterceptor.valueOf(
new HashMap<>() {{
put("getMethodHelp", new GrpcCallRateMeter(1, SECONDS));
put(getGetMethodHelpMethod().getFullMethodName(), new GrpcCallRateMeter(1, SECONDS));
}}
)));
}

View File

@ -34,7 +34,6 @@ import bisq.proto.grpc.GetOfferReply;
import bisq.proto.grpc.GetOfferRequest;
import bisq.proto.grpc.GetOffersReply;
import bisq.proto.grpc.GetOffersRequest;
import bisq.proto.grpc.OffersGrpc;
import io.grpc.ServerInterceptor;
import io.grpc.stub.StreamObserver;
@ -50,6 +49,7 @@ import lombok.extern.slf4j.Slf4j;
import static bisq.core.api.model.OfferInfo.toOfferInfo;
import static bisq.daemon.grpc.interceptor.GrpcServiceRateMeteringConfig.getCustomRateMeteringInterceptor;
import static bisq.proto.grpc.OffersGrpc.*;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.SECONDS;
@ -59,7 +59,7 @@ import bisq.daemon.grpc.interceptor.CallRateMeteringInterceptor;
import bisq.daemon.grpc.interceptor.GrpcCallRateMeter;
@Slf4j
class GrpcOffersService extends OffersGrpc.OffersImplBase {
class GrpcOffersService extends OffersImplBase {
private final CoreApi coreApi;
private final GrpcExceptionHandler exceptionHandler;
@ -193,12 +193,12 @@ class GrpcOffersService extends OffersGrpc.OffersImplBase {
return getCustomRateMeteringInterceptor(coreApi.getConfig().appDataDir, this.getClass())
.or(() -> Optional.of(CallRateMeteringInterceptor.valueOf(
new HashMap<>() {{
put("getOffer", new GrpcCallRateMeter(1, SECONDS));
put("getMyOffer", new GrpcCallRateMeter(1, SECONDS));
put("getOffers", new GrpcCallRateMeter(1, SECONDS));
put("getMyOffers", new GrpcCallRateMeter(1, SECONDS));
put("createOffer", new GrpcCallRateMeter(1, MINUTES));
put("cancelOffer", new GrpcCallRateMeter(1, MINUTES));
put(getGetOfferMethod().getFullMethodName(), new GrpcCallRateMeter(1, SECONDS));
put(getGetMyOfferMethod().getFullMethodName(), new GrpcCallRateMeter(1, SECONDS));
put(getGetOffersMethod().getFullMethodName(), new GrpcCallRateMeter(1, SECONDS));
put(getGetMyOffersMethod().getFullMethodName(), new GrpcCallRateMeter(1, SECONDS));
put(getCreateOfferMethod().getFullMethodName(), new GrpcCallRateMeter(1, MINUTES));
put(getCancelOfferMethod().getFullMethodName(), new GrpcCallRateMeter(1, MINUTES));
}}
)));
}

View File

@ -29,7 +29,6 @@ import bisq.proto.grpc.GetPaymentAccountsReply;
import bisq.proto.grpc.GetPaymentAccountsRequest;
import bisq.proto.grpc.GetPaymentMethodsReply;
import bisq.proto.grpc.GetPaymentMethodsRequest;
import bisq.proto.grpc.PaymentAccountsGrpc;
import io.grpc.ServerInterceptor;
import io.grpc.stub.StreamObserver;
@ -43,6 +42,7 @@ import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import static bisq.daemon.grpc.interceptor.GrpcServiceRateMeteringConfig.getCustomRateMeteringInterceptor;
import static bisq.proto.grpc.PaymentAccountsGrpc.*;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.SECONDS;
@ -52,7 +52,7 @@ import bisq.daemon.grpc.interceptor.CallRateMeteringInterceptor;
import bisq.daemon.grpc.interceptor.GrpcCallRateMeter;
@Slf4j
class GrpcPaymentAccountsService extends PaymentAccountsGrpc.PaymentAccountsImplBase {
class GrpcPaymentAccountsService extends PaymentAccountsImplBase {
private final CoreApi coreApi;
private final GrpcExceptionHandler exceptionHandler;
@ -135,10 +135,10 @@ class GrpcPaymentAccountsService extends PaymentAccountsGrpc.PaymentAccountsImpl
return getCustomRateMeteringInterceptor(coreApi.getConfig().appDataDir, this.getClass())
.or(() -> Optional.of(CallRateMeteringInterceptor.valueOf(
new HashMap<>() {{
put("createPaymentAccount", new GrpcCallRateMeter(1, MINUTES));
put("getPaymentAccounts", new GrpcCallRateMeter(1, SECONDS));
put("getPaymentMethods", new GrpcCallRateMeter(1, SECONDS));
put("getPaymentAccountForm", new GrpcCallRateMeter(1, SECONDS));
put(getCreatePaymentAccountMethod().getFullMethodName(), new GrpcCallRateMeter(1, MINUTES));
put(getGetPaymentAccountsMethod().getFullMethodName(), new GrpcCallRateMeter(1, SECONDS));
put(getGetPaymentMethodsMethod().getFullMethodName(), new GrpcCallRateMeter(1, SECONDS));
put(getGetPaymentAccountFormMethod().getFullMethodName(), new GrpcCallRateMeter(1, SECONDS));
}}
)));
}

View File

@ -21,7 +21,6 @@ import bisq.core.api.CoreApi;
import bisq.proto.grpc.MarketPriceReply;
import bisq.proto.grpc.MarketPriceRequest;
import bisq.proto.grpc.PriceGrpc;
import io.grpc.ServerInterceptor;
import io.grpc.stub.StreamObserver;
@ -34,6 +33,8 @@ import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import static bisq.daemon.grpc.interceptor.GrpcServiceRateMeteringConfig.getCustomRateMeteringInterceptor;
import static bisq.proto.grpc.PriceGrpc.PriceImplBase;
import static bisq.proto.grpc.PriceGrpc.getGetMarketPriceMethod;
import static java.util.concurrent.TimeUnit.SECONDS;
@ -42,7 +43,7 @@ import bisq.daemon.grpc.interceptor.CallRateMeteringInterceptor;
import bisq.daemon.grpc.interceptor.GrpcCallRateMeter;
@Slf4j
class GrpcPriceService extends PriceGrpc.PriceImplBase {
class GrpcPriceService extends PriceImplBase {
private final CoreApi coreApi;
private final GrpcExceptionHandler exceptionHandler;
@ -78,7 +79,7 @@ class GrpcPriceService extends PriceGrpc.PriceImplBase {
return getCustomRateMeteringInterceptor(coreApi.getConfig().appDataDir, this.getClass())
.or(() -> Optional.of(CallRateMeteringInterceptor.valueOf(
new HashMap<>() {{
put("getMarketPrice", new GrpcCallRateMeter(1, SECONDS));
put(getGetMarketPriceMethod().getFullMethodName(), new GrpcCallRateMeter(1, SECONDS));
}}
)));
}

View File

@ -31,7 +31,6 @@ import bisq.proto.grpc.KeepFundsReply;
import bisq.proto.grpc.KeepFundsRequest;
import bisq.proto.grpc.TakeOfferReply;
import bisq.proto.grpc.TakeOfferRequest;
import bisq.proto.grpc.TradesGrpc;
import bisq.proto.grpc.WithdrawFundsReply;
import bisq.proto.grpc.WithdrawFundsRequest;
@ -47,6 +46,7 @@ import lombok.extern.slf4j.Slf4j;
import static bisq.core.api.model.TradeInfo.toTradeInfo;
import static bisq.daemon.grpc.interceptor.GrpcServiceRateMeteringConfig.getCustomRateMeteringInterceptor;
import static bisq.proto.grpc.TradesGrpc.*;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.SECONDS;
@ -56,7 +56,7 @@ import bisq.daemon.grpc.interceptor.CallRateMeteringInterceptor;
import bisq.daemon.grpc.interceptor.GrpcCallRateMeter;
@Slf4j
class GrpcTradesService extends TradesGrpc.TradesImplBase {
class GrpcTradesService extends TradesImplBase {
private final CoreApi coreApi;
private final GrpcExceptionHandler exceptionHandler;
@ -169,12 +169,12 @@ class GrpcTradesService extends TradesGrpc.TradesImplBase {
return getCustomRateMeteringInterceptor(coreApi.getConfig().appDataDir, this.getClass())
.or(() -> Optional.of(CallRateMeteringInterceptor.valueOf(
new HashMap<>() {{
put("getTrade", new GrpcCallRateMeter(1, SECONDS));
put("takeOffer", new GrpcCallRateMeter(1, MINUTES));
put("confirmPaymentStarted", new GrpcCallRateMeter(1, MINUTES));
put("confirmPaymentReceived", new GrpcCallRateMeter(1, MINUTES));
put("keepFunds", new GrpcCallRateMeter(1, MINUTES));
put("withdrawFunds", new GrpcCallRateMeter(1, MINUTES));
put(getGetTradeMethod().getFullMethodName(), new GrpcCallRateMeter(1, SECONDS));
put(getTakeOfferMethod().getFullMethodName(), new GrpcCallRateMeter(1, MINUTES));
put(getConfirmPaymentStartedMethod().getFullMethodName(), new GrpcCallRateMeter(1, MINUTES));
put(getConfirmPaymentReceivedMethod().getFullMethodName(), new GrpcCallRateMeter(1, MINUTES));
put(getKeepFundsMethod().getFullMethodName(), new GrpcCallRateMeter(1, MINUTES));
put(getWithdrawFundsMethod().getFullMethodName(), new GrpcCallRateMeter(1, MINUTES));
}}
)));
}

View File

@ -19,7 +19,6 @@ package bisq.daemon.grpc;
import bisq.core.api.CoreApi;
import bisq.proto.grpc.GetVersionGrpc;
import bisq.proto.grpc.GetVersionReply;
import bisq.proto.grpc.GetVersionRequest;
@ -36,6 +35,8 @@ import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import static bisq.daemon.grpc.interceptor.GrpcServiceRateMeteringConfig.getCustomRateMeteringInterceptor;
import static bisq.proto.grpc.GetVersionGrpc.GetVersionImplBase;
import static bisq.proto.grpc.GetVersionGrpc.getGetVersionMethod;
import static java.util.concurrent.TimeUnit.SECONDS;
@ -45,7 +46,7 @@ import bisq.daemon.grpc.interceptor.GrpcCallRateMeter;
@VisibleForTesting
@Slf4j
public class GrpcVersionService extends GetVersionGrpc.GetVersionImplBase {
public class GrpcVersionService extends GetVersionImplBase {
private final CoreApi coreApi;
private final GrpcExceptionHandler exceptionHandler;
@ -77,7 +78,7 @@ public class GrpcVersionService extends GetVersionGrpc.GetVersionImplBase {
return getCustomRateMeteringInterceptor(coreApi.getConfig().appDataDir, this.getClass())
.or(() -> Optional.of(CallRateMeteringInterceptor.valueOf(
new HashMap<>() {{
put("getVersion", new GrpcCallRateMeter(1, SECONDS));
put(getGetVersionMethod().getFullMethodName(), new GrpcCallRateMeter(1, SECONDS));
}}
)));
}

View File

@ -51,7 +51,6 @@ import bisq.proto.grpc.UnlockWalletReply;
import bisq.proto.grpc.UnlockWalletRequest;
import bisq.proto.grpc.UnsetTxFeeRatePreferenceReply;
import bisq.proto.grpc.UnsetTxFeeRatePreferenceRequest;
import bisq.proto.grpc.WalletsGrpc;
import io.grpc.ServerInterceptor;
import io.grpc.stub.StreamObserver;
@ -73,6 +72,7 @@ import org.jetbrains.annotations.NotNull;
import static bisq.core.api.model.TxInfo.toTxInfo;
import static bisq.daemon.grpc.interceptor.GrpcServiceRateMeteringConfig.getCustomRateMeteringInterceptor;
import static bisq.proto.grpc.WalletsGrpc.*;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.SECONDS;
@ -82,7 +82,7 @@ import bisq.daemon.grpc.interceptor.CallRateMeteringInterceptor;
import bisq.daemon.grpc.interceptor.GrpcCallRateMeter;
@Slf4j
class GrpcWalletsService extends WalletsGrpc.WalletsImplBase {
class GrpcWalletsService extends WalletsImplBase {
private final CoreApi coreApi;
private final GrpcExceptionHandler exceptionHandler;
@ -352,24 +352,24 @@ class GrpcWalletsService extends WalletsGrpc.WalletsImplBase {
return getCustomRateMeteringInterceptor(coreApi.getConfig().appDataDir, this.getClass())
.or(() -> Optional.of(CallRateMeteringInterceptor.valueOf(
new HashMap<>() {{
put("getBalances", new GrpcCallRateMeter(1, SECONDS));
put("getAddressBalance", new GrpcCallRateMeter(1, SECONDS));
put("getFundingAddresses", new GrpcCallRateMeter(1, SECONDS));
put("getUnusedBsqAddress", new GrpcCallRateMeter(1, SECONDS));
put("sendBsq", new GrpcCallRateMeter(1, MINUTES));
put("sendBtc", new GrpcCallRateMeter(1, MINUTES));
put("getTxFeeRate", new GrpcCallRateMeter(1, SECONDS));
put("setTxFeeRatePreference", new GrpcCallRateMeter(1, SECONDS));
put("unsetTxFeeRatePreference", new GrpcCallRateMeter(1, SECONDS));
put("getTransaction", new GrpcCallRateMeter(1, SECONDS));
put(getGetBalancesMethod().getFullMethodName(), new GrpcCallRateMeter(1, SECONDS));
put(getGetAddressBalanceMethod().getFullMethodName(), new GrpcCallRateMeter(1, SECONDS));
put(getGetFundingAddressesMethod().getFullMethodName(), new GrpcCallRateMeter(1, SECONDS));
put(getGetUnusedBsqAddressMethod().getFullMethodName(), new GrpcCallRateMeter(1, SECONDS));
put(getSendBsqMethod().getFullMethodName(), new GrpcCallRateMeter(1, MINUTES));
put(getSendBtcMethod().getFullMethodName(), new GrpcCallRateMeter(1, MINUTES));
put(getGetTxFeeRateMethod().getFullMethodName(), new GrpcCallRateMeter(1, SECONDS));
put(getSetTxFeeRatePreferenceMethod().getFullMethodName(), new GrpcCallRateMeter(1, SECONDS));
put(getUnsetTxFeeRatePreferenceMethod().getFullMethodName(), new GrpcCallRateMeter(1, SECONDS));
put(getGetTransactionMethod().getFullMethodName(), new GrpcCallRateMeter(1, SECONDS));
// Trying to set or remove a wallet password several times before the 1st attempt has time to
// persist the change to disk may corrupt the wallet, so allow only 1 attempt per 5 seconds.
put("setWalletPassword", new GrpcCallRateMeter(1, SECONDS, 5));
put("removeWalletPassword", new GrpcCallRateMeter(1, SECONDS, 5));
put(getSetWalletPasswordMethod().getFullMethodName(), new GrpcCallRateMeter(1, SECONDS, 5));
put(getRemoveWalletPasswordMethod().getFullMethodName(), new GrpcCallRateMeter(1, SECONDS, 5));
put("lockWallet", new GrpcCallRateMeter(1, SECONDS));
put("unlockWallet", new GrpcCallRateMeter(1, SECONDS));
put(getLockWalletMethod().getFullMethodName(), new GrpcCallRateMeter(1, SECONDS));
put(getUnlockWalletMethod().getFullMethodName(), new GrpcCallRateMeter(1, SECONDS));
}}
)));
}

View File

@ -34,7 +34,6 @@ import lombok.extern.slf4j.Slf4j;
import static io.grpc.Status.PERMISSION_DENIED;
import static java.lang.String.format;
import static java.util.stream.Collectors.joining;
import static org.apache.commons.lang3.StringUtils.uncapitalize;
@Slf4j
public final class CallRateMeteringInterceptor implements ServerInterceptor {
@ -85,16 +84,19 @@ public final class CallRateMeteringInterceptor implements ServerInterceptor {
ServerCall<?, ?> serverCall)
throws StatusRuntimeException {
String msg = getDefaultRateExceededError(methodName, rateMeter);
log.warn(StringUtils.capitalize(msg) + ".");
serverCall.close(PERMISSION_DENIED.withDescription(msg), new Metadata());
log.warn(msg + ".");
serverCall.close(PERMISSION_DENIED.withDescription(msg.toLowerCase()), new Metadata());
}
private String getDefaultRateExceededError(String methodName,
GrpcCallRateMeter rateMeter) {
// The derived method name may not be an exact match to CLI's method name.
String timeUnitName = StringUtils.chop(rateMeter.getTimeUnit().name().toLowerCase());
return format("the maximum allowed number of %s calls (%d/%s) has been exceeded",
methodName.toLowerCase(),
// Just print 'getversion', not the grpc method descriptor's
// full-method-name: 'io.bisq.protobuffer.getversion/getversion'.
String loggedMethodName = methodName.split("/")[1];
return format("The maximum allowed number of %s calls (%d/%s) has been exceeded",
loggedMethodName,
rateMeter.getAllowedCallsPerTimeWindow(),
timeUnitName);
}
@ -107,15 +109,10 @@ public final class CallRateMeteringInterceptor implements ServerInterceptor {
private String getRateMeterKey(ServerCall<?, ?> serverCall) {
// Get the rate meter map key from the server call method descriptor. The
// returned key (e.g., 'createOffer') is defined in the 'serviceCallRateMeters'
// constructor argument. It is extracted from the gRPC fullMethodName, e.g.,
// 'io.bisq.protobuffer.Offers/CreateOffer'.
String fullServiceMethodName = serverCall.getMethodDescriptor().getFullMethodName();
if (fullServiceMethodName.contains("/"))
return uncapitalize(fullServiceMethodName.split("/")[1]);
else
throw new IllegalStateException("Could not extract rate meter key from "
+ fullServiceMethodName + ".");
// returned String (e.g., 'io.bisq.protobuffer.Offers/CreateOffer') will match
// a map entry key in the 'serviceCallRateMeters' constructor argument, if it
// was defined in the Grpc*Service class' rateMeteringInterceptor method.
return serverCall.getMethodDescriptor().getFullMethodName();
}
@Override

View File

@ -55,8 +55,11 @@ public class GrpcCallRateMeter {
public String getCallsCountProgress(String calledMethodName) {
String shortTimeUnitName = StringUtils.chop(timeUnit.name().toLowerCase());
// Just print 'GetVersion has been called N times...',
// not 'io.bisq.protobuffer.GetVersion/GetVersion has been called N times...'
String loggedMethodName = calledMethodName.split("/")[1];
return format("%s has been called %d time%s in the last %s, rate limit is %d/%s",
calledMethodName,
loggedMethodName,
callTimestamps.size(),
callTimestamps.size() == 1 ? "" : "s",
shortTimeUnitName,