0

I have the following code...

    val jdbcURL: String = String.format("jdbc:postgresql:///%s", dbName)
    val connProps = new Properties
    connProps.setProperty("user", dbUser)
    // Note: a non-empty string value for the password property must be set. While this property will be ignored when connecting with the Cloud SQL Connector using IAM auth, leaving it empty will cause driver-level validations to fail.
    if( dbUseIAM.equals("true") ){
      println("Using IAM password is ignored")
      connProps.setProperty("password", "ignored")
    } else {
      println("Using manual, password must be provided")
      connProps.setProperty("password", dbPassword)
    }
    connProps.setProperty("sslmode", ssoMode)
    connProps.setProperty("socketFactory", "com.google.cloud.sql.postgres.SocketFactory")
    connProps.setProperty("cloudSqlInstance", instanceConnectionName)
    connProps.setProperty("enableIamAuth", dbUseIAM)
    // Initialize connection pool
    val config = new HikariConfig
    config.setJdbcUrl(jdbcURL)
    config.setDataSourceProperties(connProps)
    config.setMaximumPoolSize(1)
    config.setConnectionTimeout(15000) // 80% of 10s (Per Auth0 Action)
    config.addDataSourceProperty("ipTypes", "PUBLIC,PRIVATE") // TODO: Make configureable
    println("Config created")
    val pool : DataSource = new HikariDataSource(config) // Do we really need Hikari here if it doesn't need pooling?
    println("Returning the datasource")
    Some(pool)

It gets called locally and deployed as a cloud function. Locally the script runs in less than 3 seconds. However, when I deploy to GCP as a Cloud function I see...

2022-07-19T20:10:36.833369Z
identity-corporatesru1oceg4a79 Config created
Info
2022-07-19T20:10:37.331144Z
identity-corporatesru1oceg4a79 Connecting to Cloud SQL instance [...:us-central1:db] via SSL socket.
Info
2022-07-19T20:10:37.331453Z
identity-corporatesru1oceg4a79 First Cloud SQL connection, generating RSA key pair.
Default
2022-07-19T20:10:52.434953Z
identity-corporatesru1oceg4a79 Returning the datasource 

Notice it took about 5 seconds to return the datasource. What is going on here and why is it taking so long from the cloud function? It is causing 504 timeouts on my postman client.

Jackie
  • 21,969
  • 32
  • 147
  • 289
  • May I know if the issue only occurs during the first request or the 504 error happens every time you call your cloud functions? This may happen because of [cold start](https://cloud.google.com/functions/docs/bestpractices/tips#use_dependencies_wisely) – Darwin Jul 20 '22 at 00:21
  • How much less than 3 seconds? – jjanes Jul 20 '22 at 00:21
  • 1
    @Darwin I think you are correct because once I started closing connections properly later calls seemed to work. – Jackie Jul 20 '22 at 14:03

1 Answers1

1

As mentioned in the comment, the delay in your Cloud functions is caused by cold start. What I can suggest is to specify a minimum number of instances in your Cloud Functions to minimize the impact of cold start and reduce latency in your application.

Darwin
  • 450
  • 4
  • 11
  • I do think it is a cold start issue but I added a minimum instance and I still see the same problem when it has sat idle too long – Jackie Jul 21 '22 at 01:39
  • As you can see on this [SO answer](https://stackoverflow.com/a/51790072/10190802), having a minimum number of instances can help reduce/minimize the (but not eliminate) cold starts. Please check the said link for another workaround in your use case. – Darwin Jul 21 '22 at 04:50