Search in sources :

Example 1 with RateLimitGRPCV2BlockingStub

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;
}
Also used : RateLimitGRPCV2BlockingStub(com.tencent.polaris.ratelimit.client.pb.RateLimitGRPCV2Grpc.RateLimitGRPCV2BlockingStub) RateLimitGRPCV2Stub(com.tencent.polaris.ratelimit.client.pb.RateLimitGRPCV2Grpc.RateLimitGRPCV2Stub) ManagedChannel(io.grpc.ManagedChannel) RateLimitRequest(com.tencent.polaris.ratelimit.client.pb.RatelimitV2.RateLimitRequest)

Example 2 with RateLimitGRPCV2BlockingStub

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;
}
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)2 RateLimitGRPCV2Stub (com.tencent.polaris.ratelimit.client.pb.RateLimitGRPCV2Grpc.RateLimitGRPCV2Stub)1 RateLimitRequest (com.tencent.polaris.ratelimit.client.pb.RatelimitV2.RateLimitRequest)1 TimeAdjustRequest (com.tencent.polaris.ratelimit.client.pb.RatelimitV2.TimeAdjustRequest)1 TimeAdjustResponse (com.tencent.polaris.ratelimit.client.pb.RatelimitV2.TimeAdjustResponse)1 ManagedChannel (io.grpc.ManagedChannel)1