HTTP triggers support asynchronous

Currently, Alibaba Cloud function computing supports two types of functions: event functions and HTTP functions. HTTP functions combined with HTTP triggers can support users to directly initiate function calls through HTTP requests using the Restful API; In this way, users can implement function calls without integrating the SDK provided by function computing, better integrating with existing system components and Web services.

Considering the purpose of the initial design of HTTP functions, for Web scenarios, HTTP functions do not support asynchronous calls. As the number of scenarios where users use HTTP functions increases, HTTP functions cannot support asynchronous calls, which poses many restrictions on the wider use of HTTP functions by users.

How difficult is it for users not to support asynchronous calls?

Currently, there are many customers who use function calculations and HTTP triggers to build Web services, and many of them have the need to process transcoding of files (videos, images, etc.), deliver tasks, and perform pressure testing through the Web service. These requirements often have characteristics such as long execution times and uneven traffic. Functions with these characteristics have the following disadvantages in synchronous execution scenarios:

Long execution of functions increases the risk of function errors and increases machine overhead.

• Clients need to maintain long links, network fluctuations, and customers lose patience and disconnect themselves due to lengthy function execution, all of which increase the probability of function errors.

• Scenario: Video website users upload video transcoding, which takes a long time to refresh the page, resulting in connection interruption and transcoding failure.

• Maintaining long links increases client machine overhead and reduces the utilization of client machine resources.

Unable to smoothly process and receive sudden increases in traffic.

• For scenarios with concurrency restrictions, the customer's sudden increase in traffic will be restricted in synchronous invocation scenarios, resulting in certain request failures when the customer does not perform error processing.

• Scenario 1: Pulse pressure measurement scenario.

• Scenario 2: Limited time online promotional activities.

In these scenarios, customers can decouple HTTP triggering and function execution through asynchronous calls, improving execution efficiency and success rates, and reducing overhead. The guarantee of asynchronously calling at least once, the ability to deliver the target, and the asynchronous task mode with observability and controllability can better enable customers to enjoy the convenience of function hosting services and free their hands.

When HTTP triggers do not support asynchronous calls, customers often need to indirectly implement HTTP triggered asynchronous calls through function jumps to meet their needs. The specific process is as follows:

The customer can create two functions. Function A is an HTTP function that can be invoked synchronously through HTTP, and function B is an event function that can be invoked asynchronously through the SDK through HTTP functions. However, the disadvantages of this scheme are also obvious:

• High cost: Each asynchronous call requires two triggers.

• Unable to achieve full hosting of flow control: The first layer of functions is called synchronously, and in the face of sudden increase in traffic being controlled, customers need to make self-adaptation, so they cannot enjoy full hosting of flow control for asynchronous calls.

• Increase customer development and maintenance costs: Two functions need to be developed and maintained to use asynchronous functionality.

New feature: HTTP triggers support asynchronous calls

The function calculation currently online supports the function of HTTP triggers for asynchronous calls. To use this function, customers need to prepare an HTTP function and an HTTP trigger.

Customers can create HTTP functions and triggers through the function calculation console, SDK, and Serverless Devs tools. HTTP trigger clients can configure themselves. If not configured, when creating HTTP functions, the function calculation will automatically create a default trigger for you.

HTTP triggers require the request header X-Fc Invocation Type to select the trigger method. The default is synchronous trigger. If asynchronous trigger is required, {"X-Fc Invocation Type": "Async"} can be added to the request header to achieve asynchronous trigger.

HTTP Asynchronous Call Test

• Testing through the console

Perform the test at the function code, and perform the asynchronous call test by checking Asynchronous Call.

On the test function side, you can check "I want to call asynchronously" to implement asynchronous call testing.

• Testing through cURL

• View test results

Upon completion of the trigger, the function will immediately return to calculate the result of the received request. The status code 202 indicates that the request was successful, and the rest indicates that an error occurred in the request. The request ID will also be returned in the request header, and you can track the execution status of the asynchronous request through the returned request ID. To better observe the execution status of request requests and increase control over function execution, you can enable asynchronous tasks.

You can query the execution result log on 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 callback of asynchronous execution results, please refer to the function callback documentation:

https://help.aliyun.com/document_detail/422720.html

Best Practices

Taking HTTP triggered video transcoding as an example, this article describes how to achieve HTTP triggered asynchronous tasks by combining Serverless Devs with the console.

prerequisite:

• Installing Serverless Devs

• Configure Serverless Devs

• Create OSS storage space for access and content changes in subsequent test cases.

• Create RAM roles and grant OSSFullAccess permission policies. You can also directly use the AliyunFCDefaultRolePolicy policy. For more information about permission policies, see Creating RAM Roles.

Operating Steps:

1. Initialize Project

2. Enter the project and deploy

3. Initiate asynchronous calls through HTTP triggers

4. Log in to the console to query the task status and control the task

5. Function callback

You can add processing logic to dest-fail or dest-succ as required, serving as a callback function for transcoding function execution results, and sensing and obtaining function execution results.

Summary

HTTP triggers support asynchronous calls, providing users with the ability to build Web services using function calculations as a full custody platform, which opens the last mile, allowing users of HTTP triggers to experience the convenience of asynchronous calls out of the box.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us