io.servicetalk.http.netty.AbstractStreamingHttpConnection Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of servicetalk-http-netty Show documentation
Show all versions of servicetalk-http-netty Show documentation
A networking framework that evolves with your application
The newest version!
/*
* Copyright © 2018-2019, 2021-2022 Apple Inc. and the ServiceTalk project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.servicetalk.http.netty;
import io.servicetalk.client.api.ConsumableEvent;
import io.servicetalk.concurrent.api.Completable;
import io.servicetalk.concurrent.api.Publisher;
import io.servicetalk.concurrent.api.Single;
import io.servicetalk.concurrent.api.TerminalSignalConsumer;
import io.servicetalk.http.api.FilterableStreamingHttpConnection;
import io.servicetalk.http.api.HttpConnectionContext;
import io.servicetalk.http.api.HttpEventKey;
import io.servicetalk.http.api.HttpExecutionContext;
import io.servicetalk.http.api.HttpExecutionStrategy;
import io.servicetalk.http.api.HttpHeadersFactory;
import io.servicetalk.http.api.HttpMetaData;
import io.servicetalk.http.api.HttpRequestMetaData;
import io.servicetalk.http.api.HttpRequestMethod;
import io.servicetalk.http.api.HttpResponseMetaData;
import io.servicetalk.http.api.StreamingHttpRequest;
import io.servicetalk.http.api.StreamingHttpRequestResponseFactory;
import io.servicetalk.http.api.StreamingHttpResponse;
import io.servicetalk.http.api.StreamingHttpResponseFactory;
import io.servicetalk.http.netty.ReservableRequestConcurrencyControllers.IgnoreConsumedEvent;
import io.servicetalk.transport.api.IoThreadFactory;
import io.servicetalk.transport.netty.internal.FlushStrategy;
import io.servicetalk.transport.netty.internal.NettyConnectionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Nullable;
import static io.servicetalk.concurrent.api.Executors.immediate;
import static io.servicetalk.concurrent.api.Publisher.failed;
import static io.servicetalk.concurrent.api.Publisher.from;
import static io.servicetalk.concurrent.api.Single.defer;
import static io.servicetalk.concurrent.api.Single.succeeded;
import static io.servicetalk.http.api.HttpApiConversions.isPayloadEmpty;
import static io.servicetalk.http.api.HttpApiConversions.isSafeToAggregate;
import static io.servicetalk.http.api.HttpContextKeys.HTTP_EXECUTION_STRATEGY_KEY;
import static io.servicetalk.http.api.HttpEventKey.MAX_CONCURRENCY;
import static io.servicetalk.http.api.HttpEventKey.newKey;
import static io.servicetalk.http.api.StreamingHttpResponses.newTransportResponse;
import static io.servicetalk.http.netty.HeaderUtils.REQ_EXPECT_CONTINUE;
import static io.servicetalk.http.netty.HeaderUtils.addRequestTransferEncodingIfNecessary;
import static io.servicetalk.http.netty.HeaderUtils.canAddRequestContentLength;
import static io.servicetalk.http.netty.HeaderUtils.emptyMessageBody;
import static io.servicetalk.http.netty.HeaderUtils.flatEmptyMessage;
import static io.servicetalk.http.netty.HeaderUtils.setRequestContentLength;
import static io.servicetalk.http.netty.HeaderUtils.shouldAppendTrailers;
import static io.servicetalk.transport.netty.internal.FlushStrategies.flushOnEnd;
import static java.util.Objects.requireNonNull;
abstract class AbstractStreamingHttpConnection
implements FilterableStreamingHttpConnection {
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractStreamingHttpConnection.class);
static final IgnoreConsumedEvent ZERO_MAX_CONCURRENCY_EVENT = new IgnoreConsumedEvent<>(0);
static final HttpEventKey> MAX_CONCURRENCY_NO_OFFLOADING =
newKey("max-concurrency-no-offloading", generify(ConsumableEvent.class));
final CC connection;
private final HttpConnectionContext connectionContext;
private final Publisher> maxConcurrencySetting;
private final StreamingHttpRequestResponseFactory reqRespFactory;
private final HttpHeadersFactory headersFactory;
private final boolean allowDropTrailersReadFromTransport;
AbstractStreamingHttpConnection(final CC conn, final int maxPipelinedRequests,
final StreamingHttpRequestResponseFactory reqRespFactory,
final HttpHeadersFactory headersFactory,
final boolean allowDropTrailersReadFromTransport) {
this.connection = requireNonNull(conn);
this.connectionContext = new DefaultNettyHttpConnectionContext(conn);
this.reqRespFactory = requireNonNull(reqRespFactory);
// This Publisher currently provides replay() semantics in that all sources support multiple subscribers and
// from(..)/succeeded(..) will provide the same state to every subscriber. If these semantics change the
// replay() operator should be used to preserve these semantics.
maxConcurrencySetting = from(new IgnoreConsumedEvent<>(maxPipelinedRequests))
.concat(connection.onClosing())
.concat(succeeded(ZERO_MAX_CONCURRENCY_EVENT));
this.headersFactory = headersFactory;
this.allowDropTrailersReadFromTransport = allowDropTrailersReadFromTransport;
}
@Override
public final HttpConnectionContext connectionContext() {
return connectionContext;
}
@SuppressWarnings("unchecked")
@Override
public final Publisher transportEventStream(final HttpEventKey eventKey) {
if (eventKey == MAX_CONCURRENCY_NO_OFFLOADING) {
return (Publisher) maxConcurrencySetting;
} else if (eventKey == MAX_CONCURRENCY) {
return (Publisher) maxConcurrencySetting
.publishOn(executionContext().executionStrategy().isEventOffloaded() ?
executionContext().executor() : immediate(),
IoThreadFactory.IoThread::currentThreadIsIoThread);
} else {
return failed(new IllegalArgumentException("Unknown key: " + eventKey));
}
}
private Single makeRequest(final HttpRequestMetaData requestMetaData,
final Publisher