All Products
Document Center

Invoking a function

Last Updated: Jun 06, 2018

Invocation types

Function Compute supports synchronous and asynchronous invocation of a function.

  • Synchronous invocation: The event is processed by the function, and then the result is returned.
  • Asynchronous invocation: The event is queued in Message Queue, and then no result is returned. Function Compute will process event based on Message Queue At-Least-Once delivery guarantee.
  • Different limits apply for synchronous and asynchronous invocation. For more information, see Limits.

You can manually invoke your functions using the console or command line tool. For more information, see Relevant examples. You can also invoke your function calling the REST API. For more information, see API specification. SDKs of different languages are provided to further simplify your operations. The following is an example of calling the Java SDK:

  1. public class FcSample {
  2. private static final String CODE_DIR = "/tmp/fc_code";
  3. private static final String REGION = "cn-shanghai";
  4. private static final String SERVICE_NAME = "test_service";
  5. private static final String FUNCTION_NAME = "test_function";
  6. public static void main(final String[] args) throws IOException {
  7. String accessKey = System.getenv("ACCESS_KEY");
  8. String accessSecretKey = System.getenv("SECRET_KEY");
  9. String accountId = System.getenv("ACCOUNT_ID");
  10. String role = System.getenv("ROLE");
  11. // Initialize FC client
  12. FunctionComputeClient fcClient = new FunctionComputeClient(REGION, accountId, accessKey, accessSecretKey);
  13. // Create a service
  14. CreateServiceRequest csReq = new CreateServiceRequest();
  15. csReq.setServiceName(SERVICE_NAME);
  16. csReq.setDescription("FC test service");
  17. csReq.setRole(role);
  18. CreateServiceResponse csResp = fcClient.createService(csReq);
  19. System.out.println("Created service, request ID " + csResp.getRequestId());
  20. // Create a function
  21. CreateFunctionRequest cfReq = new CreateFunctionRequest(SERVICE_NAME);
  22. cfReq.setFunctionName(FUNCTION_NAME);
  23. cfReq.setDescription("Function for test");
  24. cfReq.setMemorySize(128);
  25. cfReq.setHandler("hello_world.handler");
  26. cfReq.setRuntime("nodejs6");
  27. Code code = new Code().setDir(CODE_DIR);
  28. cfReq.setCode(code);
  29. cfReq.setTimeout(10);
  30. CreateFunctionResponse cfResp = fcClient.createFunction(cfReq);
  31. System.out.println("Created function, request ID " + cfResp.getRequestId());
  32. // Invoke the function with a string as function event parameter, Sync mode
  33. InvokeFunctionRequest invkReq = new InvokeFunctionRequest(SERVICE_NAME, FUNCTION_NAME);
  34. String payload = "Hello FunctionCompute!"
  35. invkReq.setPayload(payload.getBytes())
  36. InvokeFunctionResponse invkResp = fcClient.invokeFunction(invkReq);
  37. System.out.println(new String(invkResp.getContent()));
  38. // Invoke the function, Async mode
  39. invkReq.setInvocationType(Const.INVOCATION_TYPE_ASYNC);
  40. invkResp = fcClient.invokeFunction(invkReq);
  41. if (HttpURLConnection.HTTP_ACCEPTED == invkResp.getStatus()) {
  42. System.out.println("Async invocation has been queued for execution, request ID: " + invkResp.getRequestId());
  43. } else {
  44. System.out.println("Async invocation was not accepted");
  45. }
  46. // Delete the function
  47. DeleteFunctionRequest dfReq = new DeleteFunctionRequest(SERVICE_NAME, FUNCTION_NAME);
  48. DeleteFunctionResponse dfResp = fcClient.deleteFunction(dfReq);
  49. System.out.println("Deleted function, request ID " + dfResp.getRequestId());
  50. // Delete the service
  51. DeleteServiceRequest dsReq = new DeleteServiceRequest(SERVICE_NAME);
  52. DeleteServiceResponse dsResp = fcClient.deleteService(dsReq);
  53. System.out.println("Deleted service, request ID " + dsResp.getRequestId());
  54. }
  55. }

Concurrent Execution

Concurrent Executions is the total concurrent function executions within a given time period. You can use the following formula to estimate the concurrent function execution number.

Request rate x Function execution time

The request rate is the function execution rate, with the unit of “Requests per second” or “Events per second”. The unit of Function execution time is “Second”. For example, given an OSS events processing function, assuming that the function execution time is 3s, and OSS generates 10 events per second. Therefore, your function has 30 concurrent executions.

Concurrent Execution Limits

In some cases, function execution may be out of control due to incorrect settings. For example, you have set an OSS trigger. When image files are uploaded to the foo bucket of OSS, the relevant function is called. This function adjusts the source image to three images with different resolutions and incorrectly writes the result back to the foo bucket. As a result, a new function is called, resulting in an infinite loop. To prevent financial loss caused by infinite function calls, Function Compute sets the maximum number of concurrent function executions (100 by default) for each account. You can check the throttles metrics of a function in CloudMonitor. To adjust the limit, open a ticket.

Throttling errors handling

Throttling errors are handled differently based on the different invocation type.

  • Synchronous invocation: It the function is invoked synchronously and is throttled, Function Compute retuens a 429 error and the invoking service is responsible for retries. For example, if you use API Gateway to invoke a function, please make sure that the response error of Function Compute is mapped to the error code of API Gateway. If you directly invoke the function through the SDK or CLI, you can decide whether to retry based on your requirement.

  • Asynchronous invocation: If your function is invoded asynchronously and is throttled, Function Compute automatically retries the throttled event for up to 5 hours, with exponential backoff. Asynchronous events are queued before they are used to invoke the function.