Suggerimenti utili

Questo documento descrive le best practice per la progettazione, l'implementazione, i test e il deployment di Cloud Functions.

Correttezza

Questa sezione descrive le best practice generali per la progettazione e l'implementazione di Cloud Functions.

Scrivere funzioni idempotenti

Le funzioni dovrebbero produrre lo stesso risultato anche se vengono chiamate più volte. In questo modo, puoi riprovare una chiamata se quella precedente non riesce a metà del tuo codice. Per maggiori informazioni, consulta la sezione su come ritentare le funzioni basate su eventi.

Assicurati che le funzioni HTTP inviino una risposta HTTP

Se la funzione è attivata da HTTP, ricordati di inviare una risposta HTTP, come mostrato di seguito. Se non lo fai, la funzione potrebbe essere eseguita fino al timeout. In questo caso, ti verrà addebitato l'intero tempo di timeout. I timeout possono anche causare comportamenti imprevedibili o avvii a freddo per le chiamate successive, causando comportamenti imprevedibili o latenza aggiuntiva.

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}!");
    }
}

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));
}

Non avviare attività in background

Per attività in background si intende tutto ciò che accade dopo l'interruzione della funzione. La chiamata di una funzione termina una volta che la funzione restituisce o segnala in altro modo il completamento, ad esempio chiamando l'argomento callback nelle funzioni basate su eventi Node.js. Qualsiasi codice eseguito dopo la terminazione controllata non può accedere alla CPU e non farà progressi.

Inoltre, quando nello stesso ambiente viene eseguita una chiamata successiva, l'attività in background riprende, interferendo con la nuova chiamata. Ciò può portare a comportamenti imprevisti ed errori difficili da diagnosticare. L'accesso alla rete dopo la terminazione di una funzione porta in genere alla reimpostazione delle connessioni (codice di errore ECONNRESET).

Spesso l'attività in background può essere rilevata nei log dalle singole chiamate, trovando tutto ciò che viene registrato dopo la riga che indica che la chiamata è terminata. L'attività in background a volte può essere nascosta più a fondo nel codice, soprattutto quando sono presenti operazioni asincrone come callback o timer. Rivedi il codice per assicurarti che tutte le operazioni asincrone vengano completate prima di terminare la funzione.

Elimina sempre i file temporanei

Lo spazio di archiviazione del disco locale nella directory temporanea è un file system in memoria. I file che scrivi consumano memoria disponibile per la funzione e talvolta rimangono tra le chiamate. Se non elimini in modo esplicito questi file, potrebbe verificarsi un errore di memoria insufficiente e un successivo avvio a freddo.

Puoi visualizzare la memoria utilizzata da una singola funzione selezionandola nell'elenco di funzioni nella console Google Cloud e scegliendo il grafico Memoria utilizzata.

Non tentare di scrivere al di fuori della directory temporanea e assicurati di utilizzare metodi indipendenti dalla piattaforma/dal sistema operativo per creare percorsi dei file.

Puoi ridurre i requisiti di memoria quando elabori file di dimensioni maggiori utilizzando la pipeline. Ad esempio, puoi elaborare un file su Cloud Storage creando un flusso di lettura, facendolo passare attraverso un processo basato su flusso e scrivendo il flusso di output direttamente in Cloud Storage.

Framework di Functions

Quando esegui il deployment di una funzione, il framework di Functions viene aggiunto automaticamente come dipendenza, utilizzando la versione attuale. Per assicurarti che le stesse dipendenze siano installate in modo coerente in diversi ambienti, ti consigliamo di bloccare la funzione a una versione specifica del framework di Functions.

Per farlo, includi la tua versione preferita nel file di blocco pertinente (ad esempio, package-lock.json per Node.js o requirements.txt per Python).

Strumenti

Questa sezione fornisce linee guida su come utilizzare gli strumenti per implementare, testare e interagire con Cloud Functions.

Sviluppo locale

Il deployment delle funzioni richiede un po' di tempo, quindi spesso è più rapido testare il codice della funzione localmente.

Error Reporting

Nei linguaggi che utilizzano la gestione delle eccezioni, non generare eccezioni non rilevate, perché forzano gli avvii a freddo nelle chiamate future. Per informazioni su come segnalare correttamente gli errori, consulta la guida Error Reporting.

Non uscire manualmente

L'uscita manuale può causare comportamenti imprevisti. Usa invece le seguenti espressioni idiomatiche specifiche della lingua:

Node.js

Non usare process.exit(). Le funzioni HTTP devono inviare una risposta con res.status(200).send(message) e le funzioni basate su eventi usciranno una volta restituite (implicitamente o esplicitamente).

Python

Non usare sys.exit(). Le funzioni HTTP dovrebbero restituire esplicitamente una risposta sotto forma di stringa, mentre le funzioni basate su eventi escono dall'uscita una volta restituito un valore (implicitamente o esplicitamente).

Go

Non usare os.Exit(). Le funzioni HTTP dovrebbero restituire esplicitamente una risposta sotto forma di stringa, mentre le funzioni basate su eventi escono dall'uscita una volta restituito un valore (implicitamente o esplicitamente).

Java

Non usare System.exit(). Le funzioni HTTP devono inviare una risposta con response.getWriter().write(message) e le funzioni basate su eventi usciranno una volta restituite (implicitamente o esplicitamente).

C#

Non usare System.Environment.Exit(). Le funzioni HTTP devono inviare una risposta con context.Response.WriteAsync(message) e le funzioni basate su eventi usciranno una volta restituite (implicitamente o esplicitamente).

Ruby

Non utilizzare exit() o abort(). Le funzioni HTTP dovrebbero restituire esplicitamente una risposta sotto forma di stringa, mentre le funzioni basate su eventi escono dall'uscita una volta restituito un valore (implicitamente o esplicitamente).

PHP

Non utilizzare exit() o die(). Le funzioni HTTP dovrebbero restituire esplicitamente una risposta sotto forma di stringa, mentre le funzioni basate su eventi escono dall'uscita una volta restituito un valore (implicitamente o esplicitamente).

Utilizza Sendgrid per inviare email

Cloud Functions non consente connessioni in uscita sulla porta 25, quindi non è possibile stabilire connessioni non sicure a un server SMTP. Il modo consigliato per inviare email è utilizzare SendGrid. Puoi trovare altre opzioni per l'invio di email nel tutorial Invio di email da un'istanza per Google Compute Engine.

Prestazioni

Questa sezione descrive le best practice per l'ottimizzazione del rendimento.

Usa le dipendenze con oculatezza

Poiché le funzioni sono stateless, l'ambiente di esecuzione viene spesso inizializzato da zero (durante il cosiddetto avvio a freddo). Quando si verifica un avvio a freddo, viene valutato il contesto globale della funzione.

Se le funzioni importano moduli, il tempo di caricamento per questi moduli può aumentare la latenza delle chiamate durante un avvio a freddo. Puoi ridurre questa latenza, nonché il tempo necessario per il deployment della funzione, caricando correttamente le dipendenze e non caricando dipendenze che la funzione non utilizza.

Utilizza le variabili globali per riutilizzare gli oggetti nelle chiamate future

Non vi è alcuna garanzia che lo stato di una Cloud Function verrà conservato per le chiamate future. Tuttavia, Cloud Functions spesso ricicla l'ambiente di esecuzione di una chiamata precedente. Se dichiari una variabile in ambito globale, il suo valore può essere riutilizzato nelle chiamate successive senza dover essere ricalcolato.

In questo modo puoi memorizzare nella cache oggetti che possono essere costosi da ricreare a ogni chiamata di funzione. Lo spostamento di questi oggetti dal corpo della funzione all'ambito globale può portare a miglioramenti significativi delle prestazioni. L'esempio seguente crea un oggetto pesante solo una volta per istanza di funzione e lo condivide in tutte le chiamate di funzione che raggiungono l'istanza specificata:

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}");
    }

    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;
}

È particolarmente importante memorizzare nella cache le connessioni di rete, i riferimenti alle librerie e gli oggetti client API in ambito globale. Per alcuni esempi, consulta Ottimizzazione del networking.

Inizializzazione lazy delle variabili globali

Se inizializza le variabili in ambito globale, il codice di inizializzazione verrà sempre eseguito tramite una chiamata di avvio a freddo, aumentando la latenza della funzione. In alcuni casi, ciò causa timeout intermittenti per la chiamata dei servizi se non vengono gestiti correttamente in un blocco try/catch. Se alcuni oggetti non vengono utilizzati in tutti i percorsi di codice, valuta la possibilità di inizializzarli in modo lento on demand:

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

    # 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}");
    }

    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

Le funzioni PHP non possono conservare variabili tra le richieste. L'esempio degli ambiti riportato sopra utilizza il caricamento lento per memorizzare nella cache i valori delle variabili globali in un file.

Ciò è particolarmente importante se definisci varie funzioni in un singolo file e funzioni diverse usano variabili diverse. A meno che non utilizzi l'inizializzazione lazy, potresti sprecare risorse su variabili che sono state inizializzate ma mai utilizzate.

Riduci gli avvii a freddo impostando un numero minimo di istanze

Per impostazione predefinita, Cloud Functions scala il numero di istanze in base al numero di richieste in entrata. Puoi modificare questo comportamento predefinito impostando un numero minimo di istanze che Cloud Functions deve tenere pronte per gestire le richieste. L'impostazione di un numero minimo di istanze riduce gli avvii a freddo dell'applicazione. Ti consigliamo di impostare un numero minimo di istanze se la tua applicazione è sensibile alla latenza.

Per informazioni su come impostare un numero minimo di istanze, consulta Utilizzo del numero minimo di istanze.

Risorse aggiuntive

Scopri di più sull'ottimizzazione delle prestazioni nel video "Google Cloud Performance Atlas" Tempo di avvio a freddo di Cloud Functions.