Factor out repeated 'new CallRateMeteringInterceptor' calls

Move this into a static CallRateMeteringInterceptor.valueOf(Map) method.
This commit is contained in:
ghubstan 2021-01-25 14:38:53 -03:00
parent 4eed44d350
commit c99624015b
No known key found for this signature in database
GPG Key ID: E35592D6800A861E
10 changed files with 84 additions and 86 deletions

View File

@ -56,14 +56,13 @@ class GrpcDisputeAgentsService extends DisputeAgentsGrpc.DisputeAgentsImplBase {
}
final Optional<ServerInterceptor> rateMeteringInterceptor() {
CallRateMeteringInterceptor defaultCallRateMeteringInterceptor =
new CallRateMeteringInterceptor(new HashMap<>() {{
// You can only register mainnet dispute agents in the UI.
// Do not limit devs' ability to register test agents.
put("registerDisputeAgent", new GrpcCallRateMeter(1, SECONDS));
}});
return getCustomRateMeteringInterceptor(coreApi.getConfig().appDataDir, this.getClass())
.or(() -> Optional.of(defaultCallRateMeteringInterceptor));
.or(() -> Optional.of(CallRateMeteringInterceptor.valueOf(
new HashMap<>() {{
// You can only register mainnet dispute agents in the UI.
// Do not limit devs' ability to register test agents.
put("registerDisputeAgent", new GrpcCallRateMeter(1, SECONDS));
}}
)));
}
}

View File

@ -58,12 +58,11 @@ class GrpcGetTradeStatisticsService extends GetTradeStatisticsGrpc.GetTradeStati
}
final Optional<ServerInterceptor> rateMeteringInterceptor() {
CallRateMeteringInterceptor defaultCallRateMeteringInterceptor =
new CallRateMeteringInterceptor(new HashMap<>() {{
put("getTradeStatistics", new GrpcCallRateMeter(1, SECONDS));
}});
return getCustomRateMeteringInterceptor(coreApi.getConfig().appDataDir, this.getClass())
.or(() -> Optional.of(defaultCallRateMeteringInterceptor));
.or(() -> Optional.of(CallRateMeteringInterceptor.valueOf(
new HashMap<>() {{
put("getTradeStatistics", new GrpcCallRateMeter(1, SECONDS));
}}
)));
}
}

View File

@ -73,12 +73,11 @@ class GrpcHelpService extends HelpGrpc.HelpImplBase {
}
final Optional<ServerInterceptor> rateMeteringInterceptor() {
CallRateMeteringInterceptor defaultCallRateMeteringInterceptor =
new CallRateMeteringInterceptor(new HashMap<>() {{
put("getMethodHelp", new GrpcCallRateMeter(1, SECONDS));
}});
return getCustomRateMeteringInterceptor(coreApi.getConfig().appDataDir, this.getClass())
.or(() -> Optional.of(defaultCallRateMeteringInterceptor));
.or(() -> Optional.of(CallRateMeteringInterceptor.valueOf(
new HashMap<>() {{
put("getMethodHelp", new GrpcCallRateMeter(1, SECONDS));
}}
)));
}
}

View File

@ -190,17 +190,16 @@ class GrpcOffersService extends OffersGrpc.OffersImplBase {
}
final Optional<ServerInterceptor> rateMeteringInterceptor() {
CallRateMeteringInterceptor defaultCallRateMeteringInterceptor =
new CallRateMeteringInterceptor(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));
}});
return getCustomRateMeteringInterceptor(coreApi.getConfig().appDataDir, this.getClass())
.or(() -> Optional.of(defaultCallRateMeteringInterceptor));
.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));
}}
)));
}
}

View File

@ -130,15 +130,14 @@ class GrpcPaymentAccountsService extends PaymentAccountsGrpc.PaymentAccountsImpl
}
final Optional<ServerInterceptor> rateMeteringInterceptor() {
CallRateMeteringInterceptor defaultCallRateMeteringInterceptor =
new CallRateMeteringInterceptor(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));
}});
return getCustomRateMeteringInterceptor(coreApi.getConfig().appDataDir, this.getClass())
.or(() -> Optional.of(defaultCallRateMeteringInterceptor));
.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));
}}
)));
}
}

View File

@ -75,12 +75,11 @@ class GrpcPriceService extends PriceGrpc.PriceImplBase {
}
final Optional<ServerInterceptor> rateMeteringInterceptor() {
CallRateMeteringInterceptor defaultCallRateMeteringInterceptor =
new CallRateMeteringInterceptor(new HashMap<>() {{
put("getMarketPrice", new GrpcCallRateMeter(1, SECONDS));
}});
return getCustomRateMeteringInterceptor(coreApi.getConfig().appDataDir, this.getClass())
.or(() -> Optional.of(defaultCallRateMeteringInterceptor));
.or(() -> Optional.of(CallRateMeteringInterceptor.valueOf(
new HashMap<>() {{
put("getMarketPrice", new GrpcCallRateMeter(1, SECONDS));
}}
)));
}
}

View File

@ -162,17 +162,16 @@ class GrpcTradesService extends TradesGrpc.TradesImplBase {
}
final Optional<ServerInterceptor> rateMeteringInterceptor() {
CallRateMeteringInterceptor defaultCallRateMeteringInterceptor =
new CallRateMeteringInterceptor(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));
}});
return getCustomRateMeteringInterceptor(coreApi.getConfig().appDataDir, this.getClass())
.or(() -> Optional.of(defaultCallRateMeteringInterceptor));
.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));
}}
)));
}
}

View File

@ -74,12 +74,11 @@ public class GrpcVersionService extends GetVersionGrpc.GetVersionImplBase {
}
final Optional<ServerInterceptor> rateMeteringInterceptor() {
CallRateMeteringInterceptor defaultCallRateMeteringInterceptor =
new CallRateMeteringInterceptor(new HashMap<>() {{
put("getVersion", new GrpcCallRateMeter(1, SECONDS));
}});
return getCustomRateMeteringInterceptor(coreApi.getConfig().appDataDir, this.getClass())
.or(() -> Optional.of(defaultCallRateMeteringInterceptor));
.or(() -> Optional.of(CallRateMeteringInterceptor.valueOf(
new HashMap<>() {{
put("getVersion", new GrpcCallRateMeter(1, SECONDS));
}}
)));
}
}

View File

@ -349,29 +349,28 @@ class GrpcWalletsService extends WalletsGrpc.WalletsImplBase {
}
final Optional<ServerInterceptor> rateMeteringInterceptor() {
CallRateMeteringInterceptor defaultCallRateMeteringInterceptor =
new CallRateMeteringInterceptor(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));
// 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("lockWallet", new GrpcCallRateMeter(1, SECONDS));
put("unlockWallet", new GrpcCallRateMeter(1, SECONDS));
}});
return getCustomRateMeteringInterceptor(coreApi.getConfig().appDataDir, this.getClass())
.or(() -> Optional.of(defaultCallRateMeteringInterceptor));
.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));
// 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("lockWallet", new GrpcCallRateMeter(1, SECONDS));
put("unlockWallet", new GrpcCallRateMeter(1, SECONDS));
}}
)));
}
}

View File

@ -25,6 +25,7 @@ import io.grpc.StatusRuntimeException;
import org.apache.commons.lang3.StringUtils;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
@ -124,4 +125,10 @@ public final class CallRateMeteringInterceptor implements ServerInterceptor {
rateMetersString + "\n\t" + "}" + "\n"
+ "}";
}
public static CallRateMeteringInterceptor valueOf(Map<String, GrpcCallRateMeter> rateMeters) {
return new CallRateMeteringInterceptor(new HashMap<>() {{
putAll(rateMeters);
}});
}
}