도움말 및 유용한 정보

이 문서에서는 Cloud Functions의 설계, 구현, 테스트, 배포를 위한 권장사항을 설명합니다.

정확성

이 섹션에서는 Cloud Functions의 디자인과 구현을 위한 일반적인 권장사항을 설명합니다.

멱등 함수 작성

함수를 여러 번 호출해도 함수에서 동일한 결과가 나와야 합니다. 이렇게 하면 코드 중간에 이전 호출이 실패할 경우 호출을 재시도할 수 있습니다. 자세한 내용은 이벤트 기반 함수 재시도를 참조하세요.

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

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

백그라운드 활동 시작 금지

백그라운드 활동이란 함수가 종료된 뒤에 발생하는 모든 활동입니다. Node.js 이벤트 기반 함수에서 callback 인수를 호출하는 등 함수 반환 또는 신호 완료가 발생하면 함수 호출이 종료됩니다. 단계적 종료 이후에 실행되는 모든 코드는 CPU를 사용할 수 없으므로 진행되지 않습니다.

또한 동일한 환경에서 후속 호출을 실행하면 백그라운드 활동이 다시 시작되어 새 호출을 방해합니다. 이로 인해 예상치 못한 동작 및 진단하기 어려운 오류가 발생할 수 있습니다. 일반적으로 함수가 종료된 후에 네트워크에 액세스하면 연결이 재설정됩니다(ECONNRESET 오류 코드).

개별 호출의 로그에서 호출이 끝났음을 알리는 줄 다음에 로깅된 내용을 찾으면 백그라운드 활동이 종종 감지될 수 있습니다. 특히 콜백 또는 타이머와 같은 비동기 작업이 있으면 백그라운드 활동은 코드에 더 깊이 숨어 있을 수 있습니다. 함수를 종료하기 전에 모든 비동기식 작업이 완료되도록 코드를 검토합니다.

항상 임시 파일 삭제

임시 디렉터리의 로컬 디스크 저장소는 메모리 내의 파일 시스템입니다. 작성한 파일은 함수에 제공되는 메모리를 사용하며 가끔 호출 시 그대로 유지됩니다. 이 파일을 명시적으로 삭제하지 못하면 결국 메모리 부족 오류가 발생한 후 콜드 스타트가 진행될 수 있습니다.

Google Cloud 콘솔의 함수 목록에서 개별 함수를 선택한 후 메모리 사용량 플롯을 선택하면 이 함수에서 사용한 메모리를 확인할 수 있습니다.

임시 디렉터리 외부에 작성하려고 시도해서는 안 되며 플랫폼 또는 OS 독립 메서드를 사용하여 파일 경로를 구성해야 합니다.

파이프라인을 사용하여 큰 파일을 처리하면 메모리 요구사항을 줄일 수 있습니다. 예를 들어 읽기 스트림을 만들고 스트림 기반 프로세스를 통해 이를 전달한 후 Cloud Storage에 출력 스트림을 직접 작성하는 방식으로 Cloud Storage에서 파일을 처리할 수 있습니다.

함수 프레임워크

함수를 배포하면 함수 프레임워크가 현재 버전을 사용하여 종속 항목으로 자동 추가됩니다. 동일한 종속 항목이 다양한 환경에 일관되게 설치되도록 하려면 함수를 특정 버전의 함수 프레임워크로 고정하는 것이 좋습니다.

이렇게 하려면 관련 잠금 파일에 원하는 버전을 포함합니다(예: Node.js의 경우 package-lock.json, Python의 경우 requirements.txt).

도구

이 섹션에서는 도구를 사용하여 Cloud Functions를 구현 및 테스트하고 활용하는 방법을 설명합니다.

로컬 개발

함수 배포에는 시간이 다소 걸리므로 로컬에서 함수 코드를 테스트하는 것이 더 빠를 수 있습니다.

오류 보고

예외 처리를 사용하는 언어의 경우 확인되지 않은 예외는 향후 호출 시 콜드 스타트를 강제로 시작하므로, 이러한 예외를 발생시키지 마세요. 오류를 올바르게 보고하는 방법에 대해서는 Error reporting 가이드를 참조하세요.

수동으로 종료 안 함

수동으로 종료하면 예기치 않은 동작이 발생할 수 있습니다. 대신에 다음 언어별 관용구를 사용하세요.

Node.js

process.exit()를 사용하지 마세요. HTTP 함수는 res.status(200).send(message)를 사용하여 응답을 보내야 합니다. 응답이 반환되면(암시적 또는 명시적으로) 이벤트 기반 함수가 종료됩니다.

Python

sys.exit()를 사용하지 마세요. HTTP 함수는 응답을 문자열로 명시적으로 반환해야 하며, 값이 반환되면(암시적 또는 명시적으로) 이벤트 기반 함수가 종료됩니다.

Go

os.Exit()를 사용하지 마세요. HTTP 함수는 응답을 문자열로 명시적으로 반환해야 하며, 값이 반환되면(암시적 또는 명시적으로) 이벤트 기반 함수가 종료됩니다.

자바

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 Functions는 25번 포트의 발신 연결을 허용하지 않으므로 SMTP 서버에 비보안 방식으로 연결할 수 없습니다. 이메일 전송에 권장되는 방법은 SendGrid를 사용하는 것입니다. 기타 이메일 전송 관련 옵션은 Google Compute Engine의 인스턴스에서 이메일 전송 튜토리얼을 참조하세요.

성능

이 섹션에서는 성능 최적화를 위한 권장사항을 설명합니다.

현명하게 종속 항목 사용

함수는 스테이트리스(Stateless) 방식이므로 실행 환경이 종종 처음부터 초기화되는 경우가 있는데 이러한 과정을 콜드 스타트라고 합니다. 콜드 스타트가 발생하면 함수의 전역 컨텍스트가 평가됩니다.

함수가 모듈을 가져오면 완전 시작 중 해당 모듈의 로드 시간이 호출 지연 시간에 추가될 수 있습니다. 함수가 사용하지 않는 종속 항목을 로드하지 않고 올바른 종속 항목을 로드하면 함수를 배포하는 데 필요한 시간은 물론 지연 시간도 줄일 수 있습니다.

전역 변수를 사용하여 이후 호출에서 객체 재사용

다음 호출에 대비해 Cloud 함수의 상태가 유지된다는 보장은 없습니다. 하지만 Cloud Functions는 종종 이전 호출의 실행 환경을 재활용합니다. 전역 범위에서 변수를 선언하면 다시 연산할 필요 없이 후속 호출에서 변수 값을 재사용합니다.

이 방식을 사용하면 함수를 호출할 때마다 다시 만드는 데 비용이 많이 들 수 있는 객체를 캐싱할 수 있습니다. 이러한 객체를 함수 본문에서 전역 범위로 이동하면 성능이 매우 크게 향상될 수 있습니다. 다음 예에서는 리소스 사용량이 많은 객체를 함수 인스턴스당 한 번만 만든 후 특정 인스턴스에 도달하는 모든 함수 호출에서 공유합니다.

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

전역 범위에서 네트워크 연결, 라이브러리 참조, API 클라이언트 객체를 캐싱하는 것이 특히 중요합니다. 예시는 네트워킹 최적화를 참조하세요.

전역 변수의 지연 초기화 실행

전역 범위에서 변수를 초기화하는 경우 초기화 코드가 항상 콜드 스타트 호출을 통해 실행되므로 함수의 지연 시간이 증가합니다. 경우에 따라 try/catch 블록에서 서비스가 적절하게 처리되지 않으면 호출되는 서비스에 간헐적인 시간 초과가 발생합니다. 모든 코드 경로에서 사용되지는 않는 객체가 있다면 수요에 맞춰 초기화를 지연하는 것이 좋습니다.

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

PHP 함수는 요청 간에 변수를 보존할 수 없습니다. 위의 범위 샘플은 지연 로드를 사용하여 파일의 전역 변수 값을 캐시합니다.

한 파일에서 여러 함수를 정의하고 여러 함수가 다른 변수를 사용하는 경우에 특히 중요합니다. 지연 초기화를 사용하지 않는다면 초기화만 하고 절대 사용하지 않는 변수에서 리소스를 낭비할 수 있습니다.

최소 인스턴스 수를 설정하여 콜드 스타트 줄이기

기본적으로 Cloud Functions는 수신 요청 수를 기준으로 인스턴스 수를 확장합니다. Cloud Functions 요청 처리를 위해 준비할 최소 인스턴스 수를 지정하여 이러한 기본 동작을 변경할 수 있습니다. 최소 인스턴스 수를 설정하면 애플리케이션의 콜드 스타트가 줄어듭니다. 애플리케이션이 지연 시간에 민감하다면 최소 인스턴스 수를 설정하는 것이 좋습니다.

최소 인스턴스 수를 설정하는 방법은 최소 인스턴스 사용을 참조하세요.

추가 리소스

'Google Cloud Performance Atlas' 동영상 Cloud Functions 콜드 부팅 시간을 시청하여 성능 최적화에 대해 자세히 알아보세요.