针对Custom Runtime,您的代码文件ZIP包是一个HTTP Server程序。本文介绍冷启动Custom Runtime的基本原理和HTTP Server配置要求。
基本原理
针对Custom Runtime,您的代码文件ZIP包是一个HTTP Server程序,您只需设置函数配置中的启动命令和启动参数完成HTTP Server的启动。函数计算冷启动Custom Runtime时,会调用您设置的启动命令和启动参数启动您自定义的HTTP Server,该HTTP Server接管了来自函数计算的所有请求。HTTP Server的默认端口是9000,如果您的HTTP Server是其他端口,比如8080,您可以设置函数配置中的监听端口为8080。
例如,函数的程序包名称为function.zip,该运行时各个语言程序包内的文件形式和相应的启动命令和启动参数示例如下:
. ├── demo.jar customRuntimeConfig: command: - java args: - '-jar' - 'demo.jar'
. ├── server.py customRuntimeConfig: command: - python args: - 'server.py'
. ├── server.js customRuntimeConfig: command: - node args: - 'server.js'
. ├── server.php customRuntimeConfig: command: - php args: - 'server.php'
customRuntimeConfig
是函数的自定义启动命令配置。其中command
是容器的入口命令列表,args
是启动参数列表。函数计算依次把command
和args
列表中的内容进行拼接,形成完整的启动命令。如果未配置启动命令及启动参数,HTTP Server将默认从/code/bootstrap启动。
HTTP Server配置要求
创建HTTP Server时您需要满足以下要求:
- Custom Runtime启动的服务一定要监听
0.0.0.0:CAPort
或*:CAPort
端口。如果您使用127.0.0.1:CAPort
端口,会导致请求超时,出现以下错误:{ "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" }
Custom Runtime的监听端口,即函数属性监听端口默认是9000。如果Custom Runtime使用默认的监听端口,那么您实现的Custom Runtime的HTTP Server监听的端口也必须是9000。如果Custom Runtime使用的监听端口是8080,那么您实现的Custom Runtime的HTTP Server监听的端口也必须是8080。
- Connection需要设置为Keep-Alive,Server端请求超时时间需设置在24小时(函数最大运行时间)及以上。示例如下:
//例如Node.js使用express时。 var server = app.listen(PORT, HOST); server.timeout = 0; // never timeout server.keepAliveTimeout = 0; // keepalive, never timeout
- HTTP Server需要在120秒内启动完毕。