diff --git a/core/src/main/java/com/google/cloud/sql/core/CoreSocketFactory.java b/core/src/main/java/com/google/cloud/sql/core/CoreSocketFactory.java index 48f49f2f5..435ab7e5c 100644 --- a/core/src/main/java/com/google/cloud/sql/core/CoreSocketFactory.java +++ b/core/src/main/java/com/google/cloud/sql/core/CoreSocketFactory.java @@ -167,8 +167,8 @@ public static Socket connect(Properties props, String socketPathFormat) throws I "cloudSqlInstance property not set. Please specify this property in the JDBC URL or the " + "connection Properties with value in form \"project:region:instance\""); - // GAE Standard runtimes provide a connection path at "/cloudsql/" - if (forceUnixSocket || runningOnGaeStandard()) { + // GAE Standard + GCF provide a connection path at "/cloudsql/" + if (forceUnixSocket || runningOnGaeStandard() || runningOnGoogleCloudFunctions()) { logger.info( String.format( "Connecting to Cloud SQL instance [%s] via unix socket.", csqlInstanceName)); @@ -195,6 +195,13 @@ private static boolean runningOnGaeStandard() { && ("Production".equals(runEnv) || "java11".equals(gaeRuntime)); } + /** Returns {@code true} if running in a Google Cloud Functions runtime. */ + private static boolean runningOnGoogleCloudFunctions() { + // Functions automatically sets a few variables we can use to guess the env: + // See https://cloud.google.com/functions/docs/env-var#nodejs_10_and_subsequent_runtimes + return System.getenv("K_SERVICE") != null && System.getenv("K_REVISION") != null; + } + /** * Creates a secure socket representing a connection to a Cloud SQL instance. *