nastra commented on code in PR #14161:
URL: https://github.com/apache/iceberg/pull/14161#discussion_r2541474318


##########
aws/src/main/java/org/apache/iceberg/aws/HttpClientCache.java:
##########
@@ -0,0 +1,212 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.aws;
+
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Supplier;
+import 
org.apache.iceberg.relocated.com.google.common.annotations.VisibleForTesting;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import software.amazon.awssdk.http.ExecutableHttpRequest;
+import software.amazon.awssdk.http.HttpExecuteRequest;
+import software.amazon.awssdk.http.SdkHttpClient;
+
+/**
+ * A cache that manages the lifecycle of shared HTTP clients for AWS SDK v2 
using reference
+ * counting. Package-private - only accessed via {@link 
BaseHttpClientConfigurations}.
+ */
+final class HttpClientCache {
+  private static final Logger LOG = 
LoggerFactory.getLogger(HttpClientCache.class);
+
+  private final ConcurrentMap<String, ManagedHttpClient> clients;
+
+  private static volatile HttpClientCache instance;
+
+  static HttpClientCache instance() {
+    if (instance == null) {
+      synchronized (HttpClientCache.class) {
+        if (instance == null) {
+          instance = new HttpClientCache();
+        }
+      }
+    }
+    return instance;
+  }
+
+  private HttpClientCache() {
+    this.clients = Maps.newConcurrentMap();
+  }
+
+  /**
+   * Get or create a managed HTTP client for the given configuration. Each 
call increments the
+   * reference count for the client and returns a ref-counted wrapper.
+   *
+   * @param clientKey unique key identifying the client configuration
+   * @param clientFactory factory to create the HTTP client if not cached
+   * @return a ref-counted HTTP client wrapper
+   */
+  SdkHttpClient getOrCreateClient(String clientKey, Supplier<SdkHttpClient> 
clientFactory) {
+    ManagedHttpClient managedClient =
+        clients.computeIfAbsent(
+            clientKey,
+            k -> {
+              LOG.debug("Creating new managed HTTP client for key: {}", k);
+              SdkHttpClient httpClient = clientFactory.get();
+              return new ManagedHttpClient(httpClient, k);
+            });
+    // Return the cached ref-counted wrapper
+    return managedClient.acquire();
+  }
+
+  /**
+   * Release a reference to the HTTP client. When the reference count reaches 
zero, the client is
+   * closed and removed from the cache.
+   *
+   * @param clientKey the key identifying the client to release
+   */
+  void releaseClient(String clientKey) {
+    ManagedHttpClient managedClient = clients.get(clientKey);
+    if (managedClient != null) {
+      if (managedClient.release()) {
+        // Client was closed, remove from map
+        clients.remove(clientKey, managedClient);
+      }
+    }
+  }
+
+  @VisibleForTesting
+  ConcurrentMap<String, ManagedHttpClient> clients() {
+    return clients;

Review Comment:
   I think we should return an immutable copy here, so that callers don't 
modify this



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to