|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.sql.core; |
| 18 | + |
| 19 | +import com.google.cloud.sql.CredentialFactory; |
| 20 | +import java.security.KeyPair; |
| 21 | + |
| 22 | +/** |
| 23 | + * Implements the lazy refresh cache strategy, which loads the new certificate as needed during a |
| 24 | + * request for a new connection. |
| 25 | + */ |
| 26 | +class LazyRefreshConnectionInfoCache implements ConnectionInfoCache { |
| 27 | + private final ConnectionConfig config; |
| 28 | + private final CloudSqlInstanceName instanceName; |
| 29 | + |
| 30 | + private final LazyRefreshStrategy refreshStrategy; |
| 31 | + |
| 32 | + /** |
| 33 | + * Initializes a new Cloud SQL instance based on the given connection name using the lazy refresh |
| 34 | + * strategy. |
| 35 | + * |
| 36 | + * @param config instance connection name in the format "PROJECT_ID:REGION_ID:INSTANCE_ID" |
| 37 | + * @param connectionInfoRepository Service class for interacting with the Cloud SQL Admin API |
| 38 | + * @param keyPair public/private key pair used to authenticate connections |
| 39 | + */ |
| 40 | + public LazyRefreshConnectionInfoCache( |
| 41 | + ConnectionConfig config, |
| 42 | + ConnectionInfoRepository connectionInfoRepository, |
| 43 | + CredentialFactory tokenSourceFactory, |
| 44 | + KeyPair keyPair) { |
| 45 | + this.config = config; |
| 46 | + this.instanceName = new CloudSqlInstanceName(config.getCloudSqlInstance()); |
| 47 | + |
| 48 | + AccessTokenSupplier accessTokenSupplier = |
| 49 | + DefaultAccessTokenSupplier.newInstance(config.getAuthType(), tokenSourceFactory); |
| 50 | + CloudSqlInstanceName instanceName = new CloudSqlInstanceName(config.getCloudSqlInstance()); |
| 51 | + |
| 52 | + this.refreshStrategy = |
| 53 | + new LazyRefreshStrategy( |
| 54 | + config.getCloudSqlInstance(), |
| 55 | + () -> |
| 56 | + connectionInfoRepository.getConnectionInfoSync( |
| 57 | + instanceName, accessTokenSupplier, config.getAuthType(), keyPair)); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public ConnectionMetadata getConnectionMetadata(long timeoutMs) { |
| 62 | + return refreshStrategy.getConnectionInfo(timeoutMs).toConnectionMetadata(config, instanceName); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void forceRefresh() { |
| 67 | + refreshStrategy.forceRefresh(); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void refreshIfExpired() { |
| 72 | + refreshStrategy.refreshIfExpired(); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void close() { |
| 77 | + refreshStrategy.close(); |
| 78 | + } |
| 79 | +} |
0 commit comments