When Function Compute runs a function, it passes a context.Context object to the handler. The context gives your code access to runtime metadata — request IDs, temporary credentials, function configuration, and deadline information — without any extra API calls.
Use the context object when you need to:
Read invocation metadata such as the request ID or account ID.
Access temporary credentials to call other Alibaba Cloud services (for example, Object Storage Service (OSS)) without hardcoding an AccessKey pair.
Track how much execution time remains before the function times out.
Context variables
The following variables are available on the fccontext object. To retrieve them, import github.com/aliyun/fc-runtime-go-sdk/fccontext and call fccontext.FromContext(ctx).
| Variable | Description |
|---|---|
RequestID | The unique ID of the current invocation. Record this value in your logs to simplify troubleshooting. |
Credentials | A temporary AccessKey pair that Function Compute generates by assuming your service-linked role. The pair is valid for 36 hours. Use it to call services such as OSS from your function code — no need to store credentials in the code itself. For details, see Grant Function Compute permissions to access other Alibaba Cloud services. |
Function | Basic metadata about the invoked function: name, handler, memory limit, and timeout period. |
Service | Metadata about the service the function belongs to: name, the associated Simple Log Service (SLS) project and Logstore, version, and alias. The qualifier field holds the version or alias specified at invocation time; versionId holds the version that was actually executed. |
Region | The ID of the region where the function runs. For example, cn-shanghai for the China (Shanghai) region. For a full list of region IDs, see Service endpoints. |
AccountId | The ID of the Alibaba Cloud account that owns the function. |
Context method
| Method | Description |
|---|---|
deadline | The function's execution deadline as a UNIX timestamp in milliseconds. Call ctx.Deadline() to retrieve the corresponding time.Time value. |
For the complete data structure, see fc-runtime-go-sdk on GitHub.
Read context information
Add context.Context as a parameter to your handler. Call fccontext.FromContext to unwrap the Function Compute-specific fields.
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
}Track remaining execution time
Use ctx.Deadline() to get the absolute deadline for the current invocation, then calculate how long your function has left. The example below subtracts a 100 ms buffer from the deadline and stops the work loop before the timeout is reached.
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)
}