Search in sources :

Example 1 with ConnectableBufferOutputStream

use of io.servicetalk.concurrent.api.internal.ConnectableBufferOutputStream in project servicetalk by apple.

the class DefaultContainerResponseWriter method writeResponseStatusAndHeaders.

@Nullable
@Override
public OutputStream writeResponseStatusAndHeaders(final long contentLength, final ContainerResponse responseContext) throws ContainerException {
    if (!stateUpdater.compareAndSet(this, STATE_REQUEST_HANDLING, STATE_RESPONSE_WRITING)) {
        // Request has been cancelled so we do not send a response and return no OutputStream for Jersey to write to
        return null;
    }
    final Publisher<Buffer> content = getResponseBufferPublisher(request);
    // contentLength is >= 0 if the entity content length in bytes is known to Jersey, otherwise -1
    if (content != null) {
        sendResponse(UNKNOWN_RESPONSE_LENGTH, content, responseContext);
        return null;
    } else if (contentLength == 0 || isHeadRequest()) {
        sendResponse(contentLength, null, responseContext);
        return null;
    } else if (contentLength > 0) {
        // Jersey has buffered the full response body: bypass streaming response and use an optimized path instead
        return new BufferedResponseOutputStream(serviceCtx.executionContext().bufferAllocator(), buf -> sendResponse(contentLength, Publisher.from(buf), responseContext));
    }
    // OIO adapted streaming response of unknown length
    final ConnectableBufferOutputStream os = new ConnectableBufferOutputStream(serviceCtx.executionContext().bufferAllocator());
    sendResponse(contentLength, os.connect(), responseContext);
    return new CopyingOutputStream(os);
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) ConnectableBufferOutputStream(io.servicetalk.concurrent.api.internal.ConnectableBufferOutputStream) Nullable(javax.annotation.Nullable)

Example 2 with ConnectableBufferOutputStream

use of io.servicetalk.concurrent.api.internal.ConnectableBufferOutputStream in project servicetalk by apple.

the class ConnectableBufferOutputStreamBenchmark method setup.

@Setup(Level.Iteration)
public void setup() {
    data = new byte[dataSize];
    cbos = new ConnectableBufferOutputStream(PREFER_HEAP_ALLOCATOR);
    publisher = cbos.connect();
    // Don't remove this, JMH somehow provides a default which break everything
    subscription = null;
}
Also used : ConnectableBufferOutputStream(io.servicetalk.concurrent.api.internal.ConnectableBufferOutputStream) Setup(org.openjdk.jmh.annotations.Setup)

Aggregations

ConnectableBufferOutputStream (io.servicetalk.concurrent.api.internal.ConnectableBufferOutputStream)2 Buffer (io.servicetalk.buffer.api.Buffer)1 Nullable (javax.annotation.Nullable)1 Setup (org.openjdk.jmh.annotations.Setup)1