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;
}
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);
}
}
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;
}
Aggregations