-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds the SocketFactory wrapper with a ConnectorRegistry singleton. The ConnectorRegistry is a singleton to make it possible for external callers to close the associated connector. In addition, the singleton assures that multiple socket factory initializations will all re-use the same connector internally.
- Loading branch information
Showing
12 changed files
with
441 additions
and
96 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export ALLOYDB_DB="some-db" | ||
export ALLOYDB_USER="some-user" | ||
export ALLOYDB_PASS="some-password" | ||
export ALLOYDB_INSTANCE_URI="projects/<PROJECT>/locations/<REGION>/clusters/<CLUSTER>/instances/<INSTANCE>" | ||
export ALLOYDB_INSTANCE_NAME="projects/<PROJECT>/locations/<REGION>/clusters/<CLUSTER>/instances/<INSTANCE>" |
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
62 changes: 62 additions & 0 deletions
62
alloydb-jdbc-connector/src/main/java/com/google/cloud/alloydb/ConnectorRegistry.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,62 @@ | ||
/* | ||
* Copyright 2023 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 | ||
* | ||
* https://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.alloydb; | ||
|
||
import com.google.cloud.alloydb.v1beta.AlloyDBAdminClient; | ||
import java.io.Closeable; | ||
import java.io.IOException; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
|
||
/** | ||
* ConnectorRegistry is a singleton that creates a single Executor, KeyPair, and AlloyDB Admin | ||
* Client for the lifetime of the SocketFactory. When callers are finished with the Connector, they | ||
* should use the ConnectorRegistry to shut down all the associated resources. | ||
*/ | ||
public enum ConnectorRegistry implements Closeable { | ||
INSTANCE; | ||
|
||
private final ScheduledExecutorService executor; | ||
private final AlloyDBAdminClient alloyDBAdminClient; | ||
private final Connector connector; | ||
|
||
ConnectorRegistry() { | ||
this.executor = Executors.newScheduledThreadPool(2); | ||
try { | ||
alloyDBAdminClient = AlloyDBAdminClient.create(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
this.connector = | ||
new Connector( | ||
executor, | ||
new DefaultConnectionInfoRepository(executor, alloyDBAdminClient), | ||
RsaKeyPairGenerator.generateKeyPair(), | ||
new DefaultConnectionInfoCacheFactory(), | ||
new ConcurrentHashMap<>()); | ||
} | ||
|
||
Connector getConnector() { | ||
return this.connector; | ||
} | ||
|
||
@Override | ||
public void close() { | ||
this.executor.shutdown(); | ||
this.alloyDBAdminClient.close(); | ||
} | ||
} |
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.