1818
1919import static com .google .common .base .Preconditions .checkNotNull ;
2020
21- import com .google .auto .value .AutoValue ;
2221import com .google .common .annotations .VisibleForTesting ;
2322import io .grpc .ManagedChannel ;
24- import io .grpc .xds .client .ConfiguredChannelCredentials .ChannelCredsConfig ;
2523import io .grpc .xds .internal .grpcservice .GrpcServiceConfig .GoogleGrpcConfig ;
26- import java .util .concurrent .atomic .AtomicReference ;
2724import java .util .function .Function ;
2825
2926/**
3027 * Concrete class managing the lifecycle of a single ManagedChannel for a GrpcServiceConfig.
3128 */
3229public class CachedChannelManager {
3330 private final Function <GrpcServiceConfig , ManagedChannel > channelCreator ;
34- private final Object lock = new Object ();
35-
36- private final AtomicReference <ChannelHolder > channelHolder = new AtomicReference <>();
37- private boolean closed ;
3831
3932 /**
4033 * Default constructor for production that creates a channel using the config's target and
@@ -57,82 +50,14 @@ public CachedChannelManager(Function<GrpcServiceConfig, ManagedChannel> channelC
5750 }
5851
5952 /**
60- * Returns a ManagedChannel for the given configuration. If the target or credentials config
61- * changes, the old channel is shut down and a new one is created.
53+ * Returns a ManagedChannel for the given configuration.
6254 */
6355 public ManagedChannel getChannel (GrpcServiceConfig config ) {
64- GoogleGrpcConfig googleGrpc = config .googleGrpc ();
65- ChannelKey newChannelKey = ChannelKey .of (
66- googleGrpc .target (),
67- googleGrpc .configuredChannelCredentials ().channelCredsConfig ());
68-
69- // 1. Fast path: Lock-free read
70- ChannelHolder holder = channelHolder .get ();
71- if (holder != null && holder .channelKey ().equals (newChannelKey )) {
72- return holder .channel ();
73- }
74-
75- ManagedChannel oldChannel = null ;
76- ManagedChannel newChannel ;
77-
78- // 2. Slow path: Update with locking
79- synchronized (lock ) {
80- if (closed ) {
81- throw new IllegalStateException ("CachedChannelManager is closed" );
82- }
83- holder = channelHolder .get (); // Double check
84- if (holder != null && holder .channelKey ().equals (newChannelKey )) {
85- return holder .channel ();
86- }
87-
88- // 3. Create inside lock to avoid creation storms
89- newChannel = channelCreator .apply (config );
90- ChannelHolder newHolder = ChannelHolder .create (newChannelKey , newChannel );
91-
92- if (holder != null ) {
93- oldChannel = holder .channel ();
94- }
95- channelHolder .set (newHolder );
96- }
97-
98- // 4. Shutdown outside lock
99- if (oldChannel != null ) {
100- oldChannel .shutdown ();
101- }
102-
103- return newChannel ;
56+ return channelCreator .apply (config );
10457 }
10558
10659 /** Removes underlying resources on shutdown. */
10760 public void close () {
108- synchronized (lock ) {
109- closed = true ;
110- ChannelHolder holder = channelHolder .getAndSet (null );
111- if (holder != null ) {
112- holder .channel ().shutdown ();
113- }
114- }
115- }
116-
117- @ AutoValue
118- abstract static class ChannelKey {
119- static ChannelKey of (String target , ChannelCredsConfig credentialsConfig ) {
120- return new AutoValue_CachedChannelManager_ChannelKey (target , credentialsConfig );
121- }
122-
123- abstract String target ();
124-
125- abstract ChannelCredsConfig channelCredsConfig ();
126- }
127-
128- @ AutoValue
129- abstract static class ChannelHolder {
130- static ChannelHolder create (ChannelKey channelKey , ManagedChannel channel ) {
131- return new AutoValue_CachedChannelManager_ChannelHolder (channelKey , channel );
132- }
133-
134- abstract ChannelKey channelKey ();
135-
136- abstract ManagedChannel channel ();
61+ // No-op as channel caching and lifecycle management is removed.
13762 }
13863}
0 commit comments