Skip to content

Commit 76ef18e

Browse files
authored
Automatically detect GCF enviroments and enable unix socket. (#141)
1 parent 48f7ca0 commit 76ef18e

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

core/src/main/java/com/google/cloud/sql/core/CoreSocketFactory.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ public static Socket connect(Properties props, String socketPathFormat) throws I
167167
"cloudSqlInstance property not set. Please specify this property in the JDBC URL or the "
168168
+ "connection Properties with value in form \"project:region:instance\"");
169169

170-
// GAE Standard runtimes provide a connection path at "/cloudsql/<CONNECTION_NAME>"
171-
if (forceUnixSocket || runningOnGaeStandard()) {
170+
// GAE Standard + GCF provide a connection path at "/cloudsql/<CONNECTION_NAME>"
171+
if (forceUnixSocket || runningOnGaeStandard() || runningOnGoogleCloudFunctions()) {
172172
logger.info(
173173
String.format(
174174
"Connecting to Cloud SQL instance [%s] via unix socket.", csqlInstanceName));
@@ -195,6 +195,13 @@ private static boolean runningOnGaeStandard() {
195195
&& ("Production".equals(runEnv) || "java11".equals(gaeRuntime));
196196
}
197197

198+
/** Returns {@code true} if running in a Google Cloud Functions runtime. */
199+
private static boolean runningOnGoogleCloudFunctions() {
200+
// Functions automatically sets a few variables we can use to guess the env:
201+
// See https://cloud.google.com/functions/docs/env-var#nodejs_10_and_subsequent_runtimes
202+
return System.getenv("K_SERVICE") != null && System.getenv("K_REVISION") != null;
203+
}
204+
198205
/**
199206
* Creates a secure socket representing a connection to a Cloud SQL instance.
200207
*

0 commit comments

Comments
 (0)