All Products
Search
Document Center

Function Compute (2.0):Principles

Last Updated:May 06, 2023

For a custom runtime, the code file in the ZIP format is an HTTP server program. This topic describes the basic principles of cold starts of a custom runtime and the requirements for the configurations of HTTP servers.

Principles

For a custom runtime, the code file in the ZIP format is an HTTP server program. You need to only configure the Startup Command and Startup Parameter parameters for the function to start the HTTP server. When Function Compute performs a cold start in a custom runtime, the Startup Command and Startup Parameter parameters are used to start your custom HTTP server. The HTTP server takes over all requests from Function Compute. The default port of an HTTP server is 9000. If you use another port, such as 8080, for the HTTP server, you can set the listening port in the function configurations to 8080.

For example, the name of the code package of a function is function.zip. The following examples show the files that are contained in the package and the Startup Command and Startup Parameter parameters based on the programming language that is used to develop the function.

Java 8 or Spring Boot

.
├── demo.jar


customRuntimeConfig:
 command:
 - java 
 args:
 - '-jar'
 - 'demo.jar'	

Python 3.7

.
├── server.py


customRuntimeConfig:
 command:
 - python
 args:
 - 'server.py'

Node.js 10

.
├── server.js


customRuntimeConfig:
 command:
 - node
 args:
 - 'server.js'

PHP 7.4

.
├── server.php


customRuntimeConfig:
 command:
 - php
 args:
 - 'server.php'
Note

customRuntimeConfig specifies the custom startup commands for the function. command specifies the startup commands for the container. args specifies the startup parameters. Function Compute concatenates the content in command and args to form a complete startup command. If the startup command and startup parameters are not configured, the HTTP server starts from /code/bootstrap by default.

Requirements on HTTP server configurations

When you create an HTTP server, make sure that the following requirements are met:

  • Services that are started in a custom runtime must listen on 0.0.0.0:CAPort or *:CAPort. If you use the 127.0.0.1:CAPort port, a request times out, and the following error is returned:

    {
    "ErrorCode":"FunctionNotStarted",
    "ErrorMessage":"TheCA'shttpservercannotbestarted:ContainerStartDuration:25000000000.PingCAfaileddueto:dialtcp21.0.XX.XX:9000:getsockopt:connectionrefusedLogs:2019-11-29T09:53:30.859837462ZListeningonport9000"
    }

    The default listening port of a custom runtime is port 9000. If a custom runtime uses the default listening port, the listening port of its HTTP server must be port 9000. If the listening port of the custom runtime is port 8080, the listening port of its HTTP server must be port 8080.

  • You must enable the keep-alive mode for connections and set the request timeout period to 24 hours (the maximum function running period) or longer. Sample code:

    // In this example, the express framework for Node.js is used.   
    
    var server = app.listen(PORT, HOST);
    server.timeout = 0; // never timeout
    server.keepAliveTimeout = 0; // keepalive, never timeout
  • The HTTP server must be started within 120 seconds.