Skip to content

Commit 1011bca

Browse files
committed
fix!: avoid long running process when request timeout
Previously function framework use 0 timeout which is actually "no timeout" restrction. This was causing a problem that when user provides a request timeout to Cloud function, process will still continue and consume resources. In this fix, timeout is enabled; default timeout settings is 5 min, same as Cloud run. To make sure timeout settings will be respected, default settings switched from multi-threads to multi-workers. However, user is still allowed to customize workers/threads by assigning env var. But user need to note that timeout won't work when #thread > 1.
1 parent e175553 commit 1011bca

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

src/functions_framework/_http/gunicorn.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ class GunicornApplication(gunicorn.app.base.BaseApplication):
2121
def __init__(self, app, host, port, debug, **options):
2222
self.options = {
2323
"bind": "%s:%s" % (host, port),
24-
"workers": 1,
25-
"threads": (os.cpu_count() or 1) * 4,
26-
"timeout": 0,
24+
"workers": os.environ.get("WORKERS", (os.cpu_count() or 1) * 4),
25+
"threads": os.environ.get("THREADS", 1),
26+
"timeout": os.environ.get("CLOUD_RUN_TIMEOUT_SECONDS", 300),
2727
"loglevel": "error",
2828
"limit_request_line": 0,
2929
}
3030
self.options.update(options)
3131
self.app = app
32+
3233
super().__init__()
3334

3435
def load_config(self):

tests/test_http.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,17 @@ def test_gunicorn_application(debug):
9797
assert gunicorn_app.app == app
9898
assert gunicorn_app.options == {
9999
"bind": "%s:%s" % (host, port),
100-
"workers": 1,
101-
"threads": os.cpu_count() * 4,
102-
"timeout": 0,
100+
"workers": os.cpu_count() * 4,
101+
"threads": 1,
102+
"timeout": 300,
103103
"loglevel": "error",
104104
"limit_request_line": 0,
105105
}
106106

107107
assert gunicorn_app.cfg.bind == ["1.2.3.4:1234"]
108-
assert gunicorn_app.cfg.workers == 1
109-
assert gunicorn_app.cfg.threads == os.cpu_count() * 4
110-
assert gunicorn_app.cfg.timeout == 0
108+
assert gunicorn_app.cfg.workers == os.cpu_count() * 4
109+
assert gunicorn_app.cfg.threads == 1
110+
assert gunicorn_app.cfg.timeout == 300
111111
assert gunicorn_app.load() == app
112112

113113

0 commit comments

Comments
 (0)