Search in sources :

Example 1 with RateLimitRequest

use of com.tencent.polaris.ratelimit.client.pb.RatelimitV2.RateLimitRequest 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 2 with RateLimitRequest

use of com.tencent.polaris.ratelimit.client.pb.RatelimitV2.RateLimitRequest 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 3 with RateLimitRequest

use of com.tencent.polaris.ratelimit.client.pb.RatelimitV2.RateLimitRequest in project polaris-java by polarismesh.

the class StreamCounterSet method checkAndCreateResource.

/**
 * 获取同步阻塞的客户端
 *
 * @return 同步阻塞的客户端
 */
private StreamResource checkAndCreateResource(ServiceIdentifier serviceIdentifier, RateLimitWindow rateLimitWindow) {
    StreamResource streamResource = currentStreamResource.get();
    if (null != streamResource && !streamResource.endStream.get()) {
        return streamResource;
    }
    long lastConnectFailTimeMilli = 0;
    if (null != streamResource) {
        lastConnectFailTimeMilli = streamResource.lastConnectFailTimeMilli.get();
    }
    ManagedChannel channel = createConnection(lastConnectFailTimeMilli);
    if (null == channel) {
        return null;
    }
    RateLimitGRPCV2Stub rateLimitGRPCV2Stub = RateLimitGRPCV2Grpc.newStub(channel);
    StreamObserver<RateLimitRequest> streamClient = rateLimitGRPCV2Stub.service(streamResource);
    RateLimitGRPCV2BlockingStub rateLimitGRPCV2BlockingStub = RateLimitGRPCV2Grpc.newBlockingStub(channel);
    streamResource = new StreamResource(channel, streamClient, rateLimitGRPCV2BlockingStub);
    currentStreamResource.set(streamResource);
    if (initRecord.get(serviceIdentifier) == null) {
        initRecord.putIfAbsent(serviceIdentifier, new InitializeRecord(rateLimitWindow));
    }
    return streamResource;
}
Also used : RateLimitGRPCV2BlockingStub(com.tencent.polaris.ratelimit.client.pb.RateLimitGRPCV2Grpc.RateLimitGRPCV2BlockingStub) RateLimitGRPCV2Stub(com.tencent.polaris.ratelimit.client.pb.RateLimitGRPCV2Grpc.RateLimitGRPCV2Stub) ManagedChannel(io.grpc.ManagedChannel) RateLimitRequest(com.tencent.polaris.ratelimit.client.pb.RatelimitV2.RateLimitRequest)

Example 4 with RateLimitRequest

use of com.tencent.polaris.ratelimit.client.pb.RatelimitV2.RateLimitRequest 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("rk1").setValue("rv1").build()).build();
    RateLimitDescriptor descriptor2 = RateLimitDescriptor.newBuilder().addEntries(RateLimitDescriptor.Entry.newBuilder().setKey("rk2").setValue("rv2").build()).addEntries(RateLimitDescriptor.Entry.newBuilder().setKey("rk3").setValue("rv3").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(RateLimitResponse.Code.OK, response.getOverallCode());
    response.getStatusesList().forEach(e -> assertEquals(RateLimitResponse.Code.OK, e.getCode()));
}
Also used : RateLimitDescriptor(io.envoyproxy.envoy.extensions.common.ratelimit.v3.RateLimitDescriptor) TokenResult(com.alibaba.csp.sentinel.cluster.TokenResult) RateLimitRequest(io.envoyproxy.envoy.service.ratelimit.v3.RateLimitRequest) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) RateLimitResponse(io.envoyproxy.envoy.service.ratelimit.v3.RateLimitResponse) Test(org.junit.Test)

Example 5 with RateLimitRequest

use of com.tencent.polaris.ratelimit.client.pb.RatelimitV2.RateLimitRequest 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("rk1").setValue("rv1").build()).build();
    RateLimitDescriptor descriptor2 = RateLimitDescriptor.newBuilder().addEntries(RateLimitDescriptor.Entry.newBuilder().setKey("rk2").setValue("rv2").build()).addEntries(RateLimitDescriptor.Entry.newBuilder().setKey("rk3").setValue("rv3").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(RateLimitResponse.Code.OVER_LIMIT, response.getOverallCode());
    assertEquals(2, response.getStatusesCount());
    assertTrue(response.getStatusesList().stream().anyMatch(e -> e.getCode().equals(RateLimitResponse.Code.OVER_LIMIT)));
    assertFalse(response.getStatusesList().stream().allMatch(e -> e.getCode().equals(RateLimitResponse.Code.OVER_LIMIT)));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) TokenResultStatus(com.alibaba.csp.sentinel.cluster.TokenResultStatus) RateLimitDescriptor(io.envoyproxy.envoy.extensions.common.ratelimit.v3.RateLimitDescriptor) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Tuple2(com.alibaba.csp.sentinel.util.function.Tuple2) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito(org.mockito.Mockito) StreamObserver(io.grpc.stub.StreamObserver) ArgumentCaptor(org.mockito.ArgumentCaptor) RateLimitRequest(io.envoyproxy.envoy.service.ratelimit.v3.RateLimitRequest) RateLimitResponse(io.envoyproxy.envoy.service.ratelimit.v3.RateLimitResponse) TokenResult(com.alibaba.csp.sentinel.cluster.TokenResult) Assert(org.junit.Assert) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) ArgumentMatchers.same(org.mockito.ArgumentMatchers.same) RateLimitDescriptor(io.envoyproxy.envoy.extensions.common.ratelimit.v3.RateLimitDescriptor) TokenResult(com.alibaba.csp.sentinel.cluster.TokenResult) RateLimitRequest(io.envoyproxy.envoy.service.ratelimit.v3.RateLimitRequest) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) RateLimitResponse(io.envoyproxy.envoy.service.ratelimit.v3.RateLimitResponse) Test(org.junit.Test)

Aggregations

TokenResult (com.alibaba.csp.sentinel.cluster.TokenResult)4 FlowRule (com.alibaba.csp.sentinel.slots.block.flow.FlowRule)4 Test (org.junit.Test)4 RateLimitRequest (com.tencent.polaris.ratelimit.client.pb.RatelimitV2.RateLimitRequest)3 TokenResultStatus (com.alibaba.csp.sentinel.cluster.TokenResultStatus)2 Tuple2 (com.alibaba.csp.sentinel.util.function.Tuple2)2 StreamCounterSet (com.tencent.polaris.ratelimit.client.flow.StreamCounterSet)2 RateLimitDescriptor (io.envoyproxy.envoy.api.v2.ratelimit.RateLimitDescriptor)2 RateLimitDescriptor (io.envoyproxy.envoy.extensions.common.ratelimit.v3.RateLimitDescriptor)2 RateLimitRequest (io.envoyproxy.envoy.service.ratelimit.v2.RateLimitRequest)2 RateLimitResponse (io.envoyproxy.envoy.service.ratelimit.v2.RateLimitResponse)2 RateLimitRequest (io.envoyproxy.envoy.service.ratelimit.v3.RateLimitRequest)2 RateLimitResponse (io.envoyproxy.envoy.service.ratelimit.v3.RateLimitResponse)2 StreamObserver (io.grpc.stub.StreamObserver)2 Map (java.util.Map)2 Assert (org.junit.Assert)2 ArgumentCaptor (org.mockito.ArgumentCaptor)2 Mockito (org.mockito.Mockito)2 AmountInfo (com.tencent.polaris.api.plugin.ratelimiter.AmountInfo)1 LocalQuotaInfo (com.tencent.polaris.api.plugin.ratelimiter.LocalQuotaInfo)1