Search in sources :

Example 1 with CallNotPermittedException

use of io.github.resilience4j.circuitbreaker.CallNotPermittedException in project java-chassis by ServiceComb.

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));
        }
    });
}
Also used : AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Response(org.apache.servicecomb.swagger.invocation.Response) CallNotPermittedException(io.github.resilience4j.circuitbreaker.CallNotPermittedException) RequestNotPermitted(io.github.resilience4j.ratelimiter.RequestNotPermitted) GovernanceRequest(org.apache.servicecomb.governance.marker.GovernanceRequest) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) CommonExceptionData(org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData) BulkheadFullException(io.github.resilience4j.bulkhead.BulkheadFullException) DecorateCompletionStage(io.github.resilience4j.decorators.Decorators.DecorateCompletionStage) CompletionStage(java.util.concurrent.CompletionStage)

Example 2 with CallNotPermittedException

use of io.github.resilience4j.circuitbreaker.CallNotPermittedException 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));
        }
    });
}
Also used : AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Response(org.apache.servicecomb.swagger.invocation.Response) CallNotPermittedException(io.github.resilience4j.circuitbreaker.CallNotPermittedException) RequestNotPermitted(io.github.resilience4j.ratelimiter.RequestNotPermitted) GovernanceRequest(org.apache.servicecomb.governance.marker.GovernanceRequest) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) CommonExceptionData(org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData) BulkheadFullException(io.github.resilience4j.bulkhead.BulkheadFullException) DecorateCompletionStage(io.github.resilience4j.decorators.Decorators.DecorateCompletionStage) CompletionStage(java.util.concurrent.CompletionStage)

Aggregations

BulkheadFullException (io.github.resilience4j.bulkhead.BulkheadFullException)2 CallNotPermittedException (io.github.resilience4j.circuitbreaker.CallNotPermittedException)2 DecorateCompletionStage (io.github.resilience4j.decorators.Decorators.DecorateCompletionStage)2 RequestNotPermitted (io.github.resilience4j.ratelimiter.RequestNotPermitted)2 CompletionStage (java.util.concurrent.CompletionStage)2 GovernanceRequest (org.apache.servicecomb.governance.marker.GovernanceRequest)2 AsyncResponse (org.apache.servicecomb.swagger.invocation.AsyncResponse)2 Response (org.apache.servicecomb.swagger.invocation.Response)2 CommonExceptionData (org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData)2 InvocationException (org.apache.servicecomb.swagger.invocation.exception.InvocationException)2