use of org.apache.servicecomb.governance.marker.GovernanceRequest in project java-chassis by ServiceComb.
the class OperatorTest method test_prefix_api_path_match.
@Test
public void test_prefix_api_path_match() {
GovernanceRequest request = new GovernanceRequest();
request.setUri("/bulkhead/hello");
Matcher matcher = new Matcher();
RawOperator apiPath = new RawOperator();
apiPath.put("prefix", "/bulkhead");
matcher.setApiPath(apiPath);
Assert.assertTrue(requestProcessor.match(request, matcher));
}
use of org.apache.servicecomb.governance.marker.GovernanceRequest in project java-chassis by ServiceComb.
the class FlowControlTest method test_rate_limiting_work.
@Test
public void test_rate_limiting_work() throws Throwable {
DecorateCheckedSupplier<Object> ds = Decorators.ofCheckedSupplier(() -> {
return "test";
});
GovernanceRequest request = new GovernanceRequest();
request.setUri("/hello");
RateLimiter rateLimiter = rateLimitingHandler.getActuator(request);
ds.withRateLimiter(rateLimiter);
Assert.assertEquals("test", ds.get());
// flow control
CountDownLatch cd = new CountDownLatch(10);
AtomicBoolean expected = new AtomicBoolean(false);
AtomicBoolean notExpected = new AtomicBoolean(false);
for (int i = 0; i < 10; i++) {
new Thread() {
public void run() {
try {
Object result = ds.get();
if (!"test".equals(result)) {
notExpected.set(true);
}
} catch (Throwable e) {
if (e instanceof RequestNotPermitted) {
expected.set(true);
} else {
notExpected.set(true);
}
}
cd.countDown();
}
}.start();
}
cd.await(1, TimeUnit.SECONDS);
Assert.assertTrue(expected.get());
Assert.assertFalse(notExpected.get());
}
use of org.apache.servicecomb.governance.marker.GovernanceRequest in project incubator-servicecomb-java-chassis by apache.
the class ProviderGovernanceHandler method handle.
@Override
public void handle(Invocation invocation, AsyncResponse asyncResp) throws Exception {
Supplier<CompletionStage<Response>> next = createBusinessCompletionStageSupplier(invocation);
DecorateCompletionStage<Response> dcs = Decorators.ofCompletionStage(next);
GovernanceRequest request = MatchType.createGovHttpRequest(invocation);
try {
ServiceCombInvocationContext.setInvocationContext(invocation);
addRateLimiting(dcs, request);
addCircuitBreaker(dcs, request);
addBulkhead(dcs, request);
} finally {
ServiceCombInvocationContext.removeInvocationContext();
}
dcs.get().whenComplete((r, e) -> {
if (e == null) {
asyncResp.complete(r);
return;
}
if (e instanceof RequestNotPermitted) {
asyncResp.complete(Response.failResp(new InvocationException(429, "rate limited.", new CommonExceptionData("rate limited."))));
LOGGER.warn("the request is rate limit by policy : {}", e.getMessage());
} else if (e instanceof CallNotPermittedException) {
asyncResp.complete(Response.failResp(new InvocationException(429, "circuitBreaker is open.", new CommonExceptionData("circuitBreaker is open."))));
LOGGER.warn("circuitBreaker is open by policy : {}", e.getMessage());
} else if (e instanceof BulkheadFullException) {
asyncResp.complete(Response.failResp(new InvocationException(429, "bulkhead is full and does not permit further calls.", new CommonExceptionData("bulkhead is full and does not permit further calls."))));
LOGGER.warn("bulkhead is full and does not permit further calls by policy : {}", e.getMessage());
} else {
asyncResp.complete(Response.createProducerFail(e));
}
});
}
use of org.apache.servicecomb.governance.marker.GovernanceRequest in project incubator-servicecomb-java-chassis by apache.
the class OperatorTest method test_exact_api_path_match.
@Test
public void test_exact_api_path_match() {
GovernanceRequest request = new GovernanceRequest();
request.setUri("/bulkhead");
Matcher matcher = new Matcher();
RawOperator apiPath = new RawOperator();
apiPath.put("exact", "/bulkhead");
matcher.setApiPath(apiPath);
Assert.assertTrue(requestProcessor.match(request, matcher));
}
use of org.apache.servicecomb.governance.marker.GovernanceRequest in project incubator-servicecomb-java-chassis by apache.
the class OperatorTest method test_compare_header_not_match.
@Test
public void test_compare_header_not_match() {
GovernanceRequest request = new GovernanceRequest();
Map<String, String> reqHeaders = new HashMap<>();
reqHeaders.put("header1", "100");
request.setHeaders(reqHeaders);
Matcher matcher = new Matcher();
Map<String, RawOperator> headers = new HashMap<>();
RawOperator header1 = new RawOperator();
header1.put("compare", ">1000");
headers.put("header1", header1);
matcher.setHeaders(headers);
Assert.assertFalse(requestProcessor.match(request, matcher));
header1 = new RawOperator();
header1.put("compare", ">=1000");
headers.put("header1", header1);
matcher.setHeaders(headers);
Assert.assertFalse(requestProcessor.match(request, matcher));
header1 = new RawOperator();
header1.put("compare", "<10");
headers.put("header1", header1);
matcher.setHeaders(headers);
Assert.assertFalse(requestProcessor.match(request, matcher));
header1 = new RawOperator();
header1.put("compare", "<=10");
headers.put("header1", header1);
matcher.setHeaders(headers);
Assert.assertFalse(requestProcessor.match(request, matcher));
header1 = new RawOperator();
header1.put("compare", "=200");
headers.put("header1", header1);
matcher.setHeaders(headers);
Assert.assertFalse(requestProcessor.match(request, matcher));
}
Aggregations