Skip to content

Commit ef240c5

Browse files
fix: lazy init db connection for Cloud Function (GoogleCloudPlatform#10576)
1 parent aaf4a56 commit ef240c5

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

cloud-sql/mysql/sqlalchemy/main.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@
2020

2121
############ TABS vs. SPACES App for Cloud Functions ############
2222

23-
# initiate a connection pool to a Cloud SQL database
24-
db = init_connection_pool()
25-
# creates required 'votes' table in database (if it does not exist)
26-
migrate_db(db)
23+
# lazy global initialization
24+
# db connection must be established within request context
25+
db = None
2726

2827

2928
@functions_framework.http
@@ -36,6 +35,13 @@ def votes(request: Request) -> Response:
3635
Returns:
3736
Flask HTTP Response to the client.
3837
"""
38+
global db
39+
# initialize db within request context
40+
if not db:
41+
# initiate a connection pool to a Cloud SQL database
42+
db = init_connection_pool()
43+
# creates required 'votes' table in database (if it does not exist)
44+
migrate_db(db)
3945
if request.method == "GET":
4046
context = get_index_context(db)
4147
return render_template("index.html", **context)

cloud-sql/postgres/sqlalchemy/main.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,20 @@
2020

2121
############ TABS vs. SPACES App for Cloud Functions ############
2222

23-
# initiate a connection pool to a Cloud SQL database
24-
db = init_connection_pool()
25-
# creates required 'votes' table in database (if it does not exist)
26-
migrate_db(db)
23+
# lazy global initialization
24+
# db connection must be established within request context
25+
db = None
2726

2827

2928
@functions_framework.http
3029
def votes(request: Request) -> Response:
30+
global db
31+
# initialize db within request context
32+
if not db:
33+
# initiate a connection pool to a Cloud SQL database
34+
db = init_connection_pool()
35+
# creates required 'votes' table in database (if it does not exist)
36+
migrate_db(db)
3137
if request.method == "GET":
3238
context = get_index_context(db)
3339
return render_template("index.html", **context)

cloud-sql/sql-server/sqlalchemy/main.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,28 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from flask import render_template, Response
15+
from flask import render_template, Request, Response
1616

1717
import functions_framework
1818

1919
from app import get_index_context, init_connection_pool, migrate_db, save_vote
2020

2121
############ TABS vs. SPACES App for Cloud Functions ############
2222

23-
# initiate a connection pool to a Cloud SQL database
24-
db = init_connection_pool()
25-
# creates required 'votes' table in database (if it does not exist)
26-
migrate_db(db)
23+
# lazy global initialization
24+
# db connection must be established within request context
25+
db = None
2726

2827

2928
@functions_framework.http
30-
def votes(request):
29+
def votes(request: Request) -> Response:
30+
global db
31+
# initialize db within request context
32+
if not db:
33+
# initiate a connection pool to a Cloud SQL database
34+
db = init_connection_pool()
35+
# creates required 'votes' table in database (if it does not exist)
36+
migrate_db(db)
3137
if request.method == "GET":
3238
context = get_index_context(db)
3339
return render_template("index.html", **context)

0 commit comments

Comments
 (0)