Skip to content

Commit 71b66ac

Browse files
authored
Improve GAE Standard detection (#86)
* Updated connector-j-8 SocketFactory. * Updated connector-j-6 SocketFactory. * Updated connector-j-5 SocketFactory. * Updated postgres.
1 parent 7b8590b commit 71b66ac

File tree

4 files changed

+63
-36
lines changed

4 files changed

+63
-36
lines changed

connector-j-5/src/main/java/com/google/cloud/sql/mysql/SocketFactory.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,25 @@ public Socket connect(String hostname, int portNumber, Properties props) throws
4747
"cloudSqlInstance property not set. Please specify this property in the JDBC URL or "
4848
+ "the connection Properties with value in form \"project:region:instance\"");
4949

50-
logger.info(String.format("Connecting to Cloud SQL instance [%s].", instanceName));
50+
// gaeEnv="standard" indicates standard instances
51+
// runEnv="Production" indicates production instances
52+
String gaeEnv = System.getenv("GAE_ENV");
53+
String runEnv = System.getProperty("com.google.appengine.runtime.environment");
54+
// Custom env variable for forcing unix socket
55+
Boolean forceUnixSocket = System.getenv("CLOUD_SQL_FORCE_UNIX_SOCKET") != null;
5156

52-
// This env will be set by GAE OR set manually if using Cloud SQL Proxy
53-
String runtime = System.getenv("GAE_RUNTIME");
54-
55-
if (runtime == null || runtime.isEmpty()) { // Use standard SSL (direct connection)
56-
this.socket = SslSocketFactory.getInstance().create(instanceName);
57-
} else { // Use Unix Socket
58-
logger.info("Using GAE Unix Sockets");
57+
// If running on GAE Standard, connect with unix socket
58+
if (forceUnixSocket || "standard".equals(gaeEnv) && "Production".equals(runEnv)) {
59+
logger.info(String.format(
60+
"Connecting to Cloud SQL instance [%s] via unix socket.", instanceName));
5961
UnixSocketAddress socketAddress = new UnixSocketAddress(
6062
new File(CloudSqlPrefix + instanceName));
6163
this.socket = UnixSocketChannel.open(socketAddress).socket();
64+
} else {
65+
// Default to SSL Socket
66+
logger.info(String.format(
67+
"Connecting to Cloud SQL instance [%s] via ssl socket.", instanceName));
68+
this.socket = SslSocketFactory.getInstance().create(instanceName);
6269
}
6370
return this.socket;
6471
}

connector-j-6/src/main/java/com/google/cloud/sql/mysql/SocketFactory.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,25 @@ public Socket connect(String host, int portNumber, Properties props, int loginTi
4747
"cloudSqlInstance property not set. Please specify this property in the JDBC URL or "
4848
+ "the connection Properties with value in form \"project:region:instance\"");
4949

50-
logger.info(String.format("Connecting to Cloud SQL instance [%s].", instanceName));
50+
// gaeEnv="standard" indicates standard instances
51+
// runEnv="Production" indicates production instances
52+
String gaeEnv = System.getenv("GAE_ENV");
53+
String runEnv = System.getProperty("com.google.appengine.runtime.environment");
54+
// Custom env variable for forcing unix socket
55+
Boolean forceUnixSocket = System.getenv("CLOUD_SQL_FORCE_UNIX_SOCKET") != null;
5156

52-
// This env will be set by GAE OR set manually if using Cloud SQL Proxy
53-
String runtime = System.getenv("GAE_RUNTIME");
54-
55-
if (runtime == null || runtime.isEmpty()) { // Use standard SSL (direct connection)
56-
this.socket = SslSocketFactory.getInstance().create(instanceName);
57-
} else { // Use Unix Socket
58-
logger.info("Using GAE Unix Sockets");
57+
// If running on GAE Standard, connect with unix socket
58+
if (forceUnixSocket || "standard".equals(gaeEnv) && "Production".equals(runEnv)) {
59+
logger.info(String.format(
60+
"Connecting to Cloud SQL instance [%s] via unix socket.", instanceName));
5961
UnixSocketAddress socketAddress = new UnixSocketAddress(
6062
new File(CloudSqlPrefix + instanceName));
6163
this.socket = UnixSocketChannel.open(socketAddress).socket();
64+
} else {
65+
// Default to SSL Socket
66+
logger.info(String.format(
67+
"Connecting to Cloud SQL instance [%s] via ssl socket.", instanceName));
68+
this.socket = SslSocketFactory.getInstance().create(instanceName);
6269
}
6370
return this.socket;
6471
}

connector-j-8/src/main/java/com/google/cloud/sql/mysql/SocketFactory.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.mysql.cj.protocol.ServerSession;
2323
import com.mysql.cj.protocol.SocketConnection;
2424

25-
import java.io.Closeable;
2625
import java.io.File;
2726
import java.io.IOException;
2827
import java.net.Socket;
@@ -51,22 +50,30 @@ public Socket connect(String host, int portNumber, Properties props, int loginTi
5150
"cloudSqlInstance property not set. Please specify this property in the JDBC URL or "
5251
+ "the connection Properties with value in form \"project:region:instance\"");
5352

54-
logger.info(String.format("Connecting to Cloud SQL instance [%s].", instanceName));
53+
// gaeEnv="standard" indicates standard instances
54+
// runEnv="Production" indicates production instances
55+
String gaeEnv = System.getenv("GAE_ENV");
56+
String runEnv = System.getProperty("com.google.appengine.runtime.environment");
57+
// Custom env variable for forcing unix socket
58+
Boolean forceUnixSocket = System.getenv("CLOUD_SQL_FORCE_UNIX_SOCKET") != null;
5559

56-
// This env will be set by GAE OR set manually if using Cloud SQL Proxy
57-
String runtime = System.getenv("GAE_RUNTIME");
58-
59-
if (runtime == null || runtime.isEmpty()) { // Use standard SSL (direct connection)
60-
this.socket = SslSocketFactory.getInstance().create(instanceName);
61-
} else { // Use Unix Socket
62-
logger.info("Using GAE Unix Sockets");
60+
// If running on GAE Standard, connect with unix socket
61+
if (forceUnixSocket || "standard".equals(gaeEnv) && "Production".equals(runEnv)) {
62+
logger.info(String.format(
63+
"Connecting to Cloud SQL instance [%s] via unix socket.", instanceName));
6364
UnixSocketAddress socketAddress = new UnixSocketAddress(
6465
new File(CloudSqlPrefix + instanceName));
6566
this.socket = UnixSocketChannel.open(socketAddress).socket();
67+
} else {
68+
// Default to SSL Socket
69+
logger.info(String.format(
70+
"Connecting to Cloud SQL instance [%s] via ssl socket.", instanceName));
71+
this.socket = SslSocketFactory.getInstance().create(instanceName);
6672
}
6773
return this.socket;
6874
}
6975

76+
7077
// Cloud SQL sockets always use TLS and the socket returned by connect above is already TLS-ready. It is fine
7178
// to implement these as no-ops.
7279

postgres/src/main/java/com/google/cloud/sql/postgres/SocketFactory.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,25 @@ public SocketFactory(String instanceName) {
5252

5353
@Override
5454
public Socket createSocket() throws IOException {
55-
logger.info(String.format("Connecting to Cloud SQL instance [%s].", instanceName));
55+
// gaeEnv="standard" indicates standard instances
56+
// runEnv="Production" indicates production instances
57+
String gaeEnv = System.getenv("GAE_ENV");
58+
String runEnv = System.getProperty("com.google.appengine.runtime.environment");
59+
// Custom env variable for forcing unix socket
60+
Boolean forceUnixSocket = System.getenv("CLOUD_SQL_FORCE_UNIX_SOCKET") != null;
5661

57-
// This env will be set by GAE OR set manually if using Cloud SQL Proxy
58-
String runtime = System.getenv("GAE_RUNTIME");
59-
60-
if (runtime == null || runtime.isEmpty()) { // Use standard SSL (direct connection)
61-
return SslSocketFactory.getInstance().create(instanceName);
62+
// If running on GAE Standard, connect with unix socket
63+
if (forceUnixSocket || "standard".equals(gaeEnv) && "Production".equals(runEnv)) {
64+
logger.info(String.format(
65+
"Connecting to Cloud SQL instance [%s] via unix socket.", instanceName));
66+
UnixSocketAddress socketAddress = new UnixSocketAddress(
67+
new File(CloudSqlPrefix + instanceName + PostgreSqlSufix));
68+
return UnixSocketChannel.open(socketAddress).socket();
6269
}
63-
logger.info("GAE Unix Sockets");
64-
UnixSocketAddress socketAddress = new UnixSocketAddress(
65-
new File(CloudSqlPrefix + instanceName + PostgreSqlSufix));
66-
UnixSocket socket = UnixSocketChannel.open(socketAddress).socket();
67-
return socket;
70+
// Default to SSL Socket
71+
logger.info(String.format(
72+
"Connecting to Cloud SQL instance [%s] via ssl socket.", instanceName));
73+
return SslSocketFactory.getInstance().create(instanceName);
6874
}
6975

7076
@Override

0 commit comments

Comments
 (0)