Skip to content

Commit b021a21

Browse files
committed
Push down the creation of Optional<S2AIdentity> until S2AProtocolNegotiator.
1 parent 0e059e4 commit b021a21

3 files changed

Lines changed: 14 additions & 17 deletions

File tree

s2a/src/main/java/io/grpc/s2a/S2AChannelCredentials.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,7 @@ public ChannelCredentials build() {
122122
}
123123

124124
InternalProtocolNegotiator.ClientFactory buildProtocolNegotiatorFactory() {
125-
if (localIdentity == null) {
126-
return S2AProtocolNegotiatorFactory.createClientFactory(Optional.empty(), s2aChannelPool);
127-
} else {
128-
return S2AProtocolNegotiatorFactory.createClientFactory(
129-
Optional.of(localIdentity), s2aChannelPool);
130-
}
125+
return S2AProtocolNegotiatorFactory.createClientFactory(localIdentity, s2aChannelPool);
131126
}
132127
}
133128

s2a/src/main/java/io/grpc/s2a/handshaker/S2AProtocolNegotiatorFactory.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,19 @@ public final class S2AProtocolNegotiatorFactory {
6666
* @return a factory for creating a client-side protocol negotiator.
6767
*/
6868
public static InternalProtocolNegotiator.ClientFactory createClientFactory(
69-
Optional<S2AIdentity> localIdentity, ObjectPool<Channel> s2aChannelPool) {
69+
@Nullable S2AIdentity localIdentity, ObjectPool<Channel> s2aChannelPool) {
7070
checkNotNull(s2aChannelPool, "S2A channel pool should not be null.");
71-
checkNotNull(localIdentity, "Local identity should not be null on the client side.");
7271
S2AChannelPool channelPool = S2AGrpcChannelPool.create(s2aChannelPool);
7372
return new S2AClientProtocolNegotiatorFactory(localIdentity, channelPool);
7473
}
7574

7675
static final class S2AClientProtocolNegotiatorFactory
7776
implements InternalProtocolNegotiator.ClientFactory {
78-
private final Optional<S2AIdentity> localIdentity;
77+
private final @Nullable S2AIdentity localIdentity;
7978
private final S2AChannelPool channelPool;
8079

8180
S2AClientProtocolNegotiatorFactory(
82-
Optional<S2AIdentity> localIdentity, S2AChannelPool channelPool) {
81+
@Nullable S2AIdentity localIdentity, S2AChannelPool channelPool) {
8382
this.localIdentity = localIdentity;
8483
this.channelPool = channelPool;
8584
}
@@ -105,10 +104,13 @@ static final class S2AProtocolNegotiator implements ProtocolNegotiator {
105104
MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(1));
106105

107106
static S2AProtocolNegotiator createForClient(
108-
S2AChannelPool channelPool, Optional<S2AIdentity> localIdentity) {
107+
S2AChannelPool channelPool, @Nullable S2AIdentity localIdentity) {
109108
checkNotNull(channelPool, "Channel pool should not be null.");
110-
checkNotNull(localIdentity, "Local identity should not be null on the client side.");
111-
return new S2AProtocolNegotiator(channelPool, localIdentity);
109+
if (localIdentity == null) {
110+
return new S2AProtocolNegotiator(channelPool, Optional.empty());
111+
} else {
112+
return new S2AProtocolNegotiator(channelPool, Optional.of(localIdentity));
113+
}
112114
}
113115

114116
@VisibleForTesting

s2a/src/test/java/io/grpc/s2a/handshaker/S2AProtocolNegotiatorFactoryTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public void createProtocolNegotiator_nullArgument() throws Exception {
128128
@Test
129129
public void createProtocolNegotiatorFactory_getsDefaultPort_succeeds() throws Exception {
130130
InternalProtocolNegotiator.ClientFactory clientFactory =
131-
S2AProtocolNegotiatorFactory.createClientFactory(Optional.of(LOCAL_IDENTITY), channelPool);
131+
S2AProtocolNegotiatorFactory.createClientFactory(LOCAL_IDENTITY, channelPool);
132132

133133
assertThat(clientFactory.getDefaultPort()).isEqualTo(S2AProtocolNegotiatorFactory.DEFAULT_PORT);
134134
}
@@ -152,7 +152,7 @@ public void s2aProtocolNegotiator_getHostNameOnValidAuthority_returnsValidHostna
152152
public void createProtocolNegotiatorFactory_buildsAnS2AProtocolNegotiatorOnClientSide_succeeds()
153153
throws Exception {
154154
InternalProtocolNegotiator.ClientFactory clientFactory =
155-
S2AProtocolNegotiatorFactory.createClientFactory(Optional.of(LOCAL_IDENTITY), channelPool);
155+
S2AProtocolNegotiatorFactory.createClientFactory(LOCAL_IDENTITY, channelPool);
156156

157157
ProtocolNegotiator clientNegotiator = clientFactory.newNegotiator();
158158

@@ -164,7 +164,7 @@ public void createProtocolNegotiatorFactory_buildsAnS2AProtocolNegotiatorOnClien
164164
public void closeProtocolNegotiator_verifyProtocolNegotiatorIsClosedOnClientSide()
165165
throws Exception {
166166
InternalProtocolNegotiator.ClientFactory clientFactory =
167-
S2AProtocolNegotiatorFactory.createClientFactory(Optional.of(LOCAL_IDENTITY), channelPool);
167+
S2AProtocolNegotiatorFactory.createClientFactory(LOCAL_IDENTITY, channelPool);
168168
ProtocolNegotiator clientNegotiator = clientFactory.newNegotiator();
169169

170170
clientNegotiator.close();
@@ -182,7 +182,7 @@ public void createChannelHandler_addHandlerToMockContext() throws Exception {
182182
FakeS2AChannelPool fakeChannelPool = new FakeS2AChannelPool(channel);
183183
ProtocolNegotiator clientNegotiator =
184184
S2AProtocolNegotiatorFactory.S2AProtocolNegotiator.createForClient(
185-
fakeChannelPool, Optional.of(LOCAL_IDENTITY));
185+
fakeChannelPool, LOCAL_IDENTITY);
186186

187187
ChannelHandler channelHandler = clientNegotiator.newHandler(fakeConnectionHandler);
188188

0 commit comments

Comments
 (0)