Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gRPC Trailers-Only responses must be a single HEADERS frame #3152

Merged
merged 2 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
import static io.servicetalk.grpc.api.GrpcUtils.setStatus;
import static io.servicetalk.grpc.api.GrpcUtils.setStatusOk;
import static io.servicetalk.grpc.api.GrpcUtils.validateContentType;
import static io.servicetalk.grpc.internal.GrpcContextKeys.TRAILERS_ONLY_RESPONSE;
import static io.servicetalk.http.api.HttpApiConversions.toStreamingHttpService;
import static io.servicetalk.http.api.HttpExecutionStrategies.customStrategyBuilder;
import static io.servicetalk.http.api.HttpExecutionStrategies.defaultStrategy;
Expand Down Expand Up @@ -708,6 +709,7 @@ public void handle(final HttpServiceContext ctx, final BlockingStreamingHttpRequ
methodDescriptor.httpPath(), t);
HttpHeaders trailers;
if (grpcResponse == null || (trailers = grpcResponse.trailers()) == null) {
response.context().put(TRAILERS_ONLY_RESPONSE, Boolean.TRUE);
setStatus(response.headers(), t, allocator);
// Use HTTP response to avoid setting "OK" in trailers and allocating a serializer
response.sendMetaData().close();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright © 2019, 2021 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.grpc.internal;

import io.servicetalk.context.api.ContextMap;

import static io.servicetalk.context.api.ContextMap.Key.newKey;

/**
* All {@link ContextMap.Key}(s) defined for gRPC.
*/
public final class GrpcContextKeys {
/**
* For the blocking server this key allows the router to notify an upstream filter that it is safe to consolidate
* tailing empty data frames when set to true.
*
*/
public static final ContextMap.Key<Boolean> TRAILERS_ONLY_RESPONSE =
newKey("TRAILERS_ONLY_RESPONSE", Boolean.class);

private GrpcContextKeys() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ private Single<GrpcServerContext> doListen(final GrpcServiceFactory<?> serviceFa
private ExecutionContextInterceptorHttpServerBuilder preBuild() {
final ExecutionContextInterceptorHttpServerBuilder interceptor =
new ExecutionContextInterceptorHttpServerBuilder(httpServerBuilderSupplier.get());

interceptor.appendNonOffloadingServiceFilter(GrpcExceptionMapperServiceFilter.INSTANCE);

directCallInitializer.initialize(interceptor);
Expand All @@ -154,6 +153,8 @@ private ExecutionContextInterceptorHttpServerBuilder preBuild() {
}
initializer.initialize(interceptor);

interceptor.appendServiceFilter(GrpcEnforceTrailersOnlyResponseServiceFilter.INSTANCE);

return interceptor;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright © 2019, 2021 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.grpc.netty;

import io.servicetalk.concurrent.api.Single;
import io.servicetalk.http.api.HttpExecutionStrategies;
import io.servicetalk.http.api.HttpExecutionStrategy;
import io.servicetalk.http.api.HttpResponse;
import io.servicetalk.http.api.HttpServiceContext;
import io.servicetalk.http.api.StreamingHttpRequest;
import io.servicetalk.http.api.StreamingHttpResponse;
import io.servicetalk.http.api.StreamingHttpResponseFactory;
import io.servicetalk.http.api.StreamingHttpService;
import io.servicetalk.http.api.StreamingHttpServiceFilter;
import io.servicetalk.http.api.StreamingHttpServiceFilterFactory;

import static io.servicetalk.grpc.internal.GrpcContextKeys.TRAILERS_ONLY_RESPONSE;

final class GrpcEnforceTrailersOnlyResponseServiceFilter implements StreamingHttpServiceFilterFactory {
static final GrpcEnforceTrailersOnlyResponseServiceFilter INSTANCE =
new GrpcEnforceTrailersOnlyResponseServiceFilter();

private GrpcEnforceTrailersOnlyResponseServiceFilter() {
}

@Override
public StreamingHttpServiceFilter create(StreamingHttpService service) {
return new StreamingHttpServiceFilter(service) {
@Override
public Single<StreamingHttpResponse> handle(final HttpServiceContext ctx,
final StreamingHttpRequest request,
final StreamingHttpResponseFactory responseFactory) {
return delegate().handle(ctx, request, responseFactory).flatMap(response -> {
Single<StreamingHttpResponse> mappedResponse;
if (Boolean.TRUE.equals(response.context().get(TRAILERS_ONLY_RESPONSE))) {
mappedResponse = response.toResponse().map(HttpResponse::toStreamingResponse);
} else {
mappedResponse = Single.succeeded(response);
}
return mappedResponse.shareContextOnSubscribe();
});
}
};
}

@Override
public HttpExecutionStrategy requiredOffloads() {
return HttpExecutionStrategies.offloadNone();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,6 @@ private static Collection<Arguments> clientServerParams() {
for (boolean isClientServiceTalk : TRUE_FALSE) {
for (boolean isServerServiceTalk : TRUE_FALSE) {
for (boolean isServerBlocking : TRUE_FALSE) {
if (!isClientServiceTalk && isServerServiceTalk && isServerBlocking) {
// TODO there appears to be a potential bug in this combination. Separate bug filed.
continue;
}
if (isServerServiceTalk || !isServerBlocking) {
args.add(Arguments.of(isClientServiceTalk, isServerServiceTalk, isServerBlocking));
}
Expand Down
Loading