Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,32 @@ public class DefaultTPSLimiter implements TPSLimiter {

@Override
public boolean isAllowable(URL url, Invocation invocation) {
boolean isMethodLevelTpsConfigured =
url.hasMethodParameter(RpcUtils.getMethodName(invocation), TPS_LIMIT_RATE_KEY);
String key = isMethodLevelTpsConfigured
? url.getServiceKey() + "#" + RpcUtils.getMethodName(invocation)
: url.getServiceKey();
Comment on lines +44 to +46
Copy link

Copilot AI Sep 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The hardcoded '#' separator could be extracted as a constant to improve maintainability and make the key format more explicit.

Copilot uses AI. Check for mistakes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn’t support method overloading here, but that should be fine.

int rate = url.getMethodParameter(RpcUtils.getMethodName(invocation), TPS_LIMIT_RATE_KEY, -1);
long interval = url.getMethodParameter(
RpcUtils.getMethodName(invocation), TPS_LIMIT_INTERVAL_KEY, DEFAULT_TPS_LIMIT_INTERVAL);
String serviceKey = url.getServiceKey();

if (rate > 0) {
StatItem statItem = stats.get(serviceKey);
StatItem statItem = stats.get(key);
if (statItem == null) {
stats.putIfAbsent(serviceKey, new StatItem(serviceKey, rate, interval));
statItem = stats.get(serviceKey);
stats.putIfAbsent(key, new StatItem(key, rate, interval));
statItem = stats.get(key);
} else {
// rate or interval has changed, rebuild
if (statItem.getRate() != rate || statItem.getInterval() != interval) {
stats.put(serviceKey, new StatItem(serviceKey, rate, interval));
statItem = stats.get(serviceKey);
stats.put(key, new StatItem(key, rate, interval));
statItem = stats.get(key);
}
}
return statItem.isAllowable();
} else {
StatItem statItem = stats.get(serviceKey);
StatItem statItem = stats.get(key);
if (statItem != null) {
stats.remove(serviceKey);
stats.remove(key);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,52 @@ void testIsNotAllowable() {
}

@Test
void testTPSLimiterForMethodLevelConfig() {
void testMethodLevelTpsOverridesServiceLevel() {
Invocation invocation = new MockInvocation();
URL url = URL.valueOf("test://test");
url = url.addParameter(INTERFACE_KEY, "org.apache.dubbo.rpc.file.TpsService");
url = url.addParameter(TPS_LIMIT_RATE_KEY, TEST_LIMIT_RATE);
int tpsConfigForMethodLevel = 3;
url = url.addParameter("tps", 1);
url = url.addParameter("echo.tps", tpsConfigForMethodLevel);
url = url.addParameter(TPS_LIMIT_INTERVAL_KEY, 1000);
for (int i = 1; i <= tpsConfigForMethodLevel + 1; i++) {
if (i == tpsConfigForMethodLevel + 1) {
Assertions.assertFalse(defaultTPSLimiter.isAllowable(url, invocation));
} else {
Assertions.assertTrue(defaultTPSLimiter.isAllowable(url, invocation));
}
}
}

@Test
void testServiceLevelTpsWhenOtherMethodsHaveTps() {
Invocation invocation = new MockInvocation();
URL url = URL.valueOf("test://test");
url = url.addParameter(INTERFACE_KEY, "org.apache.dubbo.rpc.file.TpsService");
url = url.addParameter(TPS_LIMIT_RATE_KEY, TEST_LIMIT_RATE);
int tpsConfigForServiceLevel = 3;
url = url.addParameter("tps", tpsConfigForServiceLevel);
url = url.addParameter("otherMethod.tps", 1);
url = url.addParameter(TPS_LIMIT_INTERVAL_KEY, 1000);
for (int i = 1; i <= tpsConfigForServiceLevel + 1; i++) {
if (i == tpsConfigForServiceLevel + 1) {
Assertions.assertFalse(defaultTPSLimiter.isAllowable(url, invocation));
} else {
Assertions.assertTrue(defaultTPSLimiter.isAllowable(url, invocation));
}
}
}

@Test
void testMethodLevelTpsIsolation() {
Invocation invocation = new MockInvocation();
URL url = URL.valueOf("test://test");
url = url.addParameter(INTERFACE_KEY, "org.apache.dubbo.rpc.file.TpsService");
url = url.addParameter(TPS_LIMIT_RATE_KEY, TEST_LIMIT_RATE);
int tpsConfigForMethodLevel = 3;
url = url.addParameter("tps", 1);
url = url.addParameter("otherMethod.tps", 2);
url = url.addParameter("echo.tps", tpsConfigForMethodLevel);
url = url.addParameter(TPS_LIMIT_INTERVAL_KEY, 1000);
for (int i = 1; i <= tpsConfigForMethodLevel + 1; i++) {
Expand Down
Loading