use of com.linkedin.r2.message.stream.StreamResponse in project rest.li by linkedin.
the class TestHttpNettyStreamClient method testStreamRequests.
/**
* Tests implementations of {@link AbstractNettyStreamClient} with different request dimensions.
*
* @param client Client implementation of {@link AbstractNettyStreamClient}
* @param method HTTP request method
* @param requestSize Request content size
* @param responseSize Response content size
* @param isFullRequest Whether to buffer a full request before stream
* @throws Exception
*/
@Test(dataProvider = "requestResponseParameters")
public void testStreamRequests(AbstractNettyStreamClient client, String method, int requestSize, int responseSize, boolean isFullRequest) throws Exception {
AtomicInteger succeeded = new AtomicInteger(0);
AtomicInteger failed = new AtomicInteger(0);
Server server = new HttpServerBuilder().responseSize(responseSize).build();
try {
server.start();
CountDownLatch latch = new CountDownLatch(REQUEST_COUNT);
for (int i = 0; i < REQUEST_COUNT; i++) {
StreamRequest request = new StreamRequestBuilder(new URI(URL)).setMethod(method).setHeader(HttpHeaderNames.HOST.toString(), HOST_NAME.toString()).build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(new byte[requestSize]))));
RequestContext context = new RequestContext();
context.putLocalAttr(R2Constants.IS_FULL_REQUEST, isFullRequest);
client.streamRequest(request, context, new HashMap<>(), new TransportCallbackAdapter<>(new Callback<StreamResponse>() {
@Override
public void onSuccess(StreamResponse response) {
response.getEntityStream().setReader(new Reader() {
ReadHandle _rh;
int _consumed = 0;
@Override
public void onDataAvailable(ByteString data) {
_consumed += data.length();
_rh.request(1);
}
@Override
public void onDone() {
succeeded.incrementAndGet();
latch.countDown();
}
@Override
public void onError(Throwable e) {
failed.incrementAndGet();
latch.countDown();
}
@Override
public void onInit(ReadHandle rh) {
_rh = rh;
_rh.request(1);
}
});
}
@Override
public void onError(Throwable e) {
failed.incrementAndGet();
latch.countDown();
}
}));
}
if (!latch.await(30, TimeUnit.SECONDS)) {
Assert.fail("Timeout waiting for responses. " + succeeded + " requests succeeded and " + failed + " requests failed out of total " + REQUEST_COUNT + " requests");
}
Assert.assertEquals(latch.getCount(), 0);
Assert.assertEquals(failed.get(), 0);
Assert.assertEquals(succeeded.get(), REQUEST_COUNT);
FutureCallback<None> shutdownCallback = new FutureCallback<>();
client.shutdown(shutdownCallback);
shutdownCallback.get(30, TimeUnit.SECONDS);
} finally {
server.stop();
}
}
use of com.linkedin.r2.message.stream.StreamResponse 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);
}
}
use of com.linkedin.r2.message.stream.StreamResponse in project rest.li by linkedin.
the class Http2FrameListener method onHeadersRead.
@Override
public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int padding, boolean endOfStream) throws Http2Exception {
LOG.debug("Received HTTP/2 HEADERS frame, stream={}, end={}, headers={}, padding={}bytes", new Object[] { streamId, endOfStream, headers.size(), padding });
// Ignores response for the upgrade request
if (streamId == Http2CodecUtil.HTTP_UPGRADE_STREAM_ID) {
return;
}
final StreamResponseBuilder builder = new StreamResponseBuilder();
// Process HTTP/2 pseudo headers
if (headers.status() != null) {
builder.setStatus(Integer.parseInt(headers.status().toString()));
}
if (headers.authority() != null) {
builder.addHeaderValue(HttpHeaderNames.HOST.toString(), headers.authority().toString());
}
// Process other HTTP headers
for (Map.Entry<CharSequence, CharSequence> header : headers) {
if (Http2Headers.PseudoHeaderName.isPseudoHeader(header.getKey())) {
// Do no set HTTP/2 pseudo headers to response
continue;
}
final String key = header.getKey().toString();
final String value = header.getValue().toString();
if (key.equalsIgnoreCase(HttpConstants.RESPONSE_COOKIE_HEADER_NAME)) {
builder.addCookie(value);
} else {
builder.unsafeAddHeaderValue(key, value);
}
}
// Gets async pool handle from stream properties
Http2Connection.PropertyKey handleKey = ctx.channel().attr(Http2ClientPipelineInitializer.CHANNEL_POOL_HANDLE_ATTR_KEY).get();
TimeoutAsyncPoolHandle<?> handle = _connection.stream(streamId).removeProperty(handleKey);
if (handle == null) {
_lifecycleManager.onError(ctx, Http2Exception.connectionError(Http2Error.PROTOCOL_ERROR, "No channel pool handle is associated with this stream", streamId));
return;
}
final StreamResponse response;
if (endOfStream) {
response = builder.build(EntityStreams.emptyStream());
ctx.fireChannelRead(handle);
} else {
// Associate an entity stream writer to the HTTP/2 stream
final TimeoutBufferedWriter writer = new TimeoutBufferedWriter(ctx, streamId, _maxContentLength, handle);
if (_connection.stream(streamId).setProperty(_writerKey, writer) != null) {
_lifecycleManager.onError(ctx, Http2Exception.connectionError(Http2Error.PROTOCOL_ERROR, "Another writer has already been associated with current stream ID", streamId));
return;
}
// Prepares StreamResponse for the channel pipeline
EntityStream entityStream = EntityStreams.newEntityStream(writer);
response = builder.build(entityStream);
}
// Gets callback from stream properties
Http2Connection.PropertyKey callbackKey = ctx.channel().attr(Http2ClientPipelineInitializer.CALLBACK_ATTR_KEY).get();
TransportCallback<?> callback = _connection.stream(streamId).removeProperty(callbackKey);
if (callback != null) {
ctx.fireChannelRead(new ResponseWithCallback<Response, TransportCallback<?>>(response, callback));
}
}
use of com.linkedin.r2.message.stream.StreamResponse in project rest.li by linkedin.
the class TestStreamRequest method testRequestLarge.
@Test
public void testRequestLarge() throws Exception {
for (Client client : clients()) {
final long totalBytes = LARGE_BYTES_NUM;
EntityStream entityStream = EntityStreams.newEntityStream(new BytesWriter(totalBytes, BYTE));
StreamRequestBuilder builder = new StreamRequestBuilder(Bootstrap.createHttpURI(PORT, LARGE_URI));
StreamRequest request = builder.setMethod("POST").build(entityStream);
final AtomicInteger status = new AtomicInteger(-1);
final CountDownLatch latch = new CountDownLatch(1);
Callback<StreamResponse> callback = expectSuccessCallback(latch, status);
client.streamRequest(request, callback);
latch.await(60000, TimeUnit.MILLISECONDS);
Assert.assertEquals(status.get(), RestStatus.OK);
BytesReader reader = _checkRequestHandler.getReader();
Assert.assertNotNull(reader);
Assert.assertEquals(totalBytes, reader.getTotalBytes());
Assert.assertTrue(reader.allBytesCorrect());
}
}
use of com.linkedin.r2.message.stream.StreamResponse in project rest.li by linkedin.
the class TestStreamRequest method test404.
// jetty 404 tests singled out
@Test(enabled = false)
public void test404() throws Exception {
for (Client client : clients()) {
final long totalBytes = TINY_BYTES_NUM;
EntityStream entityStream = EntityStreams.newEntityStream(new BytesWriter(totalBytes, BYTE));
StreamRequestBuilder builder = new StreamRequestBuilder(Bootstrap.createHttpURI(PORT, URI.create("/boo")));
StreamRequest request = builder.setMethod("POST").build(entityStream);
final AtomicInteger status = new AtomicInteger(-1);
final CountDownLatch latch = new CountDownLatch(1);
Callback<StreamResponse> callback = expectErrorCallback(latch, status);
client.streamRequest(request, callback);
latch.await(60000, TimeUnit.MILLISECONDS);
Assert.assertEquals(status.get(), 404);
}
}
Aggregations