Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bugs in RateLimiterController #461

Merged
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 @@ -44,6 +44,21 @@ public boolean canPass(Node node, int acquireCount) {

@Override
public boolean canPass(Node node, int acquireCount, boolean prioritized) {
/*
1. Pass when acquire count is less or equal than 0
2. Reject when count is less or equal than 0.
Otherwise,the costTime will be max of long and waitTime will overflow in some cases.
This will lead to pass of following request.It's dangerous!!!
*/

if (acquireCount <= 0) {
return true;
}

if (count <= 0) {
return false;
}

long currentTime = TimeUtil.currentTimeMillis();
// Calculate the interval between every two requests.
long costTime = Math.round(1.0 * (acquireCount) / count * 1000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.alibaba.csp.sentinel.slots.block.flow.controller;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;

Expand All @@ -25,7 +26,6 @@

import com.alibaba.csp.sentinel.util.TimeUtil;
import com.alibaba.csp.sentinel.node.Node;
import com.alibaba.csp.sentinel.slots.block.flow.controller.RateLimiterController;

/**
* @author jialiang.linjl
Expand Down Expand Up @@ -85,4 +85,14 @@ public void run() {

}

@Test
public void testPaceController_zeroattack() throws InterruptedException {
RateLimiterController paceController = new RateLimiterController(500, 0d);
Node node = mock(Node.class);

for (int i = 0; i < 2; i++) {
assertFalse(paceController.canPass(node, 1));
assertTrue(paceController.canPass(node, 0));
}
}
}