bbotella commented on code in PR #189:
URL: https://github.com/apache/cassandra-sidecar/pull/189#discussion_r1970834279


##########
server/src/main/java/org/apache/cassandra/sidecar/utils/SidecarClientProvider.java:
##########
@@ -0,0 +1,166 @@
+/*
+ * 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.cassandra.sidecar.utils;
+
+import java.util.ArrayList;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.google.inject.Singleton;
+import io.vertx.core.Promise;
+import io.vertx.core.Vertx;
+import io.vertx.core.http.HttpClient;
+import io.vertx.core.net.JksOptions;
+import io.vertx.core.net.OpenSSLEngineOptions;
+import io.vertx.ext.web.client.WebClient;
+import io.vertx.ext.web.client.WebClientOptions;
+import org.apache.cassandra.sidecar.client.HttpClientConfig;
+import org.apache.cassandra.sidecar.client.SidecarClient;
+import org.apache.cassandra.sidecar.client.SidecarClientConfig;
+import org.apache.cassandra.sidecar.client.SidecarClientConfigImpl;
+import org.apache.cassandra.sidecar.client.SidecarClientVertxRequestExecutor;
+import org.apache.cassandra.sidecar.client.SimpleSidecarInstancesProvider;
+import org.apache.cassandra.sidecar.client.VertxHttpClient;
+import org.apache.cassandra.sidecar.client.retry.ExponentialBackoffRetryPolicy;
+import org.apache.cassandra.sidecar.client.retry.RetryPolicy;
+import org.apache.cassandra.sidecar.common.client.SidecarInstance;
+import org.apache.cassandra.sidecar.common.client.SidecarInstanceImpl;
+import org.apache.cassandra.sidecar.common.server.utils.SidecarVersionProvider;
+import org.apache.cassandra.sidecar.config.SidecarClientConfiguration;
+import org.apache.cassandra.sidecar.config.SidecarConfiguration;
+
+/**
+ * Provider class for retrieving the singleton {@link SidecarClient} instance
+ */
+@Singleton
+public class SidecarClientProvider implements Provider<SidecarClient>
+{
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(SidecarClientProvider.class);
+    private final Vertx vertx;
+    private final SidecarClientConfiguration clientConfig;
+    private final SidecarVersionProvider sidecarVersionProvider;
+    private final SidecarClient client;
+
+    @Inject
+    public SidecarClientProvider(Vertx vertx,
+                                 SidecarConfiguration sidecarConfiguration,
+                                 SidecarVersionProvider sidecarVersionProvider)
+    {
+        this.vertx = vertx;
+        this.clientConfig = sidecarConfiguration.sidecarClientConfiguration();
+        this.sidecarVersionProvider = sidecarVersionProvider;
+        this.client = initializeSidecarClient();
+    }
+
+    @Override
+    public SidecarClient get()
+    {
+        return client;
+    }
+
+    public void close(Promise<Void> completion)
+    {
+        LOGGER.info("Closing Sidecar Client...");
+        try
+        {
+            client.close();
+            Thread.sleep(100);
+            completion.complete();
+        }
+        catch (Throwable throwable)
+        {
+            completion.fail(throwable);
+        }
+    }
+
+    private SidecarClient initializeSidecarClient()
+    {
+        WebClientOptions webClientOptions = webClientOptions();
+        HttpClient httpClient = vertx.createHttpClient(webClientOptions);
+        WebClient webClient = WebClient.wrap(httpClient, webClientOptions);
+
+        HttpClientConfig httpClientConfig = new HttpClientConfig.Builder<>()
+                                            .ssl(webClientOptions().isSsl())
+                                            
.timeoutMillis(clientConfig.requestTimeout().toMillis())
+                                            
.idleTimeoutMillis(clientConfig.requestIdleTimeout().toIntMillis())
+                                            .userAgent("cassandra-sidecar/" + 
sidecarVersionProvider.sidecarVersion())
+                                            .build();
+
+        VertxHttpClient vertxHttpClient = new VertxHttpClient(vertx, 
webClient, httpClientConfig);
+        RetryPolicy defaultRetryPolicy = new 
ExponentialBackoffRetryPolicy(clientConfig.maxRetries(),
+                                                                           
clientConfig.retryDelay().toMillis(),
+                                                                           
clientConfig.retryDelay().toMillis());
+        SidecarClientVertxRequestExecutor requestExecutor = new 
SidecarClientVertxRequestExecutor(vertxHttpClient);
+        SidecarInstance instance = new 
SidecarInstanceImpl(webClientOptions.getDefaultHost(), 
webClientOptions.getDefaultPort());
+        ArrayList<SidecarInstance> instances = new ArrayList<>();
+        instances.add(instance);

Review Comment:
   Synced offline. Leaving it as it is.



-- 
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: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org

Reply via email to