4545import io .grpc .xds .MetadataRegistry .MetadataValueParser ;
4646import io .grpc .xds .XdsConfig .XdsClusterConfig ;
4747import io .grpc .xds .client .XdsResourceType .ResourceInvalidException ;
48+ import java .util .Iterator ;
4849import java .util .LinkedHashMap ;
4950import java .util .Map ;
51+ import java .util .Map .Entry ;
5052import java .util .concurrent .ScheduledExecutorService ;
5153import java .util .function .Function ;
5254import javax .annotation .Nullable ;
@@ -102,11 +104,14 @@ public ConfigOrError<GcpAuthenticationConfig> parseFilterConfig(Message rawProto
102104 // Validate cache_config
103105 if (gcpAuthnProto .hasCacheConfig ()) {
104106 TokenCacheConfig cacheConfig = gcpAuthnProto .getCacheConfig ();
105- cacheSize = cacheConfig .getCacheSize ().getValue ();
106- if (cacheSize == 0 ) {
107- return ConfigOrError .fromError (
108- "cache_config.cache_size must be greater than zero" );
107+ if (cacheConfig .hasCacheSize ()) {
108+ cacheSize = cacheConfig .getCacheSize ().getValue ();
109+ if (cacheSize == 0 ) {
110+ return ConfigOrError .fromError (
111+ "cache_config.cache_size must be greater than zero" );
112+ }
109113 }
114+
110115 // LruCache's size is an int and briefly exceeds its maximum size before evicting entries
111116 cacheSize = UnsignedLongs .min (cacheSize , Integer .MAX_VALUE - 1 );
112117 }
@@ -128,6 +133,7 @@ public ClientInterceptor buildClientInterceptor(FilterConfig config,
128133 @ Nullable FilterConfig overrideConfig , ScheduledExecutorService scheduler ) {
129134
130135 ComputeEngineCredentials credentials = ComputeEngineCredentials .create ();
136+ callCredentialsCache .resizeCache (((GcpAuthenticationConfig ) config ).getCacheSize ());
131137 return new ClientInterceptor () {
132138 @ Override
133139 public <ReqT , RespT > ClientCall <ReqT , RespT > interceptCall (
@@ -254,8 +260,10 @@ public void sendMessage(ReqT message) {}
254260 private static final class LruCache <K , V > {
255261
256262 private final Map <K , V > cache ;
263+ private int maxSize ;
257264
258265 LruCache (int maxSize ) {
266+ this .maxSize = maxSize ;
259267 this .cache = new LinkedHashMap <K , V >(
260268 maxSize ,
261269 0.75f ,
@@ -270,6 +278,19 @@ protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
270278 V getOrInsert (K key , Function <K , V > create ) {
271279 return cache .computeIfAbsent (key , create );
272280 }
281+
282+ private void resizeCache (int newSize ) {
283+ while (cache .size () > newSize ) {
284+ Iterator <Entry <K , V >> iterator = cache .entrySet ().iterator ();
285+ if (iterator .hasNext ()) {
286+ iterator .next ();
287+ iterator .remove ();
288+ } else {
289+ break ;
290+ }
291+ }
292+ maxSize = newSize ;
293+ }
273294 }
274295
275296 static class AudienceMetadataParser implements MetadataValueParser {
0 commit comments