Sugerencias

En este documento, se describen las prácticas recomendadas para diseñar, implementar y probar Cloud Functions.

Precisión

En esta sección, se describen las prácticas recomendadas generales para diseñar e implementar Cloud Functions.

Escribir funciones idempotentes

Tus funciones deben producir el mismo resultado, incluso si se llaman varias veces. Esto te permite volver a intentar una invocación si la anterior falla a mitad de camino en el código. Para obtener más información, consulta Reintenta las funciones controladas por eventos.

Asegúrate de que las funciones de HTTP envíen una respuesta HTTP

Si tu función se activa con HTTP, recuerda que debes enviar una respuesta HTTP como se muestra más abajo. No hacerlo puede dar como resultado que la función se ejecute hasta el tiempo de espera. Si esto ocurre, se te cobrará por todo el tiempo de espera. Los tiempos de espera también pueden generar un comportamiento impredecible o inicios en frío en invocaciones posteriores, lo que dará como resultado un comportamiento impredecible o una latencia adicional.

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

No inicies actividades en segundo plano

Las actividades en segundo plano son todas las acciones que ocurren después de que finaliza la ejecución de tu función. La invocación de una función termina cuando esta muestra o indica que se completó su ejecución, por ejemplo, si llama al argumento callback en las funciones controlada por eventos. El código que se ejecute después de esto no podrá acceder a la CPU y no llevará a cabo ningún progreso.

Además, cuando se ejecutan las invocaciones siguientes en el mismo entorno, se reanuda tu actividad en segundo plano, lo que provoca interferencias en la invocación nueva. Es posible que se generen errores y comportamientos inesperados difíciles de diagnosticar. Acceder a una red después de que una función termina suele generar el restablecimiento de las conexiones (código de error ECONNRESET).

A menudo, la actividad en segundo plano se puede detectar en los registros de las invocaciones individuales, ya que aparece en las líneas posteriores a la que indica que la invocación finalizó. En otras ocasiones, este tipo de actividad puede estar mucho más oculta en el código, especialmente cuando existen operaciones asíncronas, como devoluciones de llamadas o cronómetros. Revisa tu código para asegurarte de que todas las operaciones asíncronas finalicen antes de que termines la función.

Borra los archivos temporales siempre

El almacenamiento en el directorio temporal del disco local es un sistema de archivos en la memoria. Los archivos que escribes consumen memoria disponible en tu función y, a veces, persisten entre invocaciones. No borrar estos archivos explícitamente podría generar un error por falta de memoria y un posterior inicio en frío.

Para ver la memoria que usa una función específica, selecciónala en la lista de funciones de la consola de Google Cloud y elige el trazado Uso de memoria.

No intentes escribir fuera del directorio temporal y asegúrate de usar métodos independientes de SO/plataforma para construir rutas de acceso.

Puedes reducir los requisitos de memoria si procesas archivos más grandes mediante canalizaciones. Por ejemplo, para procesar un archivo alojado en Cloud Storage, puedes crear un flujo de lectura, pasarlo por un proceso basado en flujos y escribir el flujo de salida directamente en Cloud Storage.

Functions Framework

Cuando implementas una función, Functions Framework se agrega automáticamente como una dependencia con su versión actual. Para garantizar que las mismas dependencias se instalen de manera coherente en diferentes entornos, te recomendamos que fijes la función a una versión específica de Functions Framework.

Para ello, incluye la versión que prefieras en el archivo de bloqueo pertinente (por ejemplo, package-lock.json para Node.js o requirements.txt para Python).

Herramientas

En esta sección, se proporcionan lineamientos sobre cómo usar herramientas para implementar, probar y también interactuar con Cloud Functions.

Desarrollo local

La implementación de funciones toma algo de tiempo, por lo que suele ser más rápido probar el código de tu función de manera local.

Informes de errores

En lenguajes que usan el manejo de excepciones, no lances excepciones no detectadas, ya que fuerzan los inicios en frío en las invocaciones futuras. Consulta la guía de registro de errores para obtener información sobre cómo informar errores de forma correcta.

No salgas de forma manual

El cierre manual puede provocar un comportamiento inesperado. En su lugar, usa las siguientes expresiones específicas del lenguaje:

Node.js

No uses process.exit(). Las funciones de HTTP deben enviar una respuesta con res.status(200).send(message) y las funciones controladas por eventos se cerrarán una vez que muestren una respuesta (de forma implícita o explícita).

Python

No uses sys.exit(). Las funciones de HTTP deben mostrar de forma explícita una respuesta como una string, y las funciones controladas por eventos saldrán una vez que muestren un valor (ya sea de forma implícita o explícita).

Comienza a usarlo

No uses os.Exit(). Las funciones de HTTP deben mostrar de forma explícita una respuesta como una string, y las funciones controladas por eventos saldrán una vez que muestren un valor (ya sea de forma implícita o explícita).

Java

No uses System.exit(). Las funciones de HTTP deben enviar una respuesta con response.getWriter().write(message) y las funciones controladas por eventos se cerrarán una vez que muestren una respuesta (de forma implícita o explícita).

C#

No uses System.Environment.Exit(). Las funciones de HTTP deben enviar una respuesta con context.Response.WriteAsync(message) y las funciones controladas por eventos se cerrarán una vez que muestren una respuesta (de forma implícita o explícita).

Ruby

No uses exit() ni abort(). Las funciones de HTTP deben mostrar de forma explícita una respuesta como una string, y las funciones controladas por eventos saldrán una vez que muestren un valor (ya sea de forma implícita o explícita).

PHP

No uses exit() ni die(). Las funciones de HTTP deben mostrar de forma explícita una respuesta como una string, y las funciones controladas por eventos saldrán una vez que muestren un valor (ya sea de forma implícita o explícita).

Usa Sendgrid para enviar correos electrónicos

Cloud Functions no permite conexiones salientes en el puerto 25, por lo que no puedes establecer conexiones no seguras a un servidor SMTP. Para enviar correos electrónicos, se recomienda usar SendGrid. Puedes encontrar otras opciones para enviar correos electrónicos en el instructivo sobre cómo enviar correos electrónicos desde una instancia para Google Compute Engine.

Rendimiento

En esta sección, se describen las prácticas recomendadas para optimizar el rendimiento.

Usa las dependencias de manera inteligente

Debido a que las funciones no tienen estado, el entorno de ejecución suele inicializarse desde cero (durante lo que se conoce como inicio en frío). Cuando se genera un inicio en frío, se evalúa el contexto global de la función.

Si tus funciones importan módulos, el tiempo de carga de estos se agrega a la latencia de invocación durante un inicio en frío. Para reducir esta latencia y el tiempo que se requiere para implementar tu función, carga las dependencias correctamente y no cargues las dependencias que tu función no utiliza.

Usa variables globales para volver a usar objetos en invocaciones futuras

No se garantiza que el estado de una función de Cloud Functions se conserve para las invocaciones futuras. Sin embargo, Cloud Functions suele reciclar el entorno de ejecución de una invocación previa. Si declaras una variable en alcance global, su valor se puede volver a usar en invocaciones posteriores sin tener que volver a procesarse.

De esta manera, puedes almacenar objetos en la caché, ya que volver a crearlos en cada invocación de la función puede ser costoso. Mover estos objetos desde el cuerpo de la función al alcance global puede generar importantes mejoras en el rendimiento. El siguiente ejemplo crea un objeto pesado solo una vez por instancia de la función y lo comparte en todas las invocaciones de la función que alcanzan la instancia determinada:

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

Es muy importante almacenar en caché las conexiones de red, las referencias de la biblioteca y los objetos de cliente de la API en alcance global. Consulta Optimiza herramientas de redes para ver ejemplos.

Haz una inicialización diferida de las variables globales

Si inicializas variables en alcance global, el código de inicialización se ejecutará siempre a través de una invocación de inicio en frío, lo que aumenta la latencia de tu función. En algunos casos, esto genera tiempos de espera intermitentes en los servicios a los que se llama si no se administran de forma apropiada en un bloque try/catch. Si algunos objetos no se usan en todas las rutas del código, te recomendamos inicializarlos de forma diferida según demanda:

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

Las funciones de PHP no pueden conservar variables entre solicitudes. En el ejemplo de alcance anterior, se usa la carga diferida para almacenar en caché valores de variables globales en un archivo.

Esto es muy importante si defines varias funciones en un solo archivo y si varias funciones usan variables diferentes. A menos que uses la inicialización diferida, puedes perder recursos si inicializas variables que nunca se usan.

Configura una cantidad mínima de instancias para reducir los inicios en frío

De forma predeterminada, Cloud Functions escala la cantidad de instancias según la cantidad de solicitudes entrantes. Para cambiar este comportamiento predeterminado, configura una cantidad mínima de instancias que Cloud Functions debe tener listas para entregar solicitudes. Establecer una cantidad mínima de instancias reduce los inicios en frío de tu aplicación. Recomendamos que configures una cantidad mínima de instancias si tu aplicación es sensible a la latencia.

Para aprender a establecer una cantidad mínima de instancias, consulta Usa la cantidad mínima de instancias.

Recursos adicionales

Obtén más información para optimizar el rendimiento en el video de “Google Cloud Performance Atlas”, Cloud Functions Cold Boot Time.