Skip to content

Commit

Permalink
chore: Introduce ConnectionInfoCache interface (part of #992).
Browse files Browse the repository at this point in the history
  • Loading branch information
hessjcg committed May 28, 2024
1 parent 58c7676 commit 93a57e8
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* SQL Admin API. The operations to retrieve information with the API are largely done
* asynchronously, and this class should be considered threadsafe.
*/
class BaseConnectionInfoCache {
class BaseConnectionInfoCache implements ConnectionInfoCache {
private final AccessTokenSupplier accessTokenSupplier;
private final CloudSqlInstanceName instanceName;
private final RefreshStrategy refreshStrategy;
Expand Down Expand Up @@ -96,7 +96,8 @@ private ConnectionInfo getConnectionInfo(long timeoutMs) {
* Returns an unconnected {@link SSLSocket} using the SSLContext associated with the instance. May
* block until required instance data is available.
*/
SSLSocket createSslSocket(long timeoutMs) throws IOException {
@Override
public SSLSocket createSslSocket(long timeoutMs) throws IOException {
return (SSLSocket)
getConnectionInfo(timeoutMs).getSslContext().getSocketFactory().createSocket();
}
Expand All @@ -108,7 +109,8 @@ SSLSocket createSslSocket(long timeoutMs) throws IOException {
* @throws IllegalArgumentException If the instance has no IP addresses matching the provided
* preferences.
*/
ConnectionMetadata getConnectionMetadata(long timeoutMs) {
@Override
public ConnectionMetadata getConnectionMetadata(long timeoutMs) {
ConnectionInfo info = getConnectionInfo(timeoutMs);
String preferredIp = null;

Expand All @@ -132,23 +134,28 @@ ConnectionMetadata getConnectionMetadata(long timeoutMs) {
info.getSslData().getTrustManagerFactory());
}

void forceRefresh() {
@Override
public void forceRefresh() {
this.refreshStrategy.forceRefresh();
}

void refreshIfExpired() {
@Override
public void refreshIfExpired() {
this.refreshStrategy.refreshIfExpired();
}

RefreshStrategy getRefresher() {
@Override
public RefreshStrategy getRefresher() {
return this.refreshStrategy;
}

@Override
public CloudSqlInstanceName getInstanceName() {
return instanceName;
}

void close() {
@Override
public void close() {
refreshStrategy.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2024 Google LLC
*
* Licensed 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 com.google.cloud.sql.core;

import java.io.IOException;
import javax.net.ssl.SSLSocket;

/** ConnectionInfoCache is the contract for a caching strategy for ConnectionInfo. */
public interface ConnectionInfoCache {

/**
* Returns an unconnected {@link SSLSocket} using the SSLContext associated with the instance. May
* block until required instance data is available.
*/
SSLSocket createSslSocket(long timeoutMs) throws IOException;

/**
* Returns metadata needed to create a connection to the instance.
*
* @return returns ConnectionMetadata containing the preferred IP and SSL connection data.
* @throws IllegalArgumentException If the instance has no IP addresses matching the provided
* preferences.
*/
ConnectionMetadata getConnectionMetadata(long timeoutMs);

void forceRefresh();

void refreshIfExpired();

RefreshStrategy getRefresher();

CloudSqlInstanceName getInstanceName();

void close();
}
10 changes: 5 additions & 5 deletions core/src/main/java/com/google/cloud/sql/core/Connector.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Connector {
private final ListenableFuture<KeyPair> localKeyPair;
private final long minRefreshDelayMs;

private final ConcurrentHashMap<ConnectionConfig, BaseConnectionInfoCache> instances =
private final ConcurrentHashMap<ConnectionConfig, ConnectionInfoCache> instances =
new ConcurrentHashMap<>();
private final int serverProxyPort;
private final ConnectorConfig config;
Expand Down Expand Up @@ -107,7 +107,7 @@ Socket connect(ConnectionConfig config, long timeoutMs) throws IOException {
return UnixSocketChannel.open(socketAddress).socket();
}

BaseConnectionInfoCache instance = getConnection(config);
ConnectionInfoCache instance = getConnection(config);
try {

String instanceIp = instance.getConnectionMetadata(timeoutMs).getPreferredIpAddress();
Expand Down Expand Up @@ -137,8 +137,8 @@ Socket connect(ConnectionConfig config, long timeoutMs) throws IOException {
}
}

BaseConnectionInfoCache getConnection(ConnectionConfig config) {
BaseConnectionInfoCache instance =
ConnectionInfoCache getConnection(ConnectionConfig config) {
ConnectionInfoCache instance =
instances.computeIfAbsent(config, k -> createConnectionInfo(config));

// If the client certificate has expired (as when the computer goes to
Expand All @@ -151,7 +151,7 @@ BaseConnectionInfoCache getConnection(ConnectionConfig config) {
return instance;
}

private BaseConnectionInfoCache createConnectionInfo(ConnectionConfig config) {
private ConnectionInfoCache createConnectionInfo(ConnectionConfig config) {
logger.debug(
String.format("[%s] Connection info added to cache.", config.getCloudSqlInstance()));
return new BaseConnectionInfoCache(
Expand Down

0 comments on commit 93a57e8

Please sign in to comment.