Search in sources :

Example 1 with ClusterResponse

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

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

the class NettyTransportClient method sendRequest.

@Override
public ClusterResponse sendRequest(ClusterRequest request) throws Exception {
    if (!isReady()) {
        throw new SentinelClusterException(ClusterErrorMessages.CLIENT_NOT_READY);
    }
    if (!validRequest(request)) {
        throw new SentinelClusterException(ClusterErrorMessages.BAD_REQUEST);
    }
    int xid = getCurrentId();
    try {
        request.setId(xid);
        channel.writeAndFlush(request);
        ChannelPromise promise = channel.newPromise();
        TokenClientPromiseHolder.putPromise(xid, promise);
        if (!promise.await(ClusterClientConfigManager.getRequestTimeout())) {
            throw new SentinelClusterException(ClusterErrorMessages.REQUEST_TIME_OUT);
        }
        SimpleEntry<ChannelPromise, ClusterResponse> entry = TokenClientPromiseHolder.getEntry(xid);
        if (entry == null || entry.getValue() == null) {
            // Should not go through here.
            throw new SentinelClusterException(ClusterErrorMessages.UNEXPECTED_STATUS);
        }
        return entry.getValue();
    } finally {
        TokenClientPromiseHolder.remove(xid);
    }
}
Also used : SentinelClusterException(com.alibaba.csp.sentinel.cluster.exception.SentinelClusterException) ClusterResponse(com.alibaba.csp.sentinel.cluster.response.ClusterResponse) ChannelPromise(io.netty.channel.ChannelPromise)

Example 3 with ClusterResponse

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

the class TokenClientPromiseHolder method completePromise.

public static <T> boolean completePromise(int xid, ClusterResponse<T> response) {
    if (!PROMISE_MAP.containsKey(xid)) {
        return false;
    }
    SimpleEntry<ChannelPromise, ClusterResponse> entry = PROMISE_MAP.get(xid);
    if (entry != null) {
        ChannelPromise promise = entry.getKey();
        if (promise.isDone() || promise.isCancelled()) {
            return false;
        }
        entry.setValue(response);
        promise.setSuccess();
        return true;
    }
    return false;
}
Also used : ClusterResponse(com.alibaba.csp.sentinel.cluster.response.ClusterResponse) ChannelPromise(io.netty.channel.ChannelPromise)

Aggregations

ClusterResponse (com.alibaba.csp.sentinel.cluster.response.ClusterResponse)3 ChannelPromise (io.netty.channel.ChannelPromise)2 TokenResult (com.alibaba.csp.sentinel.cluster.TokenResult)1 SentinelClusterException (com.alibaba.csp.sentinel.cluster.exception.SentinelClusterException)1 FlowTokenResponseData (com.alibaba.csp.sentinel.cluster.response.data.FlowTokenResponseData)1