This topic describes the background of event functions for custom container runtime and provides sample code.

Background information

In a custom container runtime, Function Compute forwards the common headers, request body, POST method, /invoke, and /initialize to the HTTP server in the container. You can use function signatures similar to context and event of officially supported runtimes such as Golang Runtime. You can also use the request headers and body as input parameters to define the service logic of a function. For more information, see Event function calls in a custom runtime.

Determine the type of the invoked function

For a custom container 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 container runtime. The custom container runtime returns the response headers and body to the client.

Calls

Path x-fc-control-path Request Expected response
POST /invoke /invoke

Request body: function input (the Payload specified when the function is invoked).

Request header: common request headers

Response body: the return value of the function handler.
  • 200: succeeded.
  • 404: failed.

Responds with x-fc-status in the header.

POST /initialize /initialize

Request body: none.

Request header: common request headers

Response body: No response body is returned when the request is successful.
  • 200: succeeded.
  • 404: failed.

Responds with x-fc-status in the header.

Sample code

In the following Node.js Express example, the POST method and /initialize are called by Function Compute when a function instance is initialized. The POST method and /invoke serve as handlers when Function Compute is called. The context and event parameters are obtained from req.headers and req.body, and the results of the function call are then returned as an HTTP response.

'use strict';

const express = require('express');

// Constants
const PORT = 9000;
const HOST = '0.0.0.0';


// initialize example, need config Initializer in function meta 
app.post('/initialize', (req, res) => {
  res.send('Hello FunctionCompute, /initialize\n');
});


// Event function invocation
app.post('/invoke', (req, res) => {
  res.send('Hello FunctionCompute, event function\n');
});

var server = app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);

server.timeout = 0; // never timeout
server.keepAliveTimeout = 0; // keepalive, never timeout