Search in sources :

Example 1 with StreamExecutionCallback

use of com.linkedin.r2.netty.callback.StreamExecutionCallback in project rest.li by linkedin.

the class AbstractNettyStreamClient method writeRequestWithTimeout.

private void writeRequestWithTimeout(final StreamRequest request, RequestContext requestContext, Map<String, String> wireAttrs, TransportCallback<StreamResponse> callback) {
    StreamExecutionCallback executionCallback = new StreamExecutionCallback(_callbackExecutors, callback);
    // 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
    final TimeoutTransportCallback<StreamResponse> timeoutCallback = new TimeoutTransportCallback<StreamResponse>(_scheduler, _requestTimeout, TimeUnit.MILLISECONDS, executionCallback, _requestTimeoutMessage);
    final StreamRequest requestWithWireAttrHeaders = request.builder().overwriteHeaders(WireAttributeHelper.toWireAttributes(wireAttrs)).build(request.getEntityStream());
    // talk to legacy R2 servers without problem if they're just using restRequest (full request).
    if (isFullRequest(requestContext)) {
        Messages.toRestRequest(requestWithWireAttrHeaders, new Callback<RestRequest>() {

            @Override
            public void onError(Throwable e) {
                errorResponse(timeoutCallback, e);
            }

            @Override
            public void onSuccess(RestRequest restRequest) {
                writeRequest(restRequest, requestContext, timeoutCallback);
            }
        });
    } else {
        writeRequest(requestWithWireAttrHeaders, requestContext, timeoutCallback);
    }
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) StreamRequest(com.linkedin.r2.message.stream.StreamRequest)

Aggregations

RestRequest (com.linkedin.r2.message.rest.RestRequest)1 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)1 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)1