use of com.tencent.polaris.ratelimit.client.pb.RateLimitGRPCV2Grpc.RateLimitGRPCV2BlockingStub in project polaris-java by polarismesh.
the class StreamCounterSet method checkAndCreateResource.
/**
* 获取同步阻塞的客户端
*
* @return 同步阻塞的客户端
*/
private StreamResource checkAndCreateResource(ServiceIdentifier serviceIdentifier, RateLimitWindow rateLimitWindow) {
StreamResource streamResource = currentStreamResource.get();
if (null != streamResource && !streamResource.endStream.get()) {
return streamResource;
}
long lastConnectFailTimeMilli = 0;
if (null != streamResource) {
lastConnectFailTimeMilli = streamResource.lastConnectFailTimeMilli.get();
}
ManagedChannel channel = createConnection(lastConnectFailTimeMilli);
if (null == channel) {
return null;
}
RateLimitGRPCV2Stub rateLimitGRPCV2Stub = RateLimitGRPCV2Grpc.newStub(channel);
StreamObserver<RateLimitRequest> streamClient = rateLimitGRPCV2Stub.service(streamResource);
RateLimitGRPCV2BlockingStub rateLimitGRPCV2BlockingStub = RateLimitGRPCV2Grpc.newBlockingStub(channel);
streamResource = new StreamResource(channel, streamClient, rateLimitGRPCV2BlockingStub);
currentStreamResource.set(streamResource);
if (initRecord.get(serviceIdentifier) == null) {
initRecord.putIfAbsent(serviceIdentifier, new InitializeRecord(rateLimitWindow));
}
return streamResource;
}
use of com.tencent.polaris.ratelimit.client.pb.RateLimitGRPCV2Grpc.RateLimitGRPCV2BlockingStub in project polaris-java by polarismesh.
the class RemoteSyncTask method adjustTime.
/**
* 调整时间
*
* @param streamCounterSet streamCounterSet
*/
private boolean adjustTime(StreamCounterSet streamCounterSet) {
long lastSyncTimeMilli = asyncRateLimitConnector.getLastSyncTimeMilli().get();
long sendTimeMilli = System.currentTimeMillis();
// 超过间隔时间才需要调整
if (lastSyncTimeMilli > 0 && sendTimeMilli - lastSyncTimeMilli < RateLimitConstants.STARTUP_DELAY_MS) {
LOG.info("adjustTime need wait.lastSyncTimeMilli:{},sendTimeMilli:{}", lastSyncTimeMilli, sendTimeMilli);
return true;
}
RateLimitGRPCV2BlockingStub client = streamCounterSet.preCheckSync(serviceIdentifier, window);
if (client == null) {
LOG.error("[adjustTime] can not get connection {}", window.getRemoteCluster());
return false;
}
TimeAdjustRequest timeAdjustRequest = TimeAdjustRequest.newBuilder().build();
TimeAdjustResponse timeAdjustResponse = client.timeAdjust(timeAdjustRequest);
long receiveClientTimeMilli = System.currentTimeMillis();
asyncRateLimitConnector.getLastSyncTimeMilli().set(receiveClientTimeMilli);
// 服务端时间
long serverTimestamp = timeAdjustResponse.getServerTimestamp();
long latency = receiveClientTimeMilli - sendTimeMilli;
long timeDiff = serverTimestamp + latency / 2 - receiveClientTimeMilli;
asyncRateLimitConnector.getTimeDiff().set(timeDiff);
LOG.info("[RateLimit]adjust time to server time is {}, latency is {},diff is {}", serverTimestamp, latency, timeDiff);
return true;
}
Aggregations