Search in sources :

Example 1 with TimeAdjustResponse

use of com.tencent.polaris.ratelimit.client.pb.RatelimitV2.TimeAdjustResponse 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;
}
Also used : TimeAdjustRequest(com.tencent.polaris.ratelimit.client.pb.RatelimitV2.TimeAdjustRequest) RateLimitGRPCV2BlockingStub(com.tencent.polaris.ratelimit.client.pb.RateLimitGRPCV2Grpc.RateLimitGRPCV2BlockingStub) TimeAdjustResponse(com.tencent.polaris.ratelimit.client.pb.RatelimitV2.TimeAdjustResponse)

Aggregations

RateLimitGRPCV2BlockingStub (com.tencent.polaris.ratelimit.client.pb.RateLimitGRPCV2Grpc.RateLimitGRPCV2BlockingStub)1 TimeAdjustRequest (com.tencent.polaris.ratelimit.client.pb.RatelimitV2.TimeAdjustRequest)1 TimeAdjustResponse (com.tencent.polaris.ratelimit.client.pb.RatelimitV2.TimeAdjustResponse)1