All Products
Search
Document Center

Function Compute:Context

Last Updated:Aug 08, 2024

This topic describes the context for using a Go runtime in Function Compute to write code and provides sample code of the context.

What is a context?

When Function Compute executes a function, Function Compute passes the context object context.Context to the handler that you use to execute the function. The context contains information about invocations, services, functions, tracing analysis, and runtime environments.

The following table describes the parameters that are contained in the context.

Table 1. Context

Parameter

Description

Variables

RequestID

The unique ID of the request that is used to invoke the function. You can record the ID for troubleshooting if an error occurs.

Credentials

The temporary AccessKey pair that Function Compute obtains by assuming your service-linked role. The temporary AccessKey pair is valid for 36 hours. You can use credentials in your code to access related services such as Object Storage Service (OSS). This way, you can access the services without the need to write your AccessKey pair in the function code. For more information, see Grant Function Compute permissions to access other Alibaba Cloud services.

Function

The basic information about the invoked function, such as the name, handler, memory, and timeout period of the function.

Service

The information about the service to which the function belongs, such as the name, the related project, and Logstore in Simple Log Service, the version, and the alias of the service. The qualifier parameter indicates the version or alias of the service that is specified when you invoke a function. The versionId parameter indicates the version of the service that is actually invoked.

Region

The ID of the region in which the function is invoked. For example, if the function is invoked in the China (Shanghai) region, the region ID is cn-shanghai. For more information, see Service endpoints.

AccountId

The ID of the Alibaba Cloud account to which the function belongs.

Method

deadline

The timeout period of function execution. The value is a UNIX timestamp. Unit: millisecond.

For more information about the complete data structure, see fc-runtime-go-sdk on GitHub.

Sample code

Sample code for displaying the context information

Add the context parameter to the handler of a function. Function Compute passes the variables that are described in the preceding Context table to the context parameter. Then, import the aliyun/fc-runtime-go-sdk/fccontext package and call the fccontext.FromContext method to obtain fccontext.

package main

import (
    "context"
    "encoding/json"
    "log"

    "github.com/aliyun/fc-runtime-go-sdk/fc"
    "github.com/aliyun/fc-runtime-go-sdk/fccontext"
)

func main() {
    fc.Start(echoContext)
}

func echoContext(ctx context.Context) (string, error) {
    fctx, _ := fccontext.FromContext(ctx)
    log.Println(fctx.AccountId)
    log.Printf("%#v\n", fctx)
    res, _ := json.Marshal(fctx)
    return string(res), nil
}

Sample code for obtaining the remaining execution duration of a function

The following sample code provides an example on how to use the deadline parameter to obtain the remaining execution duration of a function:

package main

import (
    "context"
    "fmt"
    "log"
    "time"

    "github.com/aliyun/fc-runtime-go-sdk/fc"
)

func LongRunningHandler(ctx context.Context) (string, error) {
    deadline, _ := ctx.Deadline()
    fmt.Printf("now: %s\ndeadline: %s\n", time.Now().String(), deadline.String())
    deadline = deadline.Add(-100 * time.Millisecond)
    timeoutChannel := time.After(time.Until(deadline))

    for {
        select {
        case <-timeoutChannel:
            return "Finished before timing out.", nil
        default:
            log.Print("hello!")
            time.Sleep(50 * time.Millisecond)
        }
    }
}

func main() {
    fc.Start(LongRunningHandler)
}