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() { final Optional<ServerInterceptor> rateMeteringInterceptor() {
CallRateMeteringInterceptor defaultCallRateMeteringInterceptor = return getCustomRateMeteringInterceptor(coreApi.getConfig().appDataDir, this.getClass())
new CallRateMeteringInterceptor(new HashMap<>() {{ .or(() -> Optional.of(CallRateMeteringInterceptor.valueOf(
new HashMap<>() {{
// You can only register mainnet dispute agents in the UI. // You can only register mainnet dispute agents in the UI.
// Do not limit devs' ability to register test agents. // Do not limit devs' ability to register test agents.
put("registerDisputeAgent", new GrpcCallRateMeter(1, SECONDS)); put("registerDisputeAgent", new GrpcCallRateMeter(1, SECONDS));
}}); }}
)));
return getCustomRateMeteringInterceptor(coreApi.getConfig().appDataDir, this.getClass())
.or(() -> Optional.of(defaultCallRateMeteringInterceptor));
} }
} }

View File

@ -58,12 +58,11 @@ class GrpcGetTradeStatisticsService extends GetTradeStatisticsGrpc.GetTradeStati
} }
final Optional<ServerInterceptor> rateMeteringInterceptor() { final Optional<ServerInterceptor> rateMeteringInterceptor() {
CallRateMeteringInterceptor defaultCallRateMeteringInterceptor =
new CallRateMeteringInterceptor(new HashMap<>() {{
put("getTradeStatistics", new GrpcCallRateMeter(1, SECONDS));
}});
return getCustomRateMeteringInterceptor(coreApi.getConfig().appDataDir, this.getClass()) 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() { final Optional<ServerInterceptor> rateMeteringInterceptor() {
CallRateMeteringInterceptor defaultCallRateMeteringInterceptor =
new CallRateMeteringInterceptor(new HashMap<>() {{
put("getMethodHelp", new GrpcCallRateMeter(1, SECONDS));
}});
return getCustomRateMeteringInterceptor(coreApi.getConfig().appDataDir, this.getClass()) 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() { final Optional<ServerInterceptor> rateMeteringInterceptor() {
CallRateMeteringInterceptor defaultCallRateMeteringInterceptor = return getCustomRateMeteringInterceptor(coreApi.getConfig().appDataDir, this.getClass())
new CallRateMeteringInterceptor(new HashMap<>() {{ .or(() -> Optional.of(CallRateMeteringInterceptor.valueOf(
new HashMap<>() {{
put("getOffer", new GrpcCallRateMeter(1, SECONDS)); put("getOffer", new GrpcCallRateMeter(1, SECONDS));
put("getMyOffer", new GrpcCallRateMeter(1, SECONDS)); put("getMyOffer", new GrpcCallRateMeter(1, SECONDS));
put("getOffers", new GrpcCallRateMeter(1, SECONDS)); put("getOffers", new GrpcCallRateMeter(1, SECONDS));
put("getMyOffers", new GrpcCallRateMeter(1, SECONDS)); put("getMyOffers", new GrpcCallRateMeter(1, SECONDS));
put("createOffer", new GrpcCallRateMeter(1, MINUTES)); put("createOffer", new GrpcCallRateMeter(1, MINUTES));
put("cancelOffer", new GrpcCallRateMeter(1, MINUTES)); put("cancelOffer", new GrpcCallRateMeter(1, MINUTES));
}}); }}
)));
return getCustomRateMeteringInterceptor(coreApi.getConfig().appDataDir, this.getClass())
.or(() -> Optional.of(defaultCallRateMeteringInterceptor));
} }
} }

View File

@ -130,15 +130,14 @@ class GrpcPaymentAccountsService extends PaymentAccountsGrpc.PaymentAccountsImpl
} }
final Optional<ServerInterceptor> rateMeteringInterceptor() { final Optional<ServerInterceptor> rateMeteringInterceptor() {
CallRateMeteringInterceptor defaultCallRateMeteringInterceptor = return getCustomRateMeteringInterceptor(coreApi.getConfig().appDataDir, this.getClass())
new CallRateMeteringInterceptor(new HashMap<>() {{ .or(() -> Optional.of(CallRateMeteringInterceptor.valueOf(
new HashMap<>() {{
put("createPaymentAccount", new GrpcCallRateMeter(1, MINUTES)); put("createPaymentAccount", new GrpcCallRateMeter(1, MINUTES));
put("getPaymentAccounts", new GrpcCallRateMeter(1, SECONDS)); put("getPaymentAccounts", new GrpcCallRateMeter(1, SECONDS));
put("getPaymentMethods", new GrpcCallRateMeter(1, SECONDS)); put("getPaymentMethods", new GrpcCallRateMeter(1, SECONDS));
put("getPaymentAccountForm", new GrpcCallRateMeter(1, SECONDS)); put("getPaymentAccountForm", new GrpcCallRateMeter(1, SECONDS));
}}); }}
)));
return getCustomRateMeteringInterceptor(coreApi.getConfig().appDataDir, this.getClass())
.or(() -> Optional.of(defaultCallRateMeteringInterceptor));
} }
} }

View File

@ -75,12 +75,11 @@ class GrpcPriceService extends PriceGrpc.PriceImplBase {
} }
final Optional<ServerInterceptor> rateMeteringInterceptor() { final Optional<ServerInterceptor> rateMeteringInterceptor() {
CallRateMeteringInterceptor defaultCallRateMeteringInterceptor =
new CallRateMeteringInterceptor(new HashMap<>() {{
put("getMarketPrice", new GrpcCallRateMeter(1, SECONDS));
}});
return getCustomRateMeteringInterceptor(coreApi.getConfig().appDataDir, this.getClass()) 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() { final Optional<ServerInterceptor> rateMeteringInterceptor() {
CallRateMeteringInterceptor defaultCallRateMeteringInterceptor = return getCustomRateMeteringInterceptor(coreApi.getConfig().appDataDir, this.getClass())
new CallRateMeteringInterceptor(new HashMap<>() {{ .or(() -> Optional.of(CallRateMeteringInterceptor.valueOf(
new HashMap<>() {{
put("getTrade", new GrpcCallRateMeter(1, SECONDS)); put("getTrade", new GrpcCallRateMeter(1, SECONDS));
put("takeOffer", new GrpcCallRateMeter(1, MINUTES)); put("takeOffer", new GrpcCallRateMeter(1, MINUTES));
put("confirmPaymentStarted", new GrpcCallRateMeter(1, MINUTES)); put("confirmPaymentStarted", new GrpcCallRateMeter(1, MINUTES));
put("confirmPaymentReceived", new GrpcCallRateMeter(1, MINUTES)); put("confirmPaymentReceived", new GrpcCallRateMeter(1, MINUTES));
put("keepFunds", new GrpcCallRateMeter(1, MINUTES)); put("keepFunds", new GrpcCallRateMeter(1, MINUTES));
put("withdrawFunds", new GrpcCallRateMeter(1, MINUTES)); put("withdrawFunds", new GrpcCallRateMeter(1, MINUTES));
}}); }}
)));
return getCustomRateMeteringInterceptor(coreApi.getConfig().appDataDir, this.getClass())
.or(() -> Optional.of(defaultCallRateMeteringInterceptor));
} }
} }

View File

@ -74,12 +74,11 @@ public class GrpcVersionService extends GetVersionGrpc.GetVersionImplBase {
} }
final Optional<ServerInterceptor> rateMeteringInterceptor() { final Optional<ServerInterceptor> rateMeteringInterceptor() {
CallRateMeteringInterceptor defaultCallRateMeteringInterceptor =
new CallRateMeteringInterceptor(new HashMap<>() {{
put("getVersion", new GrpcCallRateMeter(1, SECONDS));
}});
return getCustomRateMeteringInterceptor(coreApi.getConfig().appDataDir, this.getClass()) 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,8 +349,9 @@ class GrpcWalletsService extends WalletsGrpc.WalletsImplBase {
} }
final Optional<ServerInterceptor> rateMeteringInterceptor() { final Optional<ServerInterceptor> rateMeteringInterceptor() {
CallRateMeteringInterceptor defaultCallRateMeteringInterceptor = return getCustomRateMeteringInterceptor(coreApi.getConfig().appDataDir, this.getClass())
new CallRateMeteringInterceptor(new HashMap<>() {{ .or(() -> Optional.of(CallRateMeteringInterceptor.valueOf(
new HashMap<>() {{
put("getBalances", new GrpcCallRateMeter(1, SECONDS)); put("getBalances", new GrpcCallRateMeter(1, SECONDS));
put("getAddressBalance", new GrpcCallRateMeter(1, SECONDS)); put("getAddressBalance", new GrpcCallRateMeter(1, SECONDS));
put("getFundingAddresses", new GrpcCallRateMeter(1, SECONDS)); put("getFundingAddresses", new GrpcCallRateMeter(1, SECONDS));
@ -369,9 +370,7 @@ class GrpcWalletsService extends WalletsGrpc.WalletsImplBase {
put("lockWallet", new GrpcCallRateMeter(1, SECONDS)); put("lockWallet", new GrpcCallRateMeter(1, SECONDS));
put("unlockWallet", new GrpcCallRateMeter(1, SECONDS)); put("unlockWallet", new GrpcCallRateMeter(1, SECONDS));
}}); }}
)));
return getCustomRateMeteringInterceptor(coreApi.getConfig().appDataDir, this.getClass())
.or(() -> Optional.of(defaultCallRateMeteringInterceptor));
} }
} }

View File

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