Skip to content

Commit

Permalink
Move the Context key definition and accessor to io.grpc.
Browse files Browse the repository at this point in the history
  • Loading branch information
voidzcy committed Apr 30, 2020
1 parent e4a6b9b commit 9a979c5
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
Expand Up @@ -14,23 +14,18 @@
* limitations under the License.
*/

package io.grpc.internal;

import io.grpc.Context;
import io.grpc.Internal;
import io.grpc.Server;
package io.grpc;

/**
* Internal accessor for getting the {@link Server} instance inside server RPC {@link Context}.
* This is intended for usage internal to the gRPC team, as it's unclear to us what users would
* need. If you *really* think you need to use this, please file an issue for us to discuss a
* public API.
* This is intended for usage internal to the gRPC team. If you think you need to use
* this, contact the gRPC team first.
*/
@Internal
public class InternalServerAccessor {
public static final Context.Key<Server> SERVER_KEY = ServerImpl.SERVER_CONTEXT_KEY;
public class InternalServer {
public static final Context.Key<Server> SERVER_CONTEXT_KEY = Server.SERVER_CONTEXT_KEY;

// Prevent instantiation.
private InternalServerAccessor() {
private InternalServer() {
}
}
8 changes: 8 additions & 0 deletions api/src/main/java/io/grpc/Server.java
Expand Up @@ -30,6 +30,14 @@
@ThreadSafe
public abstract class Server {

/**
* Key for accessing the {@link Server} instance inside server RPC {@link Context}. It's
* unclear to us what users would need. If you think you need to use this, please file an
* issue for us to discuss a public API.
*/
protected static final Context.Key<Server> SERVER_CONTEXT_KEY =

This comment has been minimized.

Copy link
@voidzcy

voidzcy Apr 30, 2020

Author Contributor

Okay, I was trying to have this usable in ServerImpl.

Made this package-private and changed that to use the accessor.

Context.key("io.grpc.Server");

/**
* Bind and start the server. After this call returns, clients may begin connecting to the
* listening socket(s).
Expand Down
2 changes: 0 additions & 2 deletions core/src/main/java/io/grpc/internal/ServerImpl.java
Expand Up @@ -45,7 +45,6 @@
import io.grpc.InternalLogId;
import io.grpc.InternalServerInterceptors;
import io.grpc.Metadata;
import io.grpc.Server;
import io.grpc.ServerCall;
import io.grpc.ServerCallHandler;
import io.grpc.ServerInterceptor;
Expand Down Expand Up @@ -92,7 +91,6 @@ public final class ServerImpl extends io.grpc.Server implements InternalInstrume
private static final Logger log = Logger.getLogger(ServerImpl.class.getName());
private static final ServerStreamListener NOOP_LISTENER = new NoopListener();

static final Context.Key<Server> SERVER_CONTEXT_KEY = Context.key("io.grpc.ServerImpl");
private final InternalLogId logId;
private final ObjectPool<? extends Executor> executorPool;
/** Executor for application processing. Safe to read after {@link #start()}. */
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/java/io/grpc/internal/ServerImplTest.java
Expand Up @@ -561,7 +561,7 @@ public ServerCall.Listener<String> startCall(
Context callContext = callContextReference.get();
assertNotNull(callContext);
assertEquals("context added by tracer", SERVER_TRACER_ADDED_KEY.get(callContext));
assertEquals(server, ServerImpl.SERVER_CONTEXT_KEY.get(callContext));
assertEquals(server, io.grpc.InternalServer.SERVER_CONTEXT_KEY.get(callContext));

streamListener.messagesAvailable(new SingleMessageProducer(STRING_MARSHALLER.stream(request)));
assertEquals(1, executor.runDueTasks());
Expand Down
Expand Up @@ -26,10 +26,10 @@
import com.google.protobuf.Descriptors.ServiceDescriptor;
import io.grpc.BindableService;
import io.grpc.ExperimentalApi;
import io.grpc.InternalServer;
import io.grpc.Server;
import io.grpc.ServerServiceDefinition;
import io.grpc.Status;
import io.grpc.internal.InternalServerAccessor;
import io.grpc.protobuf.ProtoFileDescriptorSupplier;
import io.grpc.reflection.v1alpha.ErrorResponse;
import io.grpc.reflection.v1alpha.ExtensionNumberResponse;
Expand Down Expand Up @@ -86,7 +86,7 @@ public static BindableService newInstance() {
*/
private ServerReflectionIndex getRefreshedIndex() {
synchronized (lock) {
Server server = InternalServerAccessor.SERVER_KEY.get();
Server server = InternalServer.SERVER_CONTEXT_KEY.get();
ServerReflectionIndex index = serverReflectionIndexes.get(server);
if (index == null) {
index =
Expand Down

0 comments on commit 9a979c5

Please sign in to comment.