use of com.linkedin.r2.transport.http.client.stream.OrderedEntityStreamReader in project rest.li by linkedin.
the class RAPStreamRequestEncoder method write.
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if (msg instanceof StreamRequest) {
StreamRequest request = (StreamRequest) msg;
HttpRequest nettyRequest = NettyRequestAdapter.toNettyRequest(request);
ctx.write(nettyRequest, promise);
_currentReader = new OrderedEntityStreamReader(ctx, new BufferedReader(ctx, MAX_BUFFERED_CHUNKS, FLUSH_THRESHOLD));
request.getEntityStream().setReader(_currentReader);
} else {
_currentReader = null;
ctx.write(msg, promise);
}
}
use of com.linkedin.r2.transport.http.client.stream.OrderedEntityStreamReader in project rest.li by linkedin.
the class Http2StreamCodec method write.
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if (!(msg instanceof RequestWithCallback)) {
ctx.write(msg, promise);
return;
}
Request request = ((RequestWithCallback) msg).request();
Http2ConnectionEncoder encoder = encoder();
int streamId = connection().local().incrementAndGetNextStreamId();
final ChannelFuture headersFuture;
if (request instanceof StreamRequest) {
final StreamRequest streamRequest = (StreamRequest) request;
final Http2Headers http2Headers = NettyRequestAdapter.toHttp2Headers(streamRequest);
final BufferedReader bufferedReader = new BufferedReader(ctx, encoder, streamId, ((RequestWithCallback) msg).handle());
final OrderedEntityStreamReader reader = new OrderedEntityStreamReader(ctx, bufferedReader);
streamRequest.getEntityStream().setReader(reader);
LOG.debug("Sent HTTP/2 HEADERS frame, stream={}, end={}, headers={}, padding={}bytes", new Object[] { streamId, NOT_END_STREAM, http2Headers.size(), NO_PADDING });
headersFuture = encoder.writeHeaders(ctx, streamId, http2Headers, NO_PADDING, NOT_END_STREAM, promise);
headersFuture.addListener(future -> {
if (future.isSuccess()) {
reader.request(BufferedReader.MAX_BUFFERED_CHUNKS);
}
});
} else if (request instanceof RestRequest) {
final RestRequest restRequest = (RestRequest) request;
final Http2Headers headers = NettyRequestAdapter.toHttp2Headers(restRequest);
LOG.debug("Sent HTTP/2 HEADERS frame, stream={}, end={}, headers={}, padding={}bytes", new Object[] { streamId, NOT_END_STREAM, headers.size(), NO_PADDING });
headersFuture = encoder.writeHeaders(ctx, streamId, headers, NO_PADDING, NOT_END_STREAM, promise);
headersFuture.addListener(future -> {
if (future.isSuccess()) {
final ByteBuf data = Unpooled.wrappedBuffer(restRequest.getEntity().asByteBuffer());
LOG.debug("Sent HTTP/2 DATA frame, stream={}, end={}, data={}bytes, padding={}bytes", new Object[] { streamId, END_STREAM, data.readableBytes(), NO_PADDING });
encoder.writeData(ctx, streamId, data, NO_PADDING, END_STREAM, ctx.newPromise());
ctx.channel().flush();
}
});
} else {
// Release the handle to put the channel back to the pool
((RequestWithCallback) msg).handle().release();
throw new IllegalArgumentException("Request is neither StreamRequest or RestRequest");
}
final TransportCallback<?> callback = ((RequestWithCallback) msg).callback();
@SuppressWarnings("unchecked") final TimeoutAsyncPoolHandle<Channel> handle = (TimeoutAsyncPoolHandle<Channel>) ((RequestWithCallback) msg).handle();
headersFuture.addListener(future -> {
if (future.isSuccess()) {
// Sets TransportCallback as a stream property to be retrieved later
Http2PipelinePropertyUtil.set(ctx, connection(), streamId, Http2ClientPipelineInitializer.CALLBACK_ATTR_KEY, callback);
// Sets AsyncPoolHandle as a stream property to be retrieved later
Http2PipelinePropertyUtil.set(ctx, connection(), streamId, Http2ClientPipelineInitializer.CHANNEL_POOL_HANDLE_ATTR_KEY, handle);
// Sets a timeout task to reset stream
// Channel pool handle is also released at timeout
handle.addTimeoutTask(() -> {
LOG.debug("Reset stream upon timeout, stream={}", streamId);
resetStream(ctx, streamId, Http2Error.CANCEL.code(), ctx.newPromise());
ctx.flush();
});
} else {
// Invokes callback onResponse with the error thrown during write header or data
callback.onResponse(TransportResponseImpl.error(future.cause()));
// Releases the handle to put the channel back to the pool
handle.release();
// Resets the stream if a stream is created after we sent header
if (connection().stream(streamId) != null) {
LOG.debug("Reset stream upon timeout, stream={}", streamId);
resetStream(ctx, streamId, Http2Error.CANCEL.code(), ctx.newPromise());
ctx.flush();
}
}
});
}
use of com.linkedin.r2.transport.http.client.stream.OrderedEntityStreamReader in project rest.li by linkedin.
the class ClientEntityStreamHandler method write.
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
if (msg instanceof StreamRequest) {
StreamRequest request = (StreamRequest) msg;
// Sets reader after the headers have been flushed on the channel
OrderedEntityStreamReader orderedReader = new OrderedEntityStreamReader(ctx, new StreamReader(ctx));
ctx.write(request).addListener(future -> request.getEntityStream().setReader(orderedReader));
} else {
ctx.write(msg);
}
}
Aggregations