שיטות מומלצות לשימוש בפונקציות

במסמך הזה מתוארות שיטות מומלצות לתכנון, להטמעה, לבדיקה ולפריסה של פונקציות Cloud Run.

נכונות

בקטע הזה מתוארות שיטות מומלצות כלליות לתכנון וליישום של פונקציות Cloud Run.

כתיבת פונקציות אידמפוטנטיות

הפונקציות צריכות להפיק את אותה תוצאה גם אם קוראים להן כמה פעמים. כך תוכלו לנסות שוב הפעלה אם ההפעלה הקודמת נכשלה באמצע הקוד. מידע נוסף זמין במאמר בנושא ניסיון חוזר של פונקציות מבוססות-אירועים.

מוודאים שפונקציות HTTP שולחות תגובת HTTP

אם הפונקציה מופעלת על ידי HTTP, חשוב לשלוח תגובת HTTP, כמו שמוצג בהמשך. אם לא תעשו את זה, הפונקציה תמשיך לפעול עד שתגיע לזמן הקצוב לתפוגה. במקרה כזה, תחויבו על כל משך הזמן של פסק הזמן. פסק זמן עלול גם לגרום להתנהגות בלתי צפויה או להפעלות קרות בקריאות הבאות, וכתוצאה מכך להתנהגות בלתי צפויה או לחביון נוסף.

Node.js

const functions = require('@google-cloud/functions-framework');
const escapeHtml = require('escape-html');

/**
 * Responds to an HTTP request using data from the request body parsed according
 * to the "content-type" header.
 *
 * @param {Object} req Cloud Function request context.
 * @param {Object} res Cloud Function response context.
 */
functions.http('helloHttp', (req, res) => {
  res.send(`Hello ${escapeHtml(req.query.name || req.body.name || 'World')}!`);
});

Python


import functions_framework


from markupsafe import escape

@functions_framework.http
def hello_http(request):
    """HTTP Cloud Function.
    Args:
        request (flask.Request): The request object.
        <https://flask.palletsprojects.com/en/1.1.x/api/#incoming-request-data>
    Returns:
        The response text, or any set of values that can be turned into a
        Response object using `make_response`
        <https://flask.palletsprojects.com/en/1.1.x/api/#flask.make_response>.
    """
    request_json = request.get_json(silent=True)
    request_args = request.args

    if request_json and "name" in request_json:
        name = request_json["name"]
    elif request_args and "name" in request_args:
        name = request_args["name"]
    else:
        name = "World"
    return f"Hello {escape(name)}!"

Go


// Package helloworld provides a set of Cloud Functions samples.
package helloworld

import (
	"encoding/json"
	"fmt"
	"html"
	"net/http"

	"github.com/GoogleCloudPlatform/functions-framework-go/functions"
)

func init() {
	functions.HTTP("HelloHTTP", HelloHTTP)
}

// HelloHTTP is an HTTP Cloud Function with a request parameter.
func HelloHTTP(w http.ResponseWriter, r *http.Request) {
	var d struct {
		Name string `json:"name"`
	}
	if err := json.NewDecoder(r.Body).Decode(&d); err != nil {
		fmt.Fprint(w, "Hello, World!")
		return
	}
	if d.Name == "" {
		fmt.Fprint(w, "Hello, World!")
		return
	}
	fmt.Fprintf(w, "Hello, %s!", html.EscapeString(d.Name))
}

Java


import com.google.cloud.functions.HttpFunction;
import com.google.cloud.functions.HttpRequest;
import com.google.cloud.functions.HttpResponse;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Logger;

public class HelloHttp implements HttpFunction {
  private static final Logger logger = Logger.getLogger(HelloHttp.class.getName());

  private static final Gson gson = new Gson();

  @Override
  public void service(HttpRequest request, HttpResponse response)
      throws IOException {
    // Check URL parameters for "name" field
    // "world" is the default value
    String name = request.getFirstQueryParameter("name").orElse("world");

    // Parse JSON request and check for "name" field
    try {
      JsonElement requestParsed = gson.fromJson(request.getReader(), JsonElement.class);
      JsonObject requestJson = null;

      if (requestParsed != null && requestParsed.isJsonObject()) {
        requestJson = requestParsed.getAsJsonObject();
      }

      if (requestJson != null && requestJson.has("name")) {
        name = requestJson.get("name").getAsString();
      }
    } catch (JsonParseException e) {
      logger.severe("Error parsing JSON: " + e.getMessage());
    }

    var writer = new PrintWriter(response.getWriter());
    writer.printf("Hello %s!", name);
  }
}

C#‎

using Google.Cloud.Functions.Framework;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;

namespace HelloHttp;

public class Function : IHttpFunction
{
    private readonly ILogger _logger;

    public Function(ILogger<Function> logger) =>
        _logger = logger;

    public async Task HandleAsync(HttpContext context)
    {
        HttpRequest request = context.Request;
        // Check URL parameters for "name" field
        // "world" is the default value
        string name = ((string) request.Query["name"]) ?? "world";

        // If there's a body, parse it as JSON and check for "name" field.
        using TextReader reader = new StreamReader(request.Body);
        string text = await reader.ReadToEndAsync();
        if (text.Length > 0)
        {
            try
            {
                JsonElement json = JsonSerializer.Deserialize<JsonElement>(text);
                if (json.TryGetProperty("name", out JsonElement nameElement) &&
                    nameElement.ValueKind == JsonValueKind.String)
                {
                    name = nameElement.GetString();
                }
            }
            catch (JsonException parseException)
            {
                _logger.LogError(parseException, "Error parsing JSON request");
            }
        }

        await context.Response.WriteAsync($"Hello {name}!", context.RequestAborted);
    }
}

Ruby

require "functions_framework"
require "cgi"
require "json"

FunctionsFramework.http "hello_http" do |request|
  # The request parameter is a Rack::Request object.
  # See https://www.rubydoc.info/gems/rack/Rack/Request
  name = request.params["name"] ||
         (request.body.rewind && JSON.parse(request.body.read)["name"] rescue nil) ||
         "World"
  # Return the response body as a string.
  # You can also return a Rack::Response object, a Rack response array, or
  # a hash which will be JSON-encoded into a response.
  "Hello #{CGI.escape_html name}!"
end

PHP

<?php

use Google\CloudFunctions\FunctionsFramework;
use Psr\Http\Message\ServerRequestInterface;

// Register the function with Functions Framework.
// This enables omitting the `FUNCTIONS_SIGNATURE_TYPE=http` environment
// variable when deploying. The `FUNCTION_TARGET` environment variable should
// match the first parameter.
FunctionsFramework::http('helloHttp', 'helloHttp');

function helloHttp(ServerRequestInterface $request): string
{
    $name = 'World';
    $body = $request->getBody()->getContents();
    if (!empty($body)) {
        $json = json_decode($body, true);
        if (json_last_error() != JSON_ERROR_NONE) {
            throw new RuntimeException(sprintf(
                'Could not parse body: %s',
                json_last_error_msg()
            ));
        }
        $name = $json['name'] ?? $name;
    }
    $queryString = $request->getQueryParams();
    $name = $queryString['name'] ?? $name;

    return sprintf('Hello, %s!', htmlspecialchars($name));
}

לא להפעיל פעילויות ברקע

פעילות ברקע היא כל מה שקורה אחרי שהפונקציה מסתיימת. הפעלת פונקציה מסתיימת כשהפונקציה מחזירה ערך או מסמנת שהיא הושלמה, למשל על ידי קריאה לארגומנט callback בפונקציות מבוססות-אירועים ב-Node.js. לכל קוד שיופעל אחרי סיום תקין לא תהיה גישה למעבד, והוא לא יתקדם.

בנוסף, כשמבצעים הפעלה עוקבת באותה סביבה, הפעילות ברקע ממשיכה ומשבשת את ההפעלה החדשה. הדבר עלול לגרום להתנהגות לא צפויה ולשגיאות שקשה לאבחן. גישה לרשת אחרי סיום הפונקציה מובילה בדרך כלל לאיפוס החיבורים (ECONNRESET קוד שגיאה).

לעתים קרובות אפשר לזהות פעילות ברקע ביומנים של קריאות נפרדות לפונקציה, על ידי חיפוש של כל מה שמתועד אחרי השורה שמציינת שהקריאה לפונקציה הסתיימה. לפעמים פעילות ברקע מוסתרת עמוק יותר בקוד, במיוחד כשקיימות פעולות אסינכרוניות כמו קריאות חוזרות או טיימרים. בודקים את הקוד כדי לוודא שכל הפעולות האסינכרוניות מסתיימות לפני שמסיימים את הפונקציה.

מחיקת קבצים זמניים תמיד

אחסון בדיסק מקומי בספרייה הזמנית הוא מערכת קבצים בזיכרון. קבצים שאתם כותבים צורכים זיכרון שזמין לפונקציה, ולפעמים נשמרים בין הפעלות. אם לא מוחקים את הקבצים האלה באופן מפורש, יכול להיות שבסופו של דבר תתקבל שגיאת חוסר זיכרון, ולאחר מכן יתבצע אתחול קר.

כדי לראות את הזיכרון שבו נעשה שימוש בפונקציה מסוימת, בוחרים אותה ברשימת הפונקציות בGoogle Cloud מסוף ובוחרים בתרשים Memory usage (שימוש בזיכרון).

אם אתם צריכים גישה לאחסון לטווח ארוך, כדאי להשתמש בהרכבות של נפחים ב-Cloud Run עם Cloud Storage או נפחי NFS.

כדי להקטין את דרישות הזיכרון כשמעבדים קבצים גדולים יותר, אפשר להשתמש בצינורות. לדוגמה, אפשר לעבד קובץ ב-Cloud Storage על ידי יצירת זרם קריאה, העברתו דרך תהליך מבוסס-זרם וכתיבת זרם הפלט ישירות ל-Cloud Storage.

Functions Framework

כדי לוודא שאותן תלויות מותקנות באופן עקבי בסביבות שונות, מומלץ לכלול את ספריית Functions Framework במנהל החבילות ולהצמיד את התלות לגרסה ספציפית של Functions Framework.

כדי לעשות את זה, צריך לכלול את הגרסה המועדפת בקובץ הנעילה הרלוונטי (לדוגמה, package-lock.json ל-Node.js או requirements.txt ל-Python).

אם Functions Framework לא מופיע במפורש כרכיב תלוי, הוא יתווסף אוטומטית במהלך תהליך הבנייה באמצעות הגרסה העדכנית ביותר שזמינה.

כלים

בקטע הזה מפורטות הנחיות לשימוש בכלים להטמעה, לבדיקה ולביצוע אינטראקציה עם פונקציות Cloud Run.

פיתוח מקומי

פריסת הפונקציה אורכת זמן, ולכן בדרך כלל מהר יותר לבדוק את הקוד של הפונקציה באופן מקומי.

דיווח על שגיאות

בשפות שמשתמשות בטיפול בחריגים, אל תפעילו חריגים שלא נתפסו, כי הם גורמים להפעלות במצב התחלתי (cold start) בהפעלות עתידיות.

לא יוצאים באופן ידני

יציאה ידנית עלולה לגרום להתנהגות לא צפויה. במקום זאת, צריך להשתמש בביטויים הבאים שספציפיים לשפה:

Node.js

אל תשתמשו ב-process.exit(). פונקציות HTTP צריכות לשלוח תגובה עם res.status(200).send(message), ופונקציות מבוססות-אירוע יסיימו את הפעולה שלהן אחרי שהן יחזירו ערך (באופן מרומז או מפורש).

Python

אל תשתמשו ב-sys.exit(). פונקציות HTTP צריכות להחזיר באופן מפורש תגובה כמחרוזת, ופונקציות מבוססות-אירועים יסיימו את הפעולה ברגע שהן יחזירו ערך (באופן מרומז או מפורש).

Go

אל תשתמשו ב-os.Exit(). פונקציות HTTP צריכות להחזיר באופן מפורש תגובה כמחרוזת, ופונקציות מבוססות-אירועים יסיימו את הפעולה ברגע שהן יחזירו ערך (באופן מרומז או מפורש).

Java

אל תשתמשו ב-System.exit(). פונקציות HTTP צריכות לשלוח תגובה עם response.getWriter().write(message), ופונקציות מבוססות-אירוע יסיימו את הפעולה שלהן אחרי שהן יחזירו ערך (באופן מרומז או מפורש).

C#‎

אל תשתמשו ב-System.Environment.Exit(). פונקציות HTTP צריכות לשלוח תגובה עם context.Response.WriteAsync(message), ופונקציות מבוססות-אירוע יסיימו את הפעולה שלהן אחרי שהן יחזירו ערך (באופן מרומז או מפורש).

Ruby

אסור להשתמש ב-exit() או ב-abort(). פונקציות HTTP צריכות להחזיר באופן מפורש תגובה כמחרוזת, ופונקציות מבוססות-אירועים יסיימו את הפעולה ברגע שהן יחזירו ערך (באופן מרומז או מפורש).

PHP

אסור להשתמש ב-exit() או ב-die(). פונקציות HTTP צריכות להחזיר באופן מפורש תגובה כמחרוזת, ופונקציות מבוססות-אירועים יסיימו את הפעולה ברגע שהן יחזירו ערך (באופן מרומז או מפורש).

שימוש ב-Sendgrid לשליחת אימיילים

פונקציות Cloud Run לא מאפשרות חיבורים יוצאים ביציאה 25, ולכן אי אפשר ליצור חיבורים לא מאובטחים לשרת SMTP. הדרך המומלצת לשלוח אימיילים היא באמצעות שירות של צד שלישי כמו SendGrid. אפשר למצוא אפשרויות נוספות לשליחת אימייל במדריך שליחת אימייל ממופע של Google Compute Engine.

ביצועים

בקטע הזה מתוארות שיטות מומלצות לאופטימיזציה של הביצועים.

הימנעות משימוש במספר קטן של תהליכים מקבילים

הפעלות במצב התחלתי (cold start) הן יקרות, ולכן היכולת לעשות שימוש חוזר במופעים שהופעלו לאחרונה במהלך עלייה חדה היא אופטימיזציה מצוינת לטיפול בעומס. הגבלת מספר הבקשות בו-זמנית מגבילה את האופן שבו אפשר להשתמש במופעים קיימים, ולכן גורמת ליותר הפעלות במצב התחלתי (cold start).

הגדלת מספר הבקשות המקבילות עוזרת לדחות מספר בקשות לכל מופע, וכך קל יותר לטפל בעליות פתאומיות בעומס.

שימוש חכם בתלות

מכיוון שהפונקציות הן חסרות מצב, סביבת ההפעלה מאותחלת לעיתים קרובות מאפס (במהלך מה שנקרא הפעלה קרה). כשמתרחש אתחול קר, ההקשר הגלובלי של הפונקציה נבדק.

אם הפונקציות מייבאות מודולים, זמן הטעינה של המודולים האלה יכול להוסיף לזמן האחזור של הקריאה במהלך הפעלה קרה. כדי לצמצם את זמן האחזור הזה ואת הזמן שנדרש לפריסת הפונקציה, צריך לטעון את התלויות בצורה נכונה ולא לטעון תלויות שהפונקציה לא משתמשת בהן.

שימוש במשתנים גלובליים כדי לעשות שימוש חוזר באובייקטים בהפעלות עתידיות

אין ערובה לכך שהמצב של פונקציית Cloud Run יישמר עבור הפעלות עתידיות. עם זאת, פונקציות Cloud Run ממחזרות לעיתים קרובות את סביבת ההפעלה של קריאה קודמת. אם מצהירים על משתנה בהיקף גלובלי, אפשר לעשות שימוש חוזר בערך שלו בהפעלות הבאות בלי לחשב אותו מחדש.

כך אפשר לשמור במטמון אובייקטים שיכול להיות שיקר ליצור מחדש בכל הפעלה של פונקציה. העברת אובייקטים כאלה מגוף הפונקציה להיקף גלובלי עשויה להוביל לשיפורים משמעותיים בביצועים. בדוגמה הבאה נוצר אובייקט כבד רק פעם אחת לכל מופע של פונקציה, והוא משותף לכל הקריאות לפונקציה שמגיעות למופע הנתון:

Node.js

const functions = require('@google-cloud/functions-framework');

// TODO(developer): Define your own computations
const {lightComputation, heavyComputation} = require('./computations');

// Global (instance-wide) scope
// This computation runs once (at instance cold-start)
const instanceVar = heavyComputation();

/**
 * HTTP function that declares a variable.
 *
 * @param {Object} req request context.
 * @param {Object} res response context.
 */
functions.http('scopeDemo', (req, res) => {
  // Per-function scope
  // This computation runs every time this function is called
  const functionVar = lightComputation();

  res.send(`Per instance: ${instanceVar}, per function: ${functionVar}`);
});

Python

import time

import functions_framework


# Placeholder
def heavy_computation():
    return time.time()


# Placeholder
def light_computation():
    return time.time()


# Global (instance-wide) scope
# This computation runs at instance cold-start
instance_var = heavy_computation()


@functions_framework.http
def scope_demo(request):
    """
    HTTP Cloud Function that declares a variable.
    Args:
        request (flask.Request): The request object.
        <http://flask.pocoo.org/docs/1.0/api/#flask.Request>
    Returns:
        The response text, or any set of values that can be turned into a
        Response object using `make_response`
        <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>.
    """

    # Per-function scope
    # This computation runs every time this function is called
    function_var = light_computation()
    return f"Instance: {instance_var}; function: {function_var}"

Go


// h is in the global (instance-wide) scope.
var h string

// init runs during package initialization. So, this will only run during an
// an instance's cold start.
func init() {
	h = heavyComputation()
	functions.HTTP("ScopeDemo", ScopeDemo)
}

// ScopeDemo is an example of using globally and locally
// scoped variables in a function.
func ScopeDemo(w http.ResponseWriter, r *http.Request) {
	l := lightComputation()
	fmt.Fprintf(w, "Global: %q, Local: %q", h, l)
}

Java


import com.google.cloud.functions.HttpFunction;
import com.google.cloud.functions.HttpRequest;
import com.google.cloud.functions.HttpResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;

public class Scopes implements HttpFunction {
  // Global (instance-wide) scope
  // This computation runs at instance cold-start.
  // Warning: Class variables used in functions code must be thread-safe.
  private static final int INSTANCE_VAR = heavyComputation();

  @Override
  public void service(HttpRequest request, HttpResponse response)
      throws IOException {
    // Per-function scope
    // This computation runs every time this function is called
    int functionVar = lightComputation();

    var writer = new PrintWriter(response.getWriter());
    writer.printf("Instance: %s; function: %s", INSTANCE_VAR, functionVar);
  }

  private static int lightComputation() {
    int[] numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    return Arrays.stream(numbers).sum();
  }

  private static int heavyComputation() {
    int[] numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    return Arrays.stream(numbers).reduce((t, x) -> t * x).getAsInt();
  }
}

C#‎

using Google.Cloud.Functions.Framework;
using Microsoft.AspNetCore.Http;
using System.Linq;
using System.Threading.Tasks;

namespace Scopes;

public class Function : IHttpFunction
{
    // Global (server-wide) scope.
    // This computation runs at server cold-start.
    // Warning: Class variables used in functions code must be thread-safe.
    private static readonly int GlobalVariable = HeavyComputation();

    // Note that one instance of this class (Function) is created per invocation,
    // so calling HeavyComputation in the constructor would not have the same
    // benefit.

    public async Task HandleAsync(HttpContext context)
    {
        // Per-function-invocation scope.
        // This computation runs every time this function is called.
        int functionVariable = LightComputation();

        await context.Response.WriteAsync(
            $"Global: {GlobalVariable}; function: {functionVariable}",
            context.RequestAborted);
    }

    private static int LightComputation()
    {
        int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        return numbers.Sum();
    }

    private static int HeavyComputation()
    {
        int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        return numbers.Aggregate((current, next) => current * next);
    }
}

Ruby

# Global (instance-wide) scope.
# This block runs on cold start, before any function is invoked.
#
# Note: It is usually best to run global initialization in an on_startup block
# instead at the top level of the Ruby file. This is because top-level code
# could be executed to verify the function during deployment, whereas an
# on_startup block is run only when an actual function instance is starting up.
FunctionsFramework.on_startup do
  instance_data = perform_heavy_computation

  # To pass data into function invocations, the best practice is to set a
  # key-value pair using the Ruby Function Framework's built-in "set_global"
  # method. Functions can call the "global" method to retrieve the data by key.
  # (You can also use Ruby global variables or "toplevel" local variables, but
  # they can make it difficult to isolate global data for testing.)
  set_global :my_instance_data, instance_data
end

FunctionsFramework.http "tips_scopes" do |_request|
  # Per-function scope.
  # This method is called every time this function is called.
  invocation_data = perform_light_computation

  # Retrieve the data computed by the on_startup block.
  instance_data = global :my_instance_data

  "instance: #{instance_data}; function: #{invocation_data}"
end

PHP


use Psr\Http\Message\ServerRequestInterface;

function scopeDemo(ServerRequestInterface $request): string
{
    // Heavy computations should be cached between invocations.
    // The PHP runtime does NOT preserve variables between invocations, so we
    // must write their values to a file or otherwise cache them.
    // (All writable directories in Cloud Functions are in-memory, so
    // file-based caching operations are typically fast.)
    // You can also use PSR-6 caching libraries for this task:
    // https://packagist.org/providers/psr/cache-implementation
    $cachePath = sys_get_temp_dir() . '/cached_value.txt';

    $response = '';
    if (file_exists($cachePath)) {
        // Read cached value from file, using file locking to prevent race
        // conditions between function executions.
        $response .= 'Reading cached value.' . PHP_EOL;
        $fh = fopen($cachePath, 'r');
        flock($fh, LOCK_EX);
        $instanceVar = stream_get_contents($fh);
        flock($fh, LOCK_UN);
    } else {
        // Compute cached value + write to file, using file locking to prevent
        // race conditions between function executions.
        $response .= 'Cache empty, computing value.' . PHP_EOL;
        $instanceVar = _heavyComputation();
        file_put_contents($cachePath, $instanceVar, LOCK_EX);
    }

    // Lighter computations can re-run on each function invocation.
    $functionVar = _lightComputation();

    $response .= 'Per instance: ' . $instanceVar . PHP_EOL;
    $response .= 'Per function: ' . $functionVar . PHP_EOL;

    return $response;
}

חשוב במיוחד לשמור במטמון חיבורים לרשת, הפניות לספריות ואובייקטים של לקוחות API בהיקף גלובלי. דוגמאות מפורטות במאמר בנושא שיטות מומלצות לניהול רשתות.

הפחתת הפעלות במצב התחלתי (cold start) על ידי הגדרת מספר מינימלי של מופעים

כברירת מחדל, פונקציות Cloud Run משנות את מספר המכונות בהתאם למספר הבקשות הנכנסות. אפשר לשנות את התנהגות ברירת המחדל הזו על ידי הגדרת מספר מינימלי של מופעים שפונקציות Cloud Run צריכות לשמור במצב מוכן כדי לטפל בבקשות. הגדרת מספר מינימלי של מופעים מצמצמת את ההפעלה האיטית במצב התחלתי של האפליקציה. אם האפליקציה רגישה לזמן האחזור, מומלץ להגדיר מספר מינימלי של מופעים ולהשלים את האתחול בזמן הטעינה.

במאמר שימוש במספר מינימלי של מכונות מוסבר איך להגדיר מספר מינימלי של מכונות.

הערות לגבי הפעלה במצב התחלתי (cold start) ואתחול

אתחול גלובלי מתבצע בזמן הטעינה. בלעדיו, הבקשה הראשונה תצטרך להשלים את האתחול ולטעון מודולים, ולכן זמן האחזור יהיה גבוה יותר.

עם זאת, לאתחול גלובלי יש גם השפעה על הפעלות במצב התחלתי (cold start). כדי לצמצם את ההשפעה הזו, כדאי לאתחל רק את מה שנדרש לבקשה הראשונה, כדי לשמור על זמן האחזור של הבקשה הראשונה נמוך ככל האפשר.

זה חשוב במיוחד אם הגדרתם את מספר המופעים המינימלי כפי שמתואר למעלה עבור פונקציה שרגישה לזמן האחזור. במקרה כזה, השלמת האתחול בזמן הטעינה ושמירת נתונים שימושיים במטמון מבטיחה שהבקשה הראשונה לא תצטרך לעשות זאת, והיא תטופל עם זמן אחזור נמוך.

אם מאתחלים משתנים בהיקף גלובלי, בהתאם לשפה, זמני אתחול ארוכים יכולים להוביל לשני סוגי התנהגויות: - בשילוב מסוים של שפות וספריות אסינכרוניות, פונקציית המסגרת יכולה לפעול באופן אסינכרוני ולחזור באופן מיידי, ולגרום לקוד להמשיך לפעול ברקע, מה שיכול לגרום לבעיות כמו חוסר אפשרות לגשת למעבד. כדי למנוע את הבעיה הזו, צריך לחסום את הפעלת המודול כמו שמתואר בהמשך. כך גם מוודאים שהבקשות לא יטופלו עד שהאתחול יסתיים. – מצד שני, אם ההפעלה היא סינכרונית, זמן ההפעלה הארוך יגרום להפעלות קרות ארוכות יותר, וזו יכולה להיות בעיה, במיוחד בפונקציות עם מקביליות נמוכה במהלך עליות פתאומיות בעומס.

דוגמה לחימום מראש של ספריית node.js אסינכרונית

‫Node.js עם Firestore היא דוגמה לספריית Node.js אסינכרונית. כדי להשתמש ב-min_instances, הקוד הבא משלים את הטעינה והאתחול בזמן הטעינה, וחוסם את טעינת המודול.

נעשה שימוש ב-TLA, כלומר נדרש ES6, באמצעות הסיומת .mjs לקוד node.js או הוספת type: module לקובץ package.json.

{
  "main": "main.js",
  "type": "module",
  "dependencies": {
    "@google-cloud/firestore": "^7.10.0",
    "@google-cloud/functions-framework": "^3.4.5"
  }
}

Node.js

import Firestore from '@google-cloud/firestore';
import * as functions from '@google-cloud/functions-framework';

const firestore = new Firestore({preferRest: true});

// Pre-warm firestore connection pool, and preload our global config
// document in cache. In order to ensure no other request comes in,
// block the module loading with a synchronous global request:
const config = await firestore.collection('collection').doc('config').get();

functions.http('fetch', (req, res) => {

// Do something with config and firestore client, which are now preloaded
// and will execute at lower latency.
});

דוגמאות לאתחול גלובלי

Node.js

const functions = require('@google-cloud/functions-framework');

// Always initialized (at cold-start)
const nonLazyGlobal = fileWideComputation();

// Declared at cold-start, but only initialized if/when the function executes
let lazyGlobal;

/**
 * HTTP function that uses lazy-initialized globals
 *
 * @param {Object} req request context.
 * @param {Object} res response context.
 */
functions.http('lazyGlobals', (req, res) => {
  // This value is initialized only if (and when) the function is called
  lazyGlobal = lazyGlobal || functionSpecificComputation();

  res.send(`Lazy global: ${lazyGlobal}, non-lazy global: ${nonLazyGlobal}`);
});

Python

import functions_framework

# Always initialized (at cold-start)
non_lazy_global = file_wide_computation()

# Declared at cold-start, but only initialized if/when the function executes
lazy_global = None


@functions_framework.http
def lazy_globals(request):
    """
    HTTP Cloud Function that uses lazily-initialized globals.
    Args:
        request (flask.Request): The request object.
        <http://flask.pocoo.org/docs/1.0/api/#flask.Request>
    Returns:
        The response text, or any set of values that can be turned into a
        Response object using `make_response`
        <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>.
    """
    global lazy_global, non_lazy_global  # noqa: F824

    # This value is initialized only if (and when) the function is called
    if not lazy_global:
        lazy_global = function_specific_computation()

    return f"Lazy: {lazy_global}, non-lazy: {non_lazy_global}."

Go


// Package tips contains tips for writing Cloud Functions in Go.
package tips

import (
	"context"
	"log"
	"net/http"
	"sync"

	"cloud.google.com/go/storage"
	"github.com/GoogleCloudPlatform/functions-framework-go/functions"
)

// client is lazily initialized by LazyGlobal.
var client *storage.Client
var clientOnce sync.Once

func init() {
	functions.HTTP("LazyGlobal", LazyGlobal)
}

// LazyGlobal is an example of lazily initializing a Google Cloud Storage client.
func LazyGlobal(w http.ResponseWriter, r *http.Request) {
	// You may wish to add different checks to see if the client is needed for
	// this request.
	clientOnce.Do(func() {
		// Pre-declare an err variable to avoid shadowing client.
		var err error
		client, err = storage.NewClient(context.Background())
		if err != nil {
			http.Error(w, "Internal error", http.StatusInternalServerError)
			log.Printf("storage.NewClient: %v", err)
			return
		}
	})
	// Use client.
}

Java


import com.google.cloud.functions.HttpFunction;
import com.google.cloud.functions.HttpRequest;
import com.google.cloud.functions.HttpResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;

public class LazyFields implements HttpFunction {
  // Always initialized (at cold-start)
  // Warning: Class variables used in Servlet classes must be thread-safe,
  // or else might introduce race conditions in your code.
  private static final int NON_LAZY_GLOBAL = fileWideComputation();

  // Declared at cold-start, but only initialized if/when the function executes
  // Uses the "initialization-on-demand holder" idiom
  // More information: https://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom
  private static class LazyGlobalHolder {
    // Making the default constructor private prohibits instantiation of this class
    private LazyGlobalHolder() {}

    // This value is initialized only if (and when) the getLazyGlobal() function below is called
    private static final Integer INSTANCE = functionSpecificComputation();

    private static Integer getInstance() {
      return LazyGlobalHolder.INSTANCE;
    }
  }

  @Override
  public void service(HttpRequest request, HttpResponse response)
      throws IOException {
    Integer lazyGlobal = LazyGlobalHolder.getInstance();

    var writer = new PrintWriter(response.getWriter());
    writer.printf("Lazy global: %s; non-lazy global: %s%n", lazyGlobal, NON_LAZY_GLOBAL);
  }

  private static int functionSpecificComputation() {
    int[] numbers = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
    return Arrays.stream(numbers).sum();
  }

  private static int fileWideComputation() {
    int[] numbers = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
    return Arrays.stream(numbers).reduce((t, x) -> t * x).getAsInt();
  }
}

C#‎

using Google.Cloud.Functions.Framework;
using Microsoft.AspNetCore.Http;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace LazyFields;

public class Function : IHttpFunction
{
    // This computation runs at server cold-start.
    // Warning: Class variables used in functions code must be thread-safe.
    private static readonly int NonLazyGlobal = FileWideComputation();

    // This variable is initialized at server cold-start, but the
    // computation is only performed when the function needs the result.
    private static readonly Lazy<int> LazyGlobal = new Lazy<int>(
        FunctionSpecificComputation,
        LazyThreadSafetyMode.ExecutionAndPublication);

    public async Task HandleAsync(HttpContext context)
    {
        // In a more complex function, there might be some paths that use LazyGlobal.Value,
        // and others that don't. The computation is only performed when necessary, and
        // only once per server.
        await context.Response.WriteAsync(
            $"Lazy global: {LazyGlobal.Value}; non-lazy global: {NonLazyGlobal}",
            context.RequestAborted);
    }

    private static int FunctionSpecificComputation()
    {
        int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        return numbers.Sum();
    }

    private static int FileWideComputation()
    {
        int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        return numbers.Aggregate((current, next) => current * next);
    }
}

Ruby

FunctionsFramework.on_startup do
  # This method is called when the function is initialized, not on each
  # invocation.

  # Declare and set non_lazy_global
  set_global :non_lazy_global, file_wide_computation

  # Declare, but do not set, lazy_global
  set_global :lazy_global do
    function_specific_computation
  end
end

FunctionsFramework.http "tips_lazy" do |_request|
  # This method is called every time this function is called.

  "Lazy: #{global :lazy_global}; non_lazy: #{global :non_lazy_global}"
end

PHP

פונקציות PHP לא יכולות לשמור משתנים בין בקשות. בדוגמה שלמעלה לגבי היקפים נעשה שימוש בטעינה מדורגת כדי לשמור במטמון ערכים של משתנים גלובליים בקובץ.

זה חשוב במיוחד אם מגדירים כמה פונקציות בקובץ אחד, ופונקציות שונות משתמשות במשתנים שונים. אם לא משתמשים באתחול עצלן, יכול להיות שתבזבזו משאבים על משתנים שאותחלו אבל אף פעם לא נעשה בהם שימוש.

מקורות מידע נוספים

מידע נוסף על אופטימיזציה של הביצועים זמין בסרטון 'Google Cloud Performance Atlas' בנושא זמן אתחול קר של פונקציות Cloud Run.