Search in sources :

Example 1 with TokenService

use of com.alibaba.csp.sentinel.cluster.TokenService 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)

Example 2 with TokenService

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

the class ParamFlowRequestProcessor method processRequest.

@Override
public ClusterResponse<FlowTokenResponseData> processRequest(ClusterRequest<ParamFlowRequestData> request) {
    TokenService tokenService = TokenServiceProvider.getService();
    long flowId = request.getData().getFlowId();
    int count = request.getData().getCount();
    Collection<Object> args = request.getData().getParams();
    TokenResult result = tokenService.requestParamToken(flowId, count, args);
    return toResponse(result, request);
}
Also used : TokenResult(com.alibaba.csp.sentinel.cluster.TokenResult) TokenService(com.alibaba.csp.sentinel.cluster.TokenService)

Example 3 with TokenService

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

the class FlowRuleChecker method passClusterCheck.

private static boolean passClusterCheck(FlowRule rule, Context context, DefaultNode node, int acquireCount, boolean prioritized) {
    try {
        TokenService clusterService = pickClusterService();
        if (clusterService == null) {
            return fallbackToLocalOrPass(rule, context, node, acquireCount, prioritized);
        }
        long flowId = rule.getClusterConfig().getFlowId();
        TokenResult result = clusterService.requestToken(flowId, acquireCount, prioritized);
        return applyTokenResult(result, rule, context, node, acquireCount, prioritized);
    // If client is absent, then fallback to local mode.
    } catch (Throwable ex) {
        RecordLog.warn("[FlowRuleChecker] Request cluster token unexpected failed", ex);
    }
    // If fallback is not enabled, then directly pass.
    return fallbackToLocalOrPass(rule, context, node, acquireCount, prioritized);
}
Also used : TokenResult(com.alibaba.csp.sentinel.cluster.TokenResult) TokenService(com.alibaba.csp.sentinel.cluster.TokenService)

Example 4 with TokenService

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

the class ParamFlowChecker method passClusterCheck.

private static boolean passClusterCheck(ResourceWrapper resourceWrapper, ParamFlowRule rule, int count, Object value) {
    try {
        Collection<Object> params = toCollection(value);
        TokenService clusterService = pickClusterService();
        if (clusterService == null) {
            // pass in need.
            return fallbackToLocalOrPass(resourceWrapper, rule, count, params);
        }
        TokenResult result = clusterService.requestParamToken(rule.getClusterConfig().getFlowId(), count, params);
        switch(result.getStatus()) {
            case TokenResultStatus.OK:
                return true;
            case TokenResultStatus.BLOCKED:
                return false;
            default:
                return fallbackToLocalOrPass(resourceWrapper, rule, count, params);
        }
    } catch (Throwable ex) {
        RecordLog.warn("[ParamFlowChecker] Request cluster token for parameter unexpected failed", ex);
        return fallbackToLocalOrPass(resourceWrapper, rule, count, value);
    }
}
Also used : TokenResult(com.alibaba.csp.sentinel.cluster.TokenResult) TokenService(com.alibaba.csp.sentinel.cluster.TokenService)

Aggregations

TokenResult (com.alibaba.csp.sentinel.cluster.TokenResult)4 TokenService (com.alibaba.csp.sentinel.cluster.TokenService)4