MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / createPool

Function createPool

cloud-sql/mysql/mysql2/index.js:56–116  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

54}
55
56const createPool = async () => {
57 const config = {
58 // [START cloud_sql_mysql_mysql2_limit]
59 // 'connectionLimit' is the maximum number of connections the pool is allowed
60 // to keep at once.
61 connectionLimit: 5,
62 // [END cloud_sql_mysql_mysql2_limit]
63
64 // [START cloud_sql_mysql_mysql2_timeout]
65 // 'connectTimeout' is the maximum number of milliseconds before a timeout
66 // occurs during the initial connection to the database.
67 connectTimeout: 10000, // 10 seconds
68 // 'acquireTimeout' is currently unsupported by mysql2
69 // 'waitForConnections' determines the pool's action when no connections are
70 // free. If true, the request will queued and a connection will be presented
71 // when ready. If false, the pool will call back with an error.
72 waitForConnections: true, // Default: true
73 // 'queueLimit' is the maximum number of requests for connections the pool
74 // will queue at once before returning an error. If 0, there is no limit.
75 queueLimit: 0, // Default: 0
76 // [END cloud_sql_mysql_mysql2_timeout]
77
78 // [START cloud_sql_mysql_mysql2_backoff]
79 // The mysql module automatically uses exponential delays between failed
80 // connection attempts.
81 // [END cloud_sql_mysql_mysql2_backoff]
82 };
83
84 // Check if a Secret Manager secret version is defined
85 // If a version is defined, retrieve the secret from Secret Manager and set as the DB_PASS
86 const {CLOUD_SQL_CREDENTIALS_SECRET} = process.env;
87 if (CLOUD_SQL_CREDENTIALS_SECRET) {
88 const secrets = await accessSecretVersion(CLOUD_SQL_CREDENTIALS_SECRET);
89 try {
90 process.env.DB_PASS = secrets.toString();
91 } catch (err) {
92 err.message = `Unable to parse secret from Secret Manager. Make sure that the secret is JSON formatted: \n ${err.message} `;
93 throw err;
94 }
95 }
96
97 if (process.env.INSTANCE_CONNECTION_NAME) {
98 // Uses the Cloud SQL Node.js Connector when INSTANCE_CONNECTION_NAME
99 // (e.g., project:region:instance) is defined
100 if (process.env.DB_IAM_USER) {
101 // Either a DB_USER or a DB_IAM_USER should be defined. If both are
102 // defined, DB_IAM_USER takes precedence
103 return createConnectorIAMAuthnPool(config);
104 } else {
105 return createConnectorPool(config);
106 }
107 } else if (process.env.INSTANCE_HOST) {
108 // Use a TCP socket when INSTANCE_HOST (e.g., 127.0.0.1) is defined
109 return createTcpPool(config);
110 } else if (process.env.INSTANCE_UNIX_SOCKET) {
111 // Use a Unix socket when INSTANCE_UNIX_SOCKET (e.g., /cloudsql/proj:region:instance) is defined.
112 return createUnixSocketPool(config);
113 } else {

Callers 1

Calls 4

toStringMethod · 0.80
accessSecretVersionFunction · 0.70
createTcpPoolFunction · 0.50
createUnixSocketPoolFunction · 0.50

Tested by

no test coverage detected