Search in sources :

Example 1 with TokenResult

use of com.alibaba.csp.sentinel.cluster.TokenResult in project Sentinel by alibaba.

the class DefaultClusterTokenClient method sendTokenRequest.

private TokenResult sendTokenRequest(ClusterRequest request) throws Exception {
    if (transportClient == null) {
        RecordLog.warn("[DefaultClusterTokenClient] Client not created, please check your config for cluster client");
        return clientFail();
    }
    ClusterResponse response = transportClient.sendRequest(request);
    TokenResult result = new TokenResult(response.getStatus());
    if (response.getData() != null) {
        FlowTokenResponseData responseData = (FlowTokenResponseData) response.getData();
        result.setRemaining(responseData.getRemainingCount()).setWaitInMs(responseData.getWaitInMs());
    }
    return result;
}
Also used : FlowTokenResponseData(com.alibaba.csp.sentinel.cluster.response.data.FlowTokenResponseData) TokenResult(com.alibaba.csp.sentinel.cluster.TokenResult) ClusterResponse(com.alibaba.csp.sentinel.cluster.response.ClusterResponse)

Example 2 with TokenResult

use of com.alibaba.csp.sentinel.cluster.TokenResult in project Sentinel by alibaba.

the class DefaultClusterTokenClient method requestParamToken.

@Override
public TokenResult requestParamToken(Long flowId, int acquireCount, Collection<Object> params) {
    if (notValidRequest(flowId, acquireCount) || params == null || params.isEmpty()) {
        return badRequest();
    }
    ParamFlowRequestData data = new ParamFlowRequestData().setCount(acquireCount).setFlowId(flowId).setParams(params);
    ClusterRequest<ParamFlowRequestData> request = new ClusterRequest<>(ClusterConstants.MSG_TYPE_PARAM_FLOW, data);
    try {
        TokenResult result = sendTokenRequest(request);
        logForResult(result);
        return result;
    } catch (Exception ex) {
        ClusterClientStatLogUtil.log(ex.getMessage());
        return new TokenResult(TokenResultStatus.FAIL);
    }
}
Also used : TokenResult(com.alibaba.csp.sentinel.cluster.TokenResult) ClusterRequest(com.alibaba.csp.sentinel.cluster.request.ClusterRequest) ParamFlowRequestData(com.alibaba.csp.sentinel.cluster.request.data.ParamFlowRequestData)

Example 3 with TokenResult

use of com.alibaba.csp.sentinel.cluster.TokenResult 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()));
}
Also used : RateLimitDescriptor(io.envoyproxy.envoy.api.v2.ratelimit.RateLimitDescriptor) TokenResult(com.alibaba.csp.sentinel.cluster.TokenResult) RateLimitRequest(io.envoyproxy.envoy.service.ratelimit.v2.RateLimitRequest) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) RateLimitResponse(io.envoyproxy.envoy.service.ratelimit.v2.RateLimitResponse) Test(org.junit.Test)

Example 4 with TokenResult

use of com.alibaba.csp.sentinel.cluster.TokenResult 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)));
}
Also used : RateLimitRequest(io.envoyproxy.envoy.service.ratelimit.v2.RateLimitRequest) Code(io.envoyproxy.envoy.service.ratelimit.v2.RateLimitResponse.Code) TokenResultStatus(com.alibaba.csp.sentinel.cluster.TokenResultStatus) Tuple2(com.alibaba.csp.sentinel.util.function.Tuple2) Test(org.junit.Test) RateLimitDescriptor(io.envoyproxy.envoy.api.v2.ratelimit.RateLimitDescriptor) Mockito(org.mockito.Mockito) StreamObserver(io.grpc.stub.StreamObserver) ArgumentCaptor(org.mockito.ArgumentCaptor) TokenResult(com.alibaba.csp.sentinel.cluster.TokenResult) Assert(org.junit.Assert) RateLimitResponse(io.envoyproxy.envoy.service.ratelimit.v2.RateLimitResponse) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) RateLimitDescriptor(io.envoyproxy.envoy.api.v2.ratelimit.RateLimitDescriptor) TokenResult(com.alibaba.csp.sentinel.cluster.TokenResult) RateLimitRequest(io.envoyproxy.envoy.service.ratelimit.v2.RateLimitRequest) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) RateLimitResponse(io.envoyproxy.envoy.service.ratelimit.v2.RateLimitResponse) Test(org.junit.Test)

Example 5 with TokenResult

use of com.alibaba.csp.sentinel.cluster.TokenResult in project Sentinel by alibaba.

the class FlowRequestProcessor method processRequest.

@Override
public ClusterResponse<FlowTokenResponseData> processRequest(ClusterRequest<FlowRequestData> request) {
    TokenService tokenService = TokenServiceProvider.getService();
    long flowId = request.getData().getFlowId();
    int count = request.getData().getCount();
    boolean prioritized = request.getData().isPriority();
    TokenResult result = tokenService.requestToken(flowId, count, prioritized);
    return toResponse(result, request);
}
Also used : TokenResult(com.alibaba.csp.sentinel.cluster.TokenResult) TokenService(com.alibaba.csp.sentinel.cluster.TokenService)

Aggregations

TokenResult (com.alibaba.csp.sentinel.cluster.TokenResult)22 FlowRule (com.alibaba.csp.sentinel.slots.block.flow.FlowRule)11 Test (org.junit.Test)6 TokenService (com.alibaba.csp.sentinel.cluster.TokenService)4 RateLimitDescriptor (io.envoyproxy.envoy.api.v2.ratelimit.RateLimitDescriptor)3 RateLimitDescriptor (io.envoyproxy.envoy.extensions.common.ratelimit.v3.RateLimitDescriptor)3 RateLimitResponse (io.envoyproxy.envoy.service.ratelimit.v2.RateLimitResponse)3 RateLimitResponse (io.envoyproxy.envoy.service.ratelimit.v3.RateLimitResponse)3 ArrayList (java.util.ArrayList)3 TokenResultStatus (com.alibaba.csp.sentinel.cluster.TokenResultStatus)2 TokenCacheNode (com.alibaba.csp.sentinel.cluster.flow.statistic.concurrent.TokenCacheNode)2 ClusterMetric (com.alibaba.csp.sentinel.cluster.flow.statistic.metric.ClusterMetric)2 ClusterRequest (com.alibaba.csp.sentinel.cluster.request.ClusterRequest)2 ParamFlowRequestData (com.alibaba.csp.sentinel.cluster.request.data.ParamFlowRequestData)2 AbstractTimeBasedTest (com.alibaba.csp.sentinel.test.AbstractTimeBasedTest)2 Tuple2 (com.alibaba.csp.sentinel.util.function.Tuple2)2 RateLimitRequest (io.envoyproxy.envoy.service.ratelimit.v2.RateLimitRequest)2 Code (io.envoyproxy.envoy.service.ratelimit.v2.RateLimitResponse.Code)2 RateLimitRequest (io.envoyproxy.envoy.service.ratelimit.v3.RateLimitRequest)2 StreamObserver (io.grpc.stub.StreamObserver)2