Skip to content

Commit

Permalink
handle IOExceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
shubha-rajan committed Nov 1, 2022
1 parent 85a8507 commit 2528959
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,29 @@ public static synchronized CoreSocketFactory getInstance() {
return coreSocketFactory;
}

private CloudSqlInstance getCloudSqlInstance(String instanceName, boolean enableIamAuth) {
private CloudSqlInstance getCloudSqlInstance(String instanceName, boolean enableIamAuth) {
return instances.computeIfAbsent(
instanceName,
k -> new CloudSqlInstance(k, adminApi, enableIamAuth, credentialFactory, executor,
localKeyPair));
k -> {
try {
return new CloudSqlInstance(k, adminApi, enableIamAuth, credentialFactory, executor,
localKeyPair);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}

private CloudSqlInstance getCloudSqlInstance(String instanceName) {
private CloudSqlInstance getCloudSqlInstance(String instanceName) {
return instances.computeIfAbsent(
instanceName,
k -> new CloudSqlInstance(k, adminApi, false, credentialFactory, executor, localKeyPair));
k -> {
try {
return new CloudSqlInstance(k, adminApi, false, credentialFactory, executor, localKeyPair);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}

static int getDefaultServerProxyPort() {
Expand Down Expand Up @@ -255,22 +267,23 @@ public static Socket connect(Properties props, String unixPathSuffix) throws IOE
/**
* Returns data that can be used to establish Cloud SQL SSL connection.
*/
public static SslData getSslData(String csqlInstanceName, boolean enableIamAuth) {
public static SslData getSslData(String csqlInstanceName, boolean enableIamAuth)
throws IOException {
return getInstance().getCloudSqlInstance(csqlInstanceName, enableIamAuth).getSslData();
}

public static SslData getSslData(String csqlInstanceName) {
public static SslData getSslData(String csqlInstanceName) throws IOException {
return getSslData(csqlInstanceName, false);
}

/**
* Returns preferred ip address that can be used to establish Cloud SQL connection.
*/
public static String getHostIp(String csqlInstanceName) {
public static String getHostIp(String csqlInstanceName) throws IOException {
return getInstance().getHostIp(csqlInstanceName, listIpTypes(DEFAULT_IP_TYPES));
}

private String getHostIp(String instanceName, List<String> ipTypes) {
private String getHostIp(String instanceName, List<String> ipTypes) throws IOException {
CloudSqlInstance instance = getCloudSqlInstance(instanceName);
return instance.getPreferredIp(ipTypes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.r2dbc.spi.ConnectionFactoryMetadata;
import io.r2dbc.spi.ConnectionFactoryOptions;
import io.r2dbc.spi.ConnectionFactoryOptions.Builder;
import java.io.IOException;
import java.util.function.Function;
import org.reactivestreams.Publisher;

Expand All @@ -50,15 +51,25 @@ public CloudSqlConnectionFactory(

@Override
public Publisher<? extends Connection> create() {
return getConnectionFactory().create();
try {
return getConnectionFactory().create();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

@Override
public ConnectionFactoryMetadata getMetadata() {
return getConnectionFactory().getMetadata();
public ConnectionFactoryMetadata getMetadata() {
try {
return getConnectionFactory().getMetadata();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

private ConnectionFactory getConnectionFactory() {
private ConnectionFactory getConnectionFactory() throws IOException {
String hostIp = CoreSocketFactory.getHostIp(csqlHostName);
builder.option(HOST, hostIp).option(PORT, CoreSocketFactory.getDefaultServerProxyPort());
return connectionFactoryFactory.apply(builder.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.r2dbc.spi.ConnectionFactoryOptions;
import io.r2dbc.spi.ConnectionFactoryProvider;
import io.r2dbc.spi.Option;
import java.io.IOException;
import java.util.function.Function;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
Expand All @@ -45,7 +46,13 @@ private static Function<SslContextBuilder, SslContextBuilder> createSslCustomize
sslContextBuilder -> {
// Execute in a default scheduler to prevent it from blocking event loop
SslData sslData = Mono
.fromSupplier(() -> CoreSocketFactory.getSslData(connectionName, enableIamAuth))
.fromSupplier(() -> {
try {
return CoreSocketFactory.getSslData(connectionName, enableIamAuth);
} catch (IOException e) {
throw new RuntimeException(e);
}
})
.subscribeOn(Schedulers.boundedElastic())
.share()
.block();
Expand Down Expand Up @@ -93,11 +100,15 @@ public ConnectionFactory create(ConnectionFactoryOptions connectionFactoryOption
"Cannot create ConnectionFactory: unsupported protocol (" + protocol + ")");
}

return createFactory(connectionFactoryOptions);
try {
return createFactory(connectionFactoryOptions);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private ConnectionFactory createFactory(
ConnectionFactoryOptions connectionFactoryOptions) {
private ConnectionFactory createFactory (
ConnectionFactoryOptions connectionFactoryOptions) throws IOException{
String connectionName = (String) connectionFactoryOptions.getRequiredValue(HOST);
String socket = (String) connectionFactoryOptions.getValue(UNIX_SOCKET);

Expand Down

0 comments on commit 2528959

Please sign in to comment.