Skip to content

Commit

Permalink
Remove global SHARED_RESOURCE_CHANNELS.
Browse files Browse the repository at this point in the history
  • Loading branch information
rmehta19 committed Sep 19, 2024
1 parent 9efaa6a commit df413b1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static java.util.concurrent.TimeUnit.SECONDS;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Maps;
import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ChannelCredentials;
Expand All @@ -35,14 +34,13 @@
import io.netty.util.concurrent.DefaultThreadFactory;
import java.time.Duration;
import java.util.Optional;
import java.util.concurrent.ConcurrentMap;
import javax.annotation.concurrent.ThreadSafe;

/**
* Provides APIs for managing gRPC channels to S2A servers. Each channel is local and plaintext. If
* credentials are provided, they are used to secure the channel.
* Provides APIs for managing gRPC channels to an S2A server. Each channel is local and plaintext.
* If credentials are provided, they are used to secure the channel.
*
* <p>This is done as follows: for each S2A server, provides an implementation of gRPC's {@link
* <p>This is done as follows: for an S2A server, provides an implementation of gRPC's {@link
* SharedResourceHolder.Resource} interface called a {@code Resource<Channel>}. A {@code
* Resource<Channel>} is a factory for creating gRPC channels to the S2A server at a given address,
* and a channel must be returned to the {@code Resource<Channel>} when it is no longer needed.
Expand All @@ -59,8 +57,6 @@
*/
@ThreadSafe
public final class S2AHandshakerServiceChannel {
private static final ConcurrentMap<String, Resource<Channel>> SHARED_RESOURCE_CHANNELS =
Maps.newConcurrentMap();
private static final Duration DELEGATE_TERMINATION_TIMEOUT = Duration.ofSeconds(2);
private static final Duration CHANNEL_SHUTDOWN_TIMEOUT = Duration.ofSeconds(10);

Expand All @@ -76,8 +72,7 @@ public final class S2AHandshakerServiceChannel {
public static Resource<Channel> getChannelResource(
String s2aAddress, Optional<ChannelCredentials> s2aChannelCredentials) {
checkNotNull(s2aAddress);
return SHARED_RESOURCE_CHANNELS.computeIfAbsent(
s2aAddress, channelResource -> new ChannelResource(s2aAddress, s2aChannelCredentials));
return new ChannelResource(s2aAddress, s2aChannelCredentials);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ public void getChannelResource_mtlsSuccess() throws Exception {

/**
* Creates two {@code Resoure<Channel>}s for the same target address and verifies that they are
* equal.
* distinct.
*/
@Test
public void getChannelResource_twoEqualChannels() {
public void getChannelResource_twoUnEqualChannels() {
Resource<Channel> resource =
S2AHandshakerServiceChannel.getChannelResource(
"localhost:" + plaintextServer.getPort(),
Expand All @@ -109,19 +109,19 @@ public void getChannelResource_twoEqualChannels() {
S2AHandshakerServiceChannel.getChannelResource(
"localhost:" + plaintextServer.getPort(),
/* s2aChannelCredentials= */ Optional.empty());
assertThat(resource).isEqualTo(resourceTwo);
assertThat(resource).isNotEqualTo(resourceTwo);
}

/** Same as getChannelResource_twoEqualChannels, but use mTLS. */
@Test
public void getChannelResource_mtlsTwoEqualChannels() throws Exception {
public void getChannelResource_mtlsTwoUnEqualChannels() throws Exception {
Resource<Channel> resource =
S2AHandshakerServiceChannel.getChannelResource(
"localhost:" + mtlsServer.getPort(), getTlsChannelCredentials());
Resource<Channel> resourceTwo =
S2AHandshakerServiceChannel.getChannelResource(
"localhost:" + mtlsServer.getPort(), getTlsChannelCredentials());
assertThat(resource).isEqualTo(resourceTwo);
assertThat(resource).isNotEqualTo(resourceTwo);
}

/**
Expand Down

0 comments on commit df413b1

Please sign in to comment.