Przeglądaj źródła

Fix zero-count divide overflow bug in RateLimiterController (#461)

master
mjaow Eric Zhao 5 lat temu
rodzic
commit
2cf6e29e72
2 zmienionych plików z 26 dodań i 1 usunięć
  1. +15
    -0
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/controller/RateLimiterController.java
  2. +11
    -1
      sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/controller/RateLimiterControllerTest.java

+ 15
- 0
sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/controller/RateLimiterController.java Wyświetl plik

@@ -44,6 +44,21 @@ public class RateLimiterController implements TrafficShapingController {

@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);


+ 11
- 1
sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/controller/RateLimiterControllerTest.java Wyświetl plik

@@ -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;

@@ -25,7 +26,6 @@ import org.junit.Test;

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
@@ -85,4 +85,14 @@ public class RateLimiterControllerTest {

}

@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));
}
}
}

Ładowanie…
Anuluj
Zapisz