Principles

更新时间:
复制 MD 格式

In a custom runtime, your code package is an HTTP server. Function Compute starts it using the Startup Command and Startup Parameter you configure, then routes all invocation requests to it.

How it works

When Function Compute performs a cold start for a custom runtime function, it runs the following sequence:

  1. Extracts your code package (function.zip).

  2. Starts your HTTP server using the Startup Command and Startup Parameter values.

  3. Routes all function invocation requests to your HTTP server.

The HTTP server handles all requests from that point forward. If you do not configure a startup command and startup parameter, Function Compute starts the server from /code/bootstrap by default.

Configure the startup command

Set Startup Command and Startup Parameter in your function configuration using customRuntimeConfig:

customRuntimeConfig:
  command:
    - <executable>     # Startup Command
  args:
    - <argument>       # Startup Parameter

Function Compute concatenates command and args to form the complete startup command.

The following table summarizes the key values for common languages. All examples use a code package named function.zip.

LanguageFile in packagecommandargs
Java 8 or Spring Bootdemo.jarjava['-jar', 'demo.jar']
Python 3.7server.pypython['server.py']
Node.js 10server.jsnode['server.js']
PHP 7.4server.phpphp['server.php']

Examples by language

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'

HTTP server requirements

Your HTTP server must meet the following requirements before Function Compute can route requests to it.

Network binding

Bind your server to 0.0.0.0:CAPort or *:CAPort. Binding to 127.0.0.1:CAPort causes connection refused errors, and Function Compute returns a FunctionNotStarted error.

The default port is 9000. To use a different port, such as 8080, set the listening port in your function configuration and bind your server to the same port.

Connection settings

Enable keep-alive on all connections and set the timeout to 24 hours (the maximum function execution duration) or longer. The following example uses the Express framework for Node.js:

// Express framework example (Node.js)
var server = app.listen(PORT, HOST);
server.timeout = 0;          // never timeout
server.keepAliveTimeout = 0; // keepalive, never timeout

Startup time

Your HTTP server must be ready to accept connections within 120 seconds of the cold start. If the server does not start in time, Function Compute returns a FunctionNotStarted error.

Troubleshooting

FunctionNotStarted

If you receive the following error, your HTTP server failed to start or is not reachable on the expected port:

{
  "ErrorCode": "FunctionNotStarted",
  "ErrorMessage": "The CA's http server cannot be started: ContainerStartDuration: 25000000000. Ping CA failed due to: dial tcp 21.0.XX.XX:9000: getsockopt: connection refused Logs: 2019-11-29T09:53:30.859837462Z Listening on port 9000"
}

Common causes and fixes:

CauseFix
Server bound to 127.0.0.1 instead of 0.0.0.0Change the bind address to 0.0.0.0:CAPort or *:CAPort
Server port does not match the configured listening portMake sure the port in your server code matches the port in your function configuration
Server did not start within 120 secondsCheck for slow initialization logic or startup errors in the logs