use of com.linkedin.r2.message.stream.StreamResponse in project rest.li by linkedin.
the class TestStreamRequest method testBackPressure.
@Test
public void testBackPressure() throws Exception {
for (Client client : clients()) {
final long totalBytes = SMALL_BYTES_NUM;
TimedBytesWriter writer = new TimedBytesWriter(totalBytes, BYTE);
EntityStream entityStream = EntityStreams.newEntityStream(writer);
StreamRequestBuilder builder = new StreamRequestBuilder(Bootstrap.createHttpURI(PORT, RATE_LIMITED_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);
TimedBytesReader reader = _rateLimitedRequestHandler.getReader();
Assert.assertNotNull(reader);
Assert.assertEquals(totalBytes, reader.getTotalBytes());
Assert.assertTrue(reader.allBytesCorrect());
long clientSendTimespan = writer.getStopTime() - writer.getStartTime();
long serverReceiveTimespan = reader.getStopTime() - reader.getStartTime();
Assert.assertTrue(serverReceiveTimespan > 1000);
double diff = Math.abs(serverReceiveTimespan - clientSendTimespan);
double diffRatio = diff / clientSendTimespan;
// make it generous to reduce the chance occasional test failures
Assert.assertTrue(diffRatio < 0.2);
}
}
use of com.linkedin.r2.message.stream.StreamResponse in project rest.li by linkedin.
the class RestClient method sendStreamRequestImpl.
/**
* Sends an untyped stream request using a callback.
*
* @param requestContext context for the request
* @param uri for resource
* @param method to perform
* @param dataMap request body entity
* @param headers additional headers to be added to the request
* @param cookies the cookies to be sent with the request
* @param methodName the method name (used for finders and actions)
* @param protocolVersion the version of the Rest.li protocol used to build this request
* @param requestOptions contains compression force on/off overrides, request content type and accept types
* @param callback to call on request completion. In the event of an error, the callback
* will receive a {@link com.linkedin.r2.RemoteInvocationException}. If a valid
* error response was received from the remote server, the callback will receive
* a {@link com.linkedin.r2.message.rest.RestException} containing the error details.
*/
private void sendStreamRequestImpl(RequestContext requestContext, URI uri, ResourceMethod method, DataMap dataMap, Map<String, String> headers, List<String> cookies, String methodName, ProtocolVersion protocolVersion, RestliRequestOptions requestOptions, List<Object> streamingAttachments, Callback<StreamResponse> callback) {
try {
final StreamRequest request = buildStreamRequest(uri, method, dataMap, headers, cookies, protocolVersion, requestOptions.getContentType(), requestOptions.getAcceptTypes(), requestOptions.getAcceptResponseAttachments(), streamingAttachments);
String operation = OperationNameGenerator.generate(method, methodName);
requestContext.putLocalAttr(R2Constants.OPERATION, operation);
requestContext.putLocalAttr(R2Constants.REQUEST_COMPRESSION_OVERRIDE, requestOptions.getRequestCompressionOverride());
requestContext.putLocalAttr(R2Constants.RESPONSE_COMPRESSION_OVERRIDE, requestOptions.getResponseCompressionOverride());
_client.streamRequest(request, requestContext, callback);
} catch (Exception e) {
// No need to wrap the exception; RestLiCallbackAdapter.onError() will take care of that
callback.onError(e);
}
}
use of com.linkedin.r2.message.stream.StreamResponse in project rest.li by linkedin.
the class FilterUtil method fireStreamRequestResponse.
// Fires a request, saving the local attributes, and then fires a response with the local
// attributes.
public static void fireStreamRequestResponse(FilterChain fc, StreamRequest req, StreamResponse res) {
final RequestContext context = new RequestContext();
wrapFilterChain(fc).onStreamRequest(req, context, emptyWireAttrs());
wrapFilterChain(fc).onStreamResponse(res, context, emptyWireAttrs());
}
use of com.linkedin.r2.message.stream.StreamResponse in project rest.li by linkedin.
the class AbstractPerfServerFactory method createPureStreamServer.
public Server createPureStreamServer(int port, URI echoUri, final int msg_size, int numHeaders, int headerSize) {
String headerContent = new StringGenerator(headerSize).nextMessage();
StreamRequestHandler handler = new StreamRequestHandler() {
@Override
public void handleRequest(StreamRequest request, RequestContext requestContext, final Callback<StreamResponse> callback) {
request.getEntityStream().setReader(new PerfStreamReader<None>(new Callback<None>() {
@Override
public void onError(Throwable e) {
callback.onError(e);
}
@Override
public void onSuccess(None result) {
StreamResponseBuilder builder = new StreamResponseBuilder();
for (int i = 0; i < numHeaders; i++) {
builder.setHeader(STATIC_HEADER_PREFIX + i, headerContent);
}
callback.onSuccess(builder.build(EntityStreams.newEntityStream(new PerfStreamWriter(msg_size))));
}
}, None.none()));
}
};
final TransportDispatcher dispatcher = new TransportDispatcherBuilder().addStreamHandler(echoUri, handler).build();
return createServer(port, dispatcher, true);
}
use of com.linkedin.r2.message.stream.StreamResponse in project rest.li by linkedin.
the class LoggingFilter method onStreamResponse.
@Override
public void onStreamResponse(StreamResponse res, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<StreamRequest, StreamResponse> nextFilter) {
EntityStream entityStream = res.getEntityStream();
entityStream.addObserver(new Observer() {
private long startTime;
private long bytesNum = 0;
@Override
public void onDataAvailable(ByteString data) {
if (bytesNum == 0) {
startTime = System.nanoTime();
}
bytesNum += data.length();
}
@Override
public void onDone() {
long stopTime = System.nanoTime();
_log.info("Status: success. Total bytes streamed: " + bytesNum + ". Total stream time: " + (stopTime - startTime) + " nano seconds.");
}
@Override
public void onError(Throwable e) {
long stopTime = System.nanoTime();
_log.error("Status: failed. Total bytes streamed: " + bytesNum + ". Total stream time before failure: " + (stopTime - startTime) + " nano seconds.");
}
});
}
Aggregations