Author | Cheer (Alibaba Cloud Serverless Technical Expert)
Currently, Alibaba Cloud Function Compute supports two types of functions: event functions and HTTP functions . Among them, HTTP functions combined with HTTP triggers can support users to directly initiate function calls by using Restful API through HTTP requests;
In this way, users can implement function calls without integrating the SDK provided by Function Compute, and better integrate with existing system components and Web services.
Considering the purpose of the original design of HTTP functions, for Web scenarios, HTTP functions do not support asynchronous calls. With the increase in the number of scenarios where users use HTTP functions, HTTP functions cannot support the limitation of asynchronous calls. The wider use of HTTP functions by users brings many restrictions.
Does not support asynchronous calls, how difficult is it for users?
At present, many customers have used Function Compute HTTP triggers to build WEB services, and many of them need to transcode files (videos, pictures, etc.) through Web services, deliver tasks, and perform stress testing.
These requirements often have the characteristics of long execution and uneven traffic. Functions with these characteristics have the following disadvantages in the case of synchronous execution:
1. Long execution functions increase the risk of function errors and increase machine overhead.
- The client needs to maintain a long link, network fluctuations, and the client loses patience and disconnects the link because the function execution takes a long time, all of which increase the probability of function errors.
Scenario: A video website user uploads a video for transcoding, and it takes a long time to refresh the page, causing the link to be interrupted and the transcoding to fail.
- Keeping a long link increases the machine overhead of the client and reduces the utilization of the client's machine resources.
2. In the face of sudden traffic, it cannot be smoothly processed and received.
- For scenarios with concurrency restrictions, the client's burst traffic will be limited in the synchronous call scenario, which will cause certain requests to fail when the client does not handle errors.
Scenario 1: Pulse stress measurement scenario.
Scenario 2: Limited time online promotion.
In these scenarios, customers can decouple HTTP triggering and function execution through asynchronous calls, improving execution efficiency and execution success rate, and reducing overhead.
The guarantee of asynchronous invocation at least once, the ability of target delivery, and the asynchronous task mode with observability and controllability can better allow customers to enjoy the convenience of function hosting services and free customers' hands.
When HTTP triggers do not support asynchronous calls, in order to meet their needs, customers often need to indirectly implement HTTP-triggered asynchronous calls through function jumps. The specific process is as follows:
Customers can create two functions, function A is an HTTP function that can be called synchronously through HTTP; function B is an event function that can be asynchronously called by an HTTP function through the SDK. However, the disadvantages of this scheme are also obvious:
- High cost: each asynchronous call requires two triggers.
- Unable to achieve full flow control hosting: The first layer functions are synchronously called, and in the face of sudden traffic flow control, the customer needs to make self-adaptation, so that they cannot enjoy the full flow control hosting of asynchronous calls.
- Increase customer development and maintenance costs: Two functions need to be developed and maintained to use asynchronous functions.
New: HTTP triggers support asynchronous calls
Function Compute is currently online and supports the function of HTTP triggers for asynchronous calls. To use this feature, the client needs to prepare an HTTP function and an HTTP trigger.
Customers can create HTTP functions and triggers through the Function Compute console, SDK, and Serverless Devs tools. HTTP trigger clients can configure it themselves. If you don't configure it, when you create an HTTP function, Function Compute will automatically create a default trigger for you.
HTTP triggers need to select the triggering method through the request header X-Fc-Invocation-Type . The default is synchronous triggering. If asynchronous triggering is required, you can add {"X-Fc-Invocation-Type": "Async" to the request header "} to achieve asynchronous triggering.
HTTP asynchronous call test
1) Test via console
Test at the function code, and perform the asynchronous call test by checking the asynchronous call.
On the test function side, you can implement the asynchronous call test by checking "I want to call in an asynchronous way" .
2) Test via cURL
curl -v -H "X-Fc-Invocation-Type: Async" https://http-***.cn-shenzhen.fcapp.run/$path
View the test results:
After the trigger is completed, the result of the Function Compute receiving request will be returned immediately. Among them, the status code 202 indicates that the request is successful, and the rest indicate that there is an error in the request. The request ID is also returned in the request header, and you can track the execution status of the asynchronous request through the returned request ID. In order to better observe the request execution status and increase the control over function execution, you can enable asynchronous tasks [ 1] .
You can query the execution result log in the console through the request ID:
For requests to activate asynchronous tasks, status monitoring and query can be performed through the asynchronous task list:
For the callback of asynchronous execution results, please refer to the function callback document [2 ] .
Best Practices
This section uses HTTP-triggered video transcoding as an example to introduce how to implement HTTP-triggered asynchronous tasks by combining Serverless Devs and the console.
Prerequisites:
- Install Serverless Devs [3 ]
- Configure Serverless Devs [4 ]
- Create an OSS storage space [5 ] so that the OSS storage space can be accessed and content changed in subsequent test cases.
- Create a RAM role and grant the OSSFullAccess permission policy. You can also use the AliyunFCDefaultRolePolicy policy directly. See Creating RAM Roles [6 ] for details on permission policies.
Steps:
1. Initialize the project
s init http-video-transcode -d http-video-transcode
2. Enter the project and deploy
cd http-video-transcode && s deploy
3. Initiate asynchronous calls through HTTP triggers
curl -v -H "X-Fc-Invocation-Type: Async" -H "Content-Type: application/json" -d '{"bucket":"my-bucket", "object":"480P.mp4", "output_dir":"a", "dst_format":"mov"}' -X POST https://http-***.cn-shenzhen.fcapp.run/
4. Log in to the console to query the task status and manage and control the task
5. Function callback
You can add processing logic to dest-fail or dest-succ according to your needs, as a callback function for the execution result of the transcoding function, to perceive and obtain the execution result of the function.
Function callback related documents: https://help.aliyun.com/document_detail/422720.html
Summarize
HTTP triggering supports asynchronous calls, which enables users to build WEB services and use function computing as a fully managed platform to get through the last mile, so that users of HTTP triggers can also experience the convenience of asynchronous calls out of the box.
Reference link:
[1] Asynchronous tasks:
https://help.aliyun.com/document_detail/62921.html
[2] Result callback:
https://help.aliyun.com/document_detail/422720.html
[3] Install Serverless Devs:
https://help.aliyun.com/document_detail/195474.htm
[4] Configure Serverless Devs:
https://help.aliyun.com/document_detail/295894.html
[5] Create OSS storage space
https://help.aliyun.com/document_detail/31842.html
[6] Create RAM role
https://help.aliyun.com/document_detail/116800.htm#
Click here to learn more about FC!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。