Skip to content

Commit abc8c21

Browse files
authored
Recommend max instances in functions samples (#8772)
* Recommend max instances in functions samples * Changelog * 80 character column limit
1 parent af176ef commit abc8c21

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
- Updated the Data Connect emulator to use pglite 0.3.x and Postgres 17, which fixes some crashes related to wire protocol inconsistencies. (#8679, #8658)
22
- Remove container cleanup logic in functions:delete command (#8771)
33
- Fixed an issue where the IAM enablement for GenKit monitoring would try to change an invalid service account. (#8756)
4+
- Added a max instance default to function templates and comments educating users on cost controls. (#8772)
45
- Added caching to API enablement checks to reduce burn of `serviceusage.googleapis.com` quota.

templates/init/functions/javascript/index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,22 @@
77
* See a full list of supported triggers at https://firebase.google.com/docs/functions
88
*/
99

10-
const {onRequest} = require("firebase-functions/v2/https");
10+
const {setGlobalOptions} = require("firebase-functions");
11+
const {onRequest} = require("firebase-functions/https");
1112
const logger = require("firebase-functions/logger");
1213

14+
// For cost control, you can set the maximum number of containers that can be
15+
// running at the same time. This helps mitigate the impact of unexpected
16+
// traffic spikes by instead downgrading performance. This limit is a
17+
// per-function limit. You can override the limit for each function using the
18+
// `maxInstances` option in the function's options, e.g.
19+
// `onRequest({ maxInstances: 5 }, (req, res) => { ... })`.
20+
// NOTE: setGlobalOptions does not apply to functions using the v1 API. V1
21+
// functions should each use functions.runWith({ maxInstances: 10 }) instead.
22+
// In the v1 API, each function can only serve one request per container, so
23+
// this will be the maximum concurrent request count.
24+
setGlobalOptions({ maxInstances: 10 });
25+
1326
// Create and deploy your first functions
1427
// https://firebase.google.com/docs/functions/get-started
1528

templates/init/functions/python/main.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,16 @@
33
# Deploy with `firebase deploy`
44

55
from firebase_functions import https_fn
6+
from firebase_functions.options import set_global_options
67
from firebase_admin import initialize_app
78

9+
# For cost control, you can set the maximum number of containers that can be
10+
# running at the same time. This helps mitigate the impact of unexpected
11+
# traffic spikes by instead downgrading performance. This limit is a per-function
12+
# limit. You can override the limit for each function using the max_instances
13+
# parameter in the decorator, e.g. @https_fn.on_request(max_instances=5).
14+
set_global_options(max_instances=10)
15+
816
# initialize_app()
917
#
1018
#

templates/init/functions/typescript/index.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,25 @@
77
* See a full list of supported triggers at https://firebase.google.com/docs/functions
88
*/
99

10-
import {onRequest} from "firebase-functions/v2/https";
10+
import {setGlobalOptions} from "firebase-functions";
11+
import {onRequest} from "firebase-functions/https";
1112
import * as logger from "firebase-functions/logger";
1213

1314
// Start writing functions
1415
// https://firebase.google.com/docs/functions/typescript
1516

17+
// For cost control, you can set the maximum number of containers that can be
18+
// running at the same time. This helps mitigate the impact of unexpected
19+
// traffic spikes by instead downgrading performance. This limit is a
20+
// per-function limit. You can override the limit for each function using the
21+
// `maxInstances` option in the function's options, e.g.
22+
// `onRequest({ maxInstances: 5 }, (req, res) => { ... })`.
23+
// NOTE: setGlobalOptions does not apply to functions using the v1 API. V1
24+
// functions should each use functions.runWith({ maxInstances: 10 }) instead.
25+
// In the v1 API, each function can only serve one request per container, so
26+
// this will be the maximum concurrent request count.
27+
setGlobalOptions({ maxInstances: 10 });
28+
1629
// export const helloWorld = onRequest((request, response) => {
1730
// logger.info("Hello logs!", {structuredData: true});
1831
// response.send("Hello from Firebase!");

0 commit comments

Comments
 (0)