Add license comment, stop & toString methods, and make isRunning transient

This commit is contained in:
ghubstan 2020-12-19 13:14:06 -03:00
parent 89e2187878
commit a5ed17e43f
No known key found for this signature in database
GPG key ID: E35592D6800A861E

View file

@ -1,3 +1,20 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/
package bisq.daemon.grpc.interceptor; package bisq.daemon.grpc.interceptor;
import bisq.common.Timer; import bisq.common.Timer;
@ -21,12 +38,10 @@ public final class GrpcCallRateMeter {
private final int allowedCallsPerTimeUnit; private final int allowedCallsPerTimeUnit;
@Getter @Getter
private final TimeUnit timeUnit; private final TimeUnit timeUnit;
@Getter @Getter
private int callsCount = 0; private int callsCount = 0;
@Getter @Getter
private boolean isRunning; private transient boolean isRunning;
@Nullable @Nullable
private Timer timer; private Timer timer;
@ -37,11 +52,16 @@ public final class GrpcCallRateMeter {
} }
public void start() { public void start() {
stop();
timer = UserThread.runPeriodically(() -> callsCount = 0, 1, timeUnit);
isRunning = true;
}
public void stop() {
if (timer != null) if (timer != null)
timer.stop(); timer.stop();
timer = UserThread.runPeriodically(() -> callsCount = 0, 1, timeUnit); isRunning = false;
isRunning = true;
} }
public void incrementCallsCount() { public void incrementCallsCount() {
@ -54,7 +74,7 @@ public final class GrpcCallRateMeter {
public String getCallsCountProgress(String calledMethodName) { public String getCallsCountProgress(String calledMethodName) {
String shortTimeUnitName = StringUtils.chop(timeUnit.name().toLowerCase()); String shortTimeUnitName = StringUtils.chop(timeUnit.name().toLowerCase());
return format("%s has been called %d time%s in the last %s; the rate limit is %d/%s.", return format("%s has been called %d time%s in the last %s, rate limit is %d/%s",
calledMethodName, calledMethodName,
callsCount, callsCount,
callsCount == 1 ? "" : "s", callsCount == 1 ? "" : "s",
@ -62,4 +82,14 @@ public final class GrpcCallRateMeter {
allowedCallsPerTimeUnit, allowedCallsPerTimeUnit,
shortTimeUnitName); shortTimeUnitName);
} }
@Override
public String toString() {
return "GrpcCallRateMeter{" +
"allowedCallsPerTimeUnit=" + allowedCallsPerTimeUnit +
", timeUnit=" + timeUnit.name() +
", callsCount=" + callsCount +
", isRunning=" + isRunning +
'}';
}
} }