Search in sources :

Example 6 with TimeoutCallback

use of com.linkedin.r2.transport.http.client.TimeoutCallback in project rest.li by linkedin.

the class SimpleLoadBalancer method getLoadBalancedServiceProperties.

@Override
public void getLoadBalancedServiceProperties(String serviceName, Callback<ServiceProperties> callback) {
    boolean waitForUpdatedValue = _timeout > 0;
    // if timeout is 0, we must not add the timeout callback, otherwise it would trigger immediately
    if (waitForUpdatedValue) {
        Callback<ServiceProperties> finalCallback = callback;
        callback = new TimeoutCallback<>(_executor, _timeout, _unit, new Callback<ServiceProperties>() {

            @Override
            public void onError(Throwable e) {
                finalCallback.onError(new ServiceUnavailableException(serviceName, "PEGA_1011. " + e.getMessage(), e));
            }

            @Override
            public void onSuccess(ServiceProperties result) {
                finalCallback.onSuccess(result);
            }
        }, "Timeout while fetching service");
    }
    getLoadBalancedServiceProperties(serviceName, waitForUpdatedValue, callback);
}
Also used : ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) LoadBalancerStateListenerCallback(com.linkedin.d2.balancer.LoadBalancerState.LoadBalancerStateListenerCallback) NullStateListenerCallback(com.linkedin.d2.balancer.LoadBalancerState.NullStateListenerCallback) TimeoutCallback(com.linkedin.r2.transport.http.client.TimeoutCallback) PropertyEventShutdownCallback(com.linkedin.d2.discovery.event.PropertyEventThread.PropertyEventShutdownCallback) FutureCallback(com.linkedin.common.callback.FutureCallback) Callback(com.linkedin.common.callback.Callback) ServiceUnavailableException(com.linkedin.d2.balancer.ServiceUnavailableException)

Example 7 with TimeoutCallback

use of com.linkedin.r2.transport.http.client.TimeoutCallback in project rest.li by linkedin.

the class SimpleLoadBalancer method listenToServiceAndCluster.

private void listenToServiceAndCluster(String serviceName, Callback<ServiceProperties> callback) {
    boolean waitForUpdatedValue = _timeout > 0;
    // if timeout is 0, we must not add the timeout callback, otherwise it would trigger immediately
    if (waitForUpdatedValue) {
        Callback<ServiceProperties> finalCallback = callback;
        callback = new TimeoutCallback<>(_executor, _timeout, _unit, new Callback<ServiceProperties>() {

            @Override
            public void onError(Throwable e) {
                finalCallback.onError(new ServiceUnavailableException(serviceName, "PEGA_1004. " + e.getMessage(), e));
            }

            @Override
            public void onSuccess(ServiceProperties result) {
                finalCallback.onSuccess(result);
            }
        }, "Timeout while fetching service");
    }
    listenToServiceAndCluster(serviceName, waitForUpdatedValue, callback);
}
Also used : ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) LoadBalancerStateListenerCallback(com.linkedin.d2.balancer.LoadBalancerState.LoadBalancerStateListenerCallback) NullStateListenerCallback(com.linkedin.d2.balancer.LoadBalancerState.NullStateListenerCallback) TimeoutCallback(com.linkedin.r2.transport.http.client.TimeoutCallback) PropertyEventShutdownCallback(com.linkedin.d2.discovery.event.PropertyEventThread.PropertyEventShutdownCallback) FutureCallback(com.linkedin.common.callback.FutureCallback) Callback(com.linkedin.common.callback.Callback) ServiceUnavailableException(com.linkedin.d2.balancer.ServiceUnavailableException)

Example 8 with TimeoutCallback

use of com.linkedin.r2.transport.http.client.TimeoutCallback in project rest.li by linkedin.

the class AbstractNettyClient method writeRequest.

/**
 * This method calls the user defined method {@link AbstractNettyClient#doWriteRequest(Request, RequestContext, SocketAddress, Map, TimeoutTransportCallback, long)}
 * after having checked that the client is still running and resolved the DNS
 */
private void writeRequest(Req request, RequestContext requestContext, Map<String, String> wireAttrs, TransportCallback<Res> callback) {
    // Decorates callback
    TransportCallback<Res> executionCallback = getExecutionCallback(callback);
    TransportCallback<Res> shutdownAwareCallback = getShutdownAwareCallback(executionCallback);
    // Resolves request timeout
    long requestTimeout = HttpNettyClient.resolveRequestTimeout(requestContext, _requestTimeout);
    // By wrapping the callback in a Timeout callback before passing it along, we deny the rest
    // of the code access to the unwrapped callback.  This ensures two things:
    // 1. The user callback will always be invoked, since the Timeout will eventually expire
    // 2. The user callback is never invoked more than once
    TimeoutTransportCallback<Res> timeoutCallback = new TimeoutTransportCallback<>(_scheduler, requestTimeout, TimeUnit.MILLISECONDS, shutdownAwareCallback, "Exceeded request timeout of " + requestTimeout + "ms");
    // check lifecycle
    NettyClientState state = _state.get();
    if (state != NettyClientState.RUNNING) {
        errorResponse(callback, new IllegalStateException("Client is " + state));
        return;
    }
    // resolve address
    final SocketAddress address;
    try {
        TimingContextUtil.markTiming(requestContext, TIMING_KEY);
        address = HttpNettyClient.resolveAddress(request, requestContext);
        TimingContextUtil.markTiming(requestContext, TIMING_KEY);
    } catch (UnknownHostException | UnknownSchemeException e) {
        errorResponse(callback, e);
        return;
    }
    doWriteRequest(request, requestContext, address, wireAttrs, timeoutCallback, requestTimeout);
}
Also used : UnknownHostException(java.net.UnknownHostException) UnknownSchemeException(com.linkedin.r2.netty.common.UnknownSchemeException) TimeoutTransportCallback(com.linkedin.r2.transport.http.client.TimeoutTransportCallback) NettyClientState(com.linkedin.r2.netty.common.NettyClientState) SocketAddress(java.net.SocketAddress)

Aggregations

Callback (com.linkedin.common.callback.Callback)4 TimeoutCallback (com.linkedin.r2.transport.http.client.TimeoutCallback)4 FutureCallback (com.linkedin.common.callback.FutureCallback)2 None (com.linkedin.common.util.None)2 LoadBalancerStateListenerCallback (com.linkedin.d2.balancer.LoadBalancerState.LoadBalancerStateListenerCallback)2 NullStateListenerCallback (com.linkedin.d2.balancer.LoadBalancerState.NullStateListenerCallback)2 ServiceUnavailableException (com.linkedin.d2.balancer.ServiceUnavailableException)2 ServiceProperties (com.linkedin.d2.balancer.properties.ServiceProperties)2 PropertyEventShutdownCallback (com.linkedin.d2.discovery.event.PropertyEventThread.PropertyEventShutdownCallback)2 TimeoutRunnable (com.linkedin.r2.util.TimeoutRunnable)2 ChannelGroupFutureListener (io.netty.channel.group.ChannelGroupFutureListener)2 SimpleCallback (com.linkedin.common.callback.SimpleCallback)1 DarkClusterConfigMap (com.linkedin.d2.DarkClusterConfigMap)1 ClusterProperties (com.linkedin.d2.balancer.properties.ClusterProperties)1 SizeLimitExceededException (com.linkedin.r2.SizeLimitExceededException)1 RestRequest (com.linkedin.r2.message.rest.RestRequest)1 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)1 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)1 NettyClientState (com.linkedin.r2.netty.common.NettyClientState)1 UnknownSchemeException (com.linkedin.r2.netty.common.UnknownSchemeException)1