This topic describes the background information and scenarios in which asynchronous invocations can be used. This topic also describes how to defer the invocation of a function.
Background information
When Function Compute receives an asynchronous invocation request, the system returns a response immediately after the request is persisted instead of waiting for the request to be completed before returning a response. Function Compute ensures that the request is executed at least once. To obtain the results of the asynchronous invocation, you can configure destinations for the asynchronous invocation. For more information, see Result callback. To obtain the status of the asynchronous request at each phase, you can enable the task mode. For more information, see Overview.
Scenarios
- In a user registration system, a new user sends a registration request. After the registration is successful, the system sends email notifications to the user. The action of sending an email can be separated from the registration process and executed asynchronously.
- When you upload a file, actions such as format conversion and import and export can be separated from the data upload process and executed asynchronously.
Deferred invocation of a function
In some scenarios, after you submit an asynchronous invocation request, you may want Function Compute to defer the invocation. In this case, you can use the Function Compute API or SDK to defer the invocation.
Add the HTTP request header x-fc-async-delay
to the code. Valid values: (0,3600). Unit: seconds. Function Compute invokes the function after the period specified by x-fc-async-delay
elapses.
package main
import (
"fmt"
"os"
"github.com/aliyun/fc-go-sdk"
)
func main() {
fcClient, err := fc.NewClient(fmt.Sprintf("%s.cn-shanghai.fc.aliyuncs.com", os.Getenv("ACCOUNT_ID")),
"2016-08-15", os.Getenv("ACCESS_KEY_ID"), os.Getenv("ACCESS_KEY_ID_SECRET"))
if err != nil {
panic(err)
}
// invoke function with delay
invokeInput := fc.NewInvokeFunctionInput({ServiceName}, {FunctionName}).WithPayload({payload})
invokeInput = invokeInput.WithAsyncInvocation().WithHeader("x-fc-async-delay", "200")
_, err := FCClient.InvokeFunction(invokeInput)
if err != nil {
panic(err)
}
}
Common features
See the following topics to learn about common features of asynchronous invocations: