-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Refactor RefreshAheadConnectionInfoCache. Part of #992.
The lazy refresh strategy only refreshes credentials and certificate information when the application attempts to establish a new database connection. On Cloud Run and other serverless runtimes, this is more reliable than the default background refresh strategy. Fixes #992. WIP Refactor BaseConnectionInfoCache chore: Refactor RefreshAheadConnectionInfoCache. Part of #992. The lazy refresh strategy only refreshes credentials and certificate information when the application attempts to establish a new database connection. On Cloud Run and other serverless runtimes, this is more reliable than the default background refresh strategy. Fixes #992. WIP Refactor BaseConnectionInfoCache
- Loading branch information
Showing
9 changed files
with
162 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
core/src/main/java/com/google/cloud/sql/core/RefreshAheadConnectionInfoCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* 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 com.google.cloud.sql.CredentialFactory; | ||
import com.google.common.util.concurrent.ListenableFuture; | ||
import com.google.common.util.concurrent.ListeningScheduledExecutorService; | ||
import java.security.KeyPair; | ||
|
||
/** | ||
* Implements the refresh ahead cache strategy, which will load the new ConnectionInfo using a | ||
* background thread before its certificate expires. | ||
*/ | ||
class RefreshAheadConnectionInfoCache extends BaseConnectionInfoCache { | ||
|
||
/** | ||
* Initializes a new Cloud SQL instance based on the given connection name. + * Initializes a new | ||
* Cloud SQL instance based on the given connection name using the background + * refresh | ||
* strategy. | ||
* | ||
* @param config instance connection name in the format "PROJECT_ID:REGION_ID:INSTANCE_ID" | ||
* @param connectionInfoRepository Service class for interacting with the Cloud SQL Admin API | ||
* @param executor executor used to schedule asynchronous tasks | ||
* @param keyPair public/private key pair used to authenticate connections | ||
*/ | ||
public static RefreshAheadConnectionInfoCache newInstance( | ||
ConnectionConfig config, | ||
ConnectionInfoRepository connectionInfoRepository, | ||
CredentialFactory tokenSourceFactory, | ||
ListeningScheduledExecutorService executor, | ||
ListenableFuture<KeyPair> keyPair, | ||
long minRefreshDelayMs) { | ||
|
||
AccessTokenSupplier accessTokenSupplier = | ||
BaseConnectionInfoCache.newAccessTokenSupplier(config, tokenSourceFactory); | ||
CloudSqlInstanceName instanceName = new CloudSqlInstanceName(config.getCloudSqlInstance()); | ||
|
||
RefreshAheadStrategy strategy = | ||
new RefreshAheadStrategy( | ||
config.getCloudSqlInstance(), | ||
executor, | ||
() -> | ||
connectionInfoRepository.getConnectionInfo( | ||
instanceName, accessTokenSupplier, config.getAuthType(), executor, keyPair), | ||
new AsyncRateLimiter(minRefreshDelayMs)); | ||
|
||
return new RefreshAheadConnectionInfoCache(config, accessTokenSupplier, strategy); | ||
} | ||
|
||
private RefreshAheadConnectionInfoCache( | ||
ConnectionConfig config, | ||
AccessTokenSupplier accessTokenSupplier, | ||
RefreshStrategy refreshStrategy) { | ||
super(config, accessTokenSupplier, refreshStrategy); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.