use of com.alibaba.csp.sentinel.slots.block.flow.FlowRule in project Sentinel by alibaba.
the class SentinelSpringMvcIntegrationTest method configureExceptionRulesFor.
private void configureExceptionRulesFor(String resource, int count, String limitApp) {
FlowRule rule = new FlowRule().setCount(count).setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_RATIO);
rule.setResource(resource);
if (StringUtil.isNotBlank(limitApp)) {
rule.setLimitApp(limitApp);
}
FlowRuleManager.loadRules(Collections.singletonList(rule));
}
use of com.alibaba.csp.sentinel.slots.block.flow.FlowRule in project Sentinel by alibaba.
the class ClusterFlowRuleManager method getFlowRules.
/**
* Get all cluster flow rules within a specific namespace.
*
* @param namespace valid namespace
* @return cluster flow rules within the provided namespace
*/
public static List<FlowRule> getFlowRules(String namespace) {
if (StringUtil.isEmpty(namespace)) {
return new ArrayList<>();
}
List<FlowRule> rules = new ArrayList<>();
Set<Long> flowIdSet = NAMESPACE_FLOW_ID_MAP.get(namespace);
if (flowIdSet == null || flowIdSet.isEmpty()) {
return rules;
}
for (Long flowId : flowIdSet) {
FlowRule rule = FLOW_RULES.get(flowId);
if (rule != null) {
rules.add(rule);
}
}
return rules;
}
use of com.alibaba.csp.sentinel.slots.block.flow.FlowRule in project Sentinel by alibaba.
the class ClusterMetricNodeGenerator method flowToMetricNode.
public static ClusterMetricNode flowToMetricNode(long flowId) {
FlowRule rule = ClusterFlowRuleManager.getFlowRuleById(flowId);
if (rule == null) {
return null;
}
ClusterMetric metric = ClusterMetricStatistics.getMetric(flowId);
if (metric == null) {
return new ClusterMetricNode().setFlowId(flowId).setResourceName(rule.getResource());
}
return new ClusterMetricNode().setFlowId(flowId).setResourceName(rule.getResource()).setBlockQps(metric.getAvg(ClusterFlowEvent.BLOCK)).setPassQps(metric.getAvg(ClusterFlowEvent.PASS)).setTimestamp(TimeUtil.currentTimeMillis());
}
use of com.alibaba.csp.sentinel.slots.block.flow.FlowRule in project Sentinel by alibaba.
the class SentinelEnvoyRlsServiceImplTest method testShouldRateLimitPass.
@Test
public void testShouldRateLimitPass() {
SentinelEnvoyRlsServiceImpl rlsService = mock(SentinelEnvoyRlsServiceImpl.class);
StreamObserver<RateLimitResponse> streamObserver = mock(StreamObserver.class);
String domain = "testShouldRateLimitPass";
int acquireCount = 1;
RateLimitDescriptor descriptor1 = RateLimitDescriptor.newBuilder().addEntries(RateLimitDescriptor.Entry.newBuilder().setKey("a1").setValue("b1").build()).build();
RateLimitDescriptor descriptor2 = RateLimitDescriptor.newBuilder().addEntries(RateLimitDescriptor.Entry.newBuilder().setKey("a2").setValue("b2").build()).addEntries(RateLimitDescriptor.Entry.newBuilder().setKey("a3").setValue("b3").build()).build();
ArgumentCaptor<RateLimitResponse> responseCapture = ArgumentCaptor.forClass(RateLimitResponse.class);
doNothing().when(streamObserver).onNext(responseCapture.capture());
doCallRealMethod().when(rlsService).shouldRateLimit(any(), any());
when(rlsService.checkToken(eq(domain), same(descriptor1), eq(acquireCount))).thenReturn(Tuple2.of(new FlowRule(), new TokenResult(TokenResultStatus.OK)));
when(rlsService.checkToken(eq(domain), same(descriptor2), eq(acquireCount))).thenReturn(Tuple2.of(new FlowRule(), new TokenResult(TokenResultStatus.OK)));
RateLimitRequest rateLimitRequest = RateLimitRequest.newBuilder().addDescriptors(descriptor1).addDescriptors(descriptor2).setDomain(domain).setHitsAddend(acquireCount).build();
rlsService.shouldRateLimit(rateLimitRequest, streamObserver);
RateLimitResponse response = responseCapture.getValue();
assertEquals(Code.OK, response.getOverallCode());
response.getStatusesList().forEach(e -> assertEquals(Code.OK, e.getCode()));
}
use of com.alibaba.csp.sentinel.slots.block.flow.FlowRule in project Sentinel by alibaba.
the class SentinelEnvoyRlsServiceImplTest method testShouldRatePartialBlock.
@Test
public void testShouldRatePartialBlock() {
SentinelEnvoyRlsServiceImpl rlsService = mock(SentinelEnvoyRlsServiceImpl.class);
StreamObserver<RateLimitResponse> streamObserver = mock(StreamObserver.class);
String domain = "testShouldRatePartialBlock";
int acquireCount = 1;
RateLimitDescriptor descriptor1 = RateLimitDescriptor.newBuilder().addEntries(RateLimitDescriptor.Entry.newBuilder().setKey("a1").setValue("b1").build()).build();
RateLimitDescriptor descriptor2 = RateLimitDescriptor.newBuilder().addEntries(RateLimitDescriptor.Entry.newBuilder().setKey("a2").setValue("b2").build()).addEntries(RateLimitDescriptor.Entry.newBuilder().setKey("a3").setValue("b3").build()).build();
ArgumentCaptor<RateLimitResponse> responseCapture = ArgumentCaptor.forClass(RateLimitResponse.class);
doNothing().when(streamObserver).onNext(responseCapture.capture());
doCallRealMethod().when(rlsService).shouldRateLimit(any(), any());
when(rlsService.checkToken(eq(domain), same(descriptor1), eq(acquireCount))).thenReturn(Tuple2.of(new FlowRule(), new TokenResult(TokenResultStatus.BLOCKED)));
when(rlsService.checkToken(eq(domain), same(descriptor2), eq(acquireCount))).thenReturn(Tuple2.of(new FlowRule(), new TokenResult(TokenResultStatus.OK)));
RateLimitRequest rateLimitRequest = RateLimitRequest.newBuilder().addDescriptors(descriptor1).addDescriptors(descriptor2).setDomain(domain).setHitsAddend(acquireCount).build();
rlsService.shouldRateLimit(rateLimitRequest, streamObserver);
RateLimitResponse response = responseCapture.getValue();
assertEquals(Code.OVER_LIMIT, response.getOverallCode());
assertEquals(2, response.getStatusesCount());
assertTrue(response.getStatusesList().stream().anyMatch(e -> e.getCode().equals(Code.OVER_LIMIT)));
assertFalse(response.getStatusesList().stream().allMatch(e -> e.getCode().equals(Code.OVER_LIMIT)));
}
Aggregations