-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
api,core: Add ServerInterceptor2 interface #7746
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2020 The gRPC 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.grpc; | ||
|
||
import javax.annotation.concurrent.ThreadSafe; | ||
|
||
/** | ||
* Interface for intercepting server calls by modifying the {@link ServerMethodDefinition} after | ||
* method lookup and before call dispatch. This provides a superset of the functionality offered by | ||
* {@link ServerInterceptor}. | ||
*/ | ||
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/????") | ||
@ThreadSafe | ||
public interface ServerInterceptor2 { | ||
/** | ||
* Intercept and modify the {@link ServerMethodDefinition}. | ||
* | ||
* <p>The returned ServerMethodDefinition cannot be null and implementations must not throw. | ||
*/ | ||
<ReqT, RespT> ServerMethodDefinition<ReqT, RespT> interceptMethodDefinition( | ||
ServerMethodDefinition<ReqT, RespT> method); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,9 @@ | |
|
||
package io.grpc; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
/** | ||
* Definition of a method exposed by a {@link Server}. | ||
* | ||
|
@@ -24,11 +27,15 @@ | |
public final class ServerMethodDefinition<ReqT, RespT> { | ||
private final MethodDescriptor<ReqT, RespT> method; | ||
private final ServerCallHandler<ReqT, RespT> handler; | ||
private final List<ServerStreamTracer.Factory> tracerFactories; | ||
|
||
private ServerMethodDefinition(MethodDescriptor<ReqT, RespT> method, | ||
ServerCallHandler<ReqT, RespT> handler) { | ||
private ServerMethodDefinition( | ||
MethodDescriptor<ReqT, RespT> method, | ||
ServerCallHandler<ReqT, RespT> handler, | ||
List<ServerStreamTracer.Factory> tracerFactories) { | ||
this.method = method; | ||
this.handler = handler; | ||
this.tracerFactories = Collections.unmodifiableList(tracerFactories); | ||
} | ||
|
||
/** | ||
|
@@ -39,9 +46,9 @@ private ServerMethodDefinition(MethodDescriptor<ReqT, RespT> method, | |
* @return a new instance. | ||
*/ | ||
public static <ReqT, RespT> ServerMethodDefinition<ReqT, RespT> create( | ||
MethodDescriptor<ReqT, RespT> method, | ||
ServerCallHandler<ReqT, RespT> handler) { | ||
return new ServerMethodDefinition<>(method, handler); | ||
MethodDescriptor<ReqT, RespT> method, ServerCallHandler<ReqT, RespT> handler) { | ||
return new ServerMethodDefinition<>( | ||
method, handler, Collections.<ServerStreamTracer.Factory>emptyList()); | ||
} | ||
|
||
/** The {@code MethodDescriptor} for this method. */ | ||
|
@@ -62,6 +69,29 @@ public ServerCallHandler<ReqT, RespT> getServerCallHandler() { | |
*/ | ||
public ServerMethodDefinition<ReqT, RespT> withServerCallHandler( | ||
ServerCallHandler<ReqT, RespT> handler) { | ||
return new ServerMethodDefinition<>(method, handler); | ||
return new ServerMethodDefinition<>(method, handler, tracerFactories); | ||
} | ||
|
||
/** | ||
* Create a new method definition with a different list of tracer factories. | ||
* | ||
* @param tracerFactories to bind to a cloned instance of this | ||
* @return a cloned instance of this with an unmodifiable view of the new tracer factories | ||
* @since 1.36.0 | ||
*/ | ||
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/????") | ||
public ServerMethodDefinition<ReqT, RespT> withStreamTracerFactories( | ||
List<ServerStreamTracer.Factory> tracerFactories) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is an annoying API to use, as it requires getting the current list, copying it, and adding your own factory to the list, all before calling this API. How about a |
||
return new ServerMethodDefinition<>(method, handler, tracerFactories); | ||
} | ||
|
||
/** | ||
* Returns the stream tracer factories bound to this definition. | ||
* | ||
* @since 1.36.0 | ||
*/ | ||
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/????") | ||
public List<ServerStreamTracer.Factory> getStreamTracerFactories() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There may be value in this being a Collection. List is fine for now, but it should be discussed in the API review meeting. |
||
return tracerFactories; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not immutable. You need to make a copy.