Skip to content

Commit d49ab2f

Browse files
vlcekmilanMilan Vlcek
authored andcommitted
Expose InternalServer as a transport attribute in ServerImpl
1 parent 31fdb6c commit d49ab2f

2 files changed

Lines changed: 21 additions & 17 deletions

File tree

core/src/main/java/io/grpc/internal/ServerImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@
9494
public final class ServerImpl extends io.grpc.Server implements InternalInstrumented<ServerStats> {
9595
private static final Logger log = Logger.getLogger(ServerImpl.class.getName());
9696
private static final ServerStreamListener NOOP_LISTENER = new NoopListener();
97+
static final Attributes.Key<InternalServer> TRANSPORT_SERVER_ATTR =
98+
Attributes.Key.create("io.grpc.Grpc.TRANSPORT_ATTR_SERVER_TRANSPORT");
9799

98100
private final InternalLogId logId;
99101
private final ObjectPool<? extends Executor> executorPool;
@@ -438,6 +440,10 @@ public Attributes transportReady(Attributes attributes) {
438440
handshakeTimeoutFuture.cancel(false);
439441
handshakeTimeoutFuture = null;
440442

443+
final Attributes.Builder builder = attributes.toBuilder();
444+
builder.set(TRANSPORT_SERVER_ATTR, transportServer);
445+
attributes = builder.build();
446+
441447
for (ServerTransportFilter filter : transportFilters) {
442448
attributes = Preconditions.checkNotNull(filter.transportReady(attributes),
443449
"Filter %s returned null", filter);

core/src/test/java/io/grpc/internal/ServerImplTest.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,8 @@ public void transportFilters() throws Exception {
723723
final Attributes.Key<String> key1 = Attributes.Key.create("test-key1");
724724
final Attributes.Key<String> key2 = Attributes.Key.create("test-key2");
725725
final Attributes.Key<String> key3 = Attributes.Key.create("test-key3");
726+
final Attributes.Key<InternalServer> transportServerKey =
727+
ServerImpl.TRANSPORT_SERVER_ATTR;
726728
final AtomicReference<Attributes> filter1TerminationCallbackArgument =
727729
new AtomicReference<>();
728730
final AtomicReference<Attributes> filter2TerminationCallbackArgument =
@@ -732,9 +734,8 @@ public void transportFilters() throws Exception {
732734
builder.addTransportFilter(new ServerTransportFilter() {
733735
@Override
734736
public Attributes transportReady(Attributes attrs) {
735-
assertEquals(Attributes.newBuilder()
736-
.set(Grpc.TRANSPORT_ATTR_REMOTE_ADDR, remoteAddr)
737-
.build(), attrs);
737+
assertEquals(remoteAddr, attrs.get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR));
738+
assertNotNull(attrs.get(transportServerKey));
738739
readyCallbackCalled.incrementAndGet();
739740
return attrs.toBuilder()
740741
.set(key1, "yalayala")
@@ -751,11 +752,10 @@ public void transportTerminated(Attributes attrs) {
751752
builder.addTransportFilter(new ServerTransportFilter() {
752753
@Override
753754
public Attributes transportReady(Attributes attrs) {
754-
assertEquals(Attributes.newBuilder()
755-
.set(Grpc.TRANSPORT_ATTR_REMOTE_ADDR, remoteAddr)
756-
.set(key1, "yalayala")
757-
.set(key2, "blabla")
758-
.build(), attrs);
755+
assertEquals(remoteAddr, attrs.get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR));
756+
assertNotNull(attrs.get(transportServerKey));
757+
assertEquals("yalayala", attrs.get(key1));
758+
assertEquals("blabla", attrs.get(key2));
759759
readyCallbackCalled.incrementAndGet();
760760
return attrs.toBuilder()
761761
.set(key1, "ouch")
@@ -769,26 +769,24 @@ public void transportTerminated(Attributes attrs) {
769769
filter2TerminationCallbackArgument.set(attrs);
770770
}
771771
});
772-
Attributes expectedTransportAttrs = Attributes.newBuilder()
773-
.set(key1, "ouch")
774-
.set(key2, "blabla")
775-
.set(key3, "puff")
776-
.set(Grpc.TRANSPORT_ATTR_REMOTE_ADDR, remoteAddr)
777-
.build();
778772

779773
createAndStartServer();
780774
ServerTransportListener transportListener
781775
= transportServer.registerNewServerTransport(new SimpleServerTransport());
782776
Attributes transportAttrs = transportListener.transportReady(Attributes.newBuilder()
783777
.set(Grpc.TRANSPORT_ATTR_REMOTE_ADDR, remoteAddr).build());
784778

785-
assertEquals(expectedTransportAttrs, transportAttrs);
779+
assertEquals(remoteAddr, transportAttrs.get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR));
780+
assertNotNull(transportAttrs.get(transportServerKey));
781+
assertEquals("ouch", transportAttrs.get(key1));
782+
assertEquals("blabla", transportAttrs.get(key2));
783+
assertEquals("puff", transportAttrs.get(key3));
786784

787785
server.shutdown();
788786
server.awaitTermination();
789787

790-
assertEquals(expectedTransportAttrs, filter1TerminationCallbackArgument.get());
791-
assertEquals(expectedTransportAttrs, filter2TerminationCallbackArgument.get());
788+
assertEquals(transportAttrs, filter1TerminationCallbackArgument.get());
789+
assertEquals(transportAttrs, filter2TerminationCallbackArgument.get());
792790
assertEquals(2, readyCallbackCalled.get());
793791
assertEquals(2, terminationCallbackCalled.get());
794792
}

0 commit comments

Comments
 (0)