use of com.linkedin.r2.transport.common.bridge.common.TransportCallback in project rest.li by linkedin.
the class TestHttpBridge method testHttpToRestErrorMessage.
@Test
public void testHttpToRestErrorMessage() throws TimeoutException, InterruptedException, ExecutionException {
FutureCallback<RestResponse> futureCallback = new FutureCallback<RestResponse>();
TransportCallback<RestResponse> callback = new TransportCallbackAdapter<RestResponse>(futureCallback);
TransportCallback<RestResponse> bridgeCallback = HttpBridge.httpToRestCallback(callback);
RestResponse restResponse = new RestResponseBuilder().build();
// Note: FutureCallback will fail if called twice. An exception would be raised on the current
// thread because we begin the callback sequence here in onResponse.
// (test originally added due to bug with double callback invocation)
bridgeCallback.onResponse(TransportResponseImpl.<RestResponse>error(new RestException(restResponse)));
RestResponse resp = futureCallback.get(30, TimeUnit.SECONDS);
// should have unpacked restResponse from the RestException that we passed in without
// propagating the actual exception
Assert.assertSame(resp, restResponse);
}
use of com.linkedin.r2.transport.common.bridge.common.TransportCallback in project rest.li by linkedin.
the class RAPResponseHandler method channelRead0.
@Override
protected void channelRead0(ChannelHandlerContext ctx, RestResponse response) throws Exception {
final Map<String, String> headers = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
final Map<String, String> wireAttrs = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
headers.putAll(response.getHeaders());
wireAttrs.putAll(WireAttributeHelper.removeWireAttributes(headers));
final RestResponse newResponse = new RestResponseBuilder(response).unsafeSetHeaders(headers).build();
// In general there should always be a callback to handle a received message,
// but it could have been removed due to a previous exception or closure on the
// channel
TransportCallback<RestResponse> callback = ctx.channel().attr(CALLBACK_ATTR_KEY).getAndRemove();
if (callback != null) {
LOG.debug("{}: handling a response", ctx.channel().remoteAddress());
callback.onResponse(TransportResponseImpl.success(newResponse, wireAttrs));
} else {
LOG.debug("{}: dropped a response", ctx.channel().remoteAddress());
}
ctx.fireChannelRead(response);
}
use of com.linkedin.r2.transport.common.bridge.common.TransportCallback in project rest.li by linkedin.
the class RAPStreamResponseHandler method channelRead0.
@Override
protected void channelRead0(ChannelHandlerContext ctx, StreamResponse response) throws Exception {
final Map<String, String> headers = new HashMap<String, String>(response.getHeaders());
final Map<String, String> wireAttrs = new HashMap<String, String>(WireAttributeHelper.removeWireAttributes(headers));
final StreamResponse newResponse = new StreamResponseBuilder(response).unsafeSetHeaders(headers).build(response.getEntityStream());
// In general there should always be a callback to handle a received message,
// but it could have been removed due to a previous exception or closure on the
// channel
TransportCallback<StreamResponse> callback = ctx.channel().attr(CALLBACK_ATTR_KEY).getAndRemove();
if (callback != null) {
LOG.debug("{}: handling a response", ctx.channel().remoteAddress());
callback.onResponse(TransportResponseImpl.success(newResponse, wireAttrs));
} else {
LOG.debug("{}: dropped a response", ctx.channel().remoteAddress());
}
}
use of com.linkedin.r2.transport.common.bridge.common.TransportCallback in project rest.li by linkedin.
the class StreamExecutionCallback method onResponse.
@Override
public void onResponse(TransportResponse<StreamResponse> response) {
final TransportCallback<StreamResponse> callback = _callbackRef.getAndSet(null);
if (callback != null) {
final TransportResponse<StreamResponse> wrappedResponse;
if (response.hasError()) {
wrappedResponse = response;
} else {
EventLoopConnector connector = new EventLoopConnector(response.getResponse().getEntityStream());
StreamResponse newResponse = response.getResponse().builder().build(EntityStreams.newEntityStream(connector));
wrappedResponse = TransportResponseImpl.success(newResponse, response.getWireAttributes());
}
trySchedule(() -> callback.onResponse(wrappedResponse));
} else {
LOG.warn("Received response {} while _callback is null. Ignored.", response.getResponse());
}
}
use of com.linkedin.r2.transport.common.bridge.common.TransportCallback in project rest.li by linkedin.
the class HttpNettyClient method shutdown.
@Override
public void shutdown(final Callback<None> callback) {
LOG.info("Shutdown requested");
if (_state.compareAndSet(State.RUNNING, State.SHUTTING_DOWN)) {
LOG.info("Shutting down");
final long deadline = System.currentTimeMillis() + _shutdownTimeout;
TimeoutCallback<None> closeChannels = new TimeoutCallback<None>(_scheduler, _shutdownTimeout, TimeUnit.MILLISECONDS, new Callback<None>() {
private void finishShutdown() {
_state.set(State.REQUESTS_STOPPING);
// Timeout any waiters which haven't received a Channel yet
for (Callback<Channel> callback : _channelPoolManager.cancelWaiters()) {
callback.onError(new TimeoutException("Operation did not complete before shutdown"));
}
// Timeout any requests still pending response
for (Channel c : _allChannels) {
TransportCallback<RestResponse> callback = c.attr(RAPResponseHandler.CALLBACK_ATTR_KEY).getAndRemove();
if (callback != null) {
errorResponse(callback, new TimeoutException("Operation did not complete before shutdown"));
}
}
// Close all active and idle Channels
final TimeoutRunnable afterClose = new TimeoutRunnable(_scheduler, deadline - System.currentTimeMillis(), TimeUnit.MILLISECONDS, new Runnable() {
@Override
public void run() {
_state.set(State.SHUTDOWN);
LOG.info("Shutdown complete");
callback.onSuccess(None.none());
}
}, "Timed out waiting for channels to close, continuing shutdown");
_allChannels.close().addListener(new ChannelGroupFutureListener() {
@Override
public void operationComplete(ChannelGroupFuture channelGroupFuture) throws Exception {
if (!channelGroupFuture.isSuccess()) {
LOG.warn("Failed to close some connections, ignoring");
}
afterClose.run();
}
});
}
@Override
public void onSuccess(None none) {
LOG.info("All connection pools shut down, closing all channels");
finishShutdown();
}
@Override
public void onError(Throwable e) {
LOG.warn("Error shutting down HTTP connection pools, ignoring and continuing shutdown", e);
finishShutdown();
}
}, "Connection pool shutdown timeout exceeded (" + _shutdownTimeout + "ms)");
_channelPoolManager.shutdown(closeChannels);
_jmxManager.onProviderShutdown(_channelPoolManager);
} else {
callback.onError(new IllegalStateException("Shutdown has already been requested."));
}
}
Aggregations