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