This topic describes how to determine and invoke event functions in a custom runtime.

Background information

In a custom runtime, Function Compute forwards the common headers, request body, POST method, /invoke, and /initialize to the HTTP server. A request parameter similar to the context in an official runtime can be built based on the information in common headers and a request parameter similar to the event in an official runtime can be built based on the request body of the invoked function.

Determine the type of the invoked function

For a custom runtime, you can determine whether a request is used to invoke an HTTP function or an event function based on the x-fc-control-path header.

Note Generally, you do not need to take note of the x-fc-control-path header.
  • /invoke: The request is used to invoke an event function. /invoke indicates that the request is used to invoke an event function.
  • /initialize: The request is automatically initiated by Function Compute to invoke an initializer function when an execution environment is created for the first time. In the lifecycle of a container, the initializer function is invoked only once, which is similar to the class constructor.
  • /http-invoke: The request is used to invoke an HTTP function. /http-invoke indicates that the request is used to invoke an HTTP function. Function Compute adds common headers to your request that includes the HTTP method, request path, request body, query parameters, and request headers and forwards the request to the custom runtime. The custom runtime returns the response headers and body to the client.

Invocations

When an event function is invoked, only the path for the invocation is implemented: /invoke and /initialize.
Notice If the initializer parameter is not specified when a function is created, /initialize does not need to be implemented. In this case, even if /initialize is implemented by the HTTP server, the /initialize logic in the code cannot be invoked or executed.
Path Request Expected response
POST /invoke Response body: the return value of the function handler.
  • StatusCode
    • 200: succeeded.
    • 404: failed.
  • Header: x-fc-status
    • 200: succeeded.
    • 404: failed.
The x-fc-status field in the response header reports to Function Compute whether the function invocation is successful.
  • The x-fc-status field not specified: Function Compute reckons that the invocation is successful by default. However, your function may have an exception and the exception is not reported to Function Compute. Function Compute thinks that the function invocation is successful. This may not affect the business logic, but will affect monitoring observability.
  • The x-fc-status field specified: When your function has an exception, the x-fc-status field in the response header reports to Function Compute that the function execution fails and pushes the error stack information to logs.
Note In the HTTP response, we recommend that you specify both the StatusCode and x-fc-status fields.
(Optional) POST /initialize Response body: the return value of the function initializer.
StatusCode
  • 200: succeeded.
  • 404: failed.