You need to enable JavaScript to run this app.
Volcano Engine Function as a Service

Volcano Engine Function as a Service

Copy page
Download PDF
Native runtime
Development guidelines (for web applications)
Copy page
Download PDF
Development guidelines (for web applications)
A veFaaS web application is an HTTP server running in the Native Runtime. This topic outlines the specifications and guidelines you need to follow when developing an HTTP server locally.​
Service specifications​
When developing an HTTP server locally, follow the specifications below:​
  • The service itself must be stateless​
  • If state persistence is required, store state remotely, for example, in Redis or MySQL. Service startup must not depend on local memory caches or on-disk data.​
  • Startup command​
  • For a simple startup command, you can enter it directly in the console.​
  • If the startup process is more complex, you can include a startup script in the project and specify this script when configuring the startup command in the console.​
warning​
The startup script must have the execute permission. Otherwise, a permission denied error will occur when you release the service. You can refer to the following command to grant execute permission to the script for all user groups in a Linux system.​
  • ​
    # Note: execute this command locally​
    # Assuming the startup script is run.sh​
    chmod a+x run.sh​
    ​
  • Listening port​
  • The service must listen on a port.​
  • ‌Ensure the port your service code listens on matches the port configured when you create the web application in the console. A mismatch will cause a cold start failure during function deployment with the following error.​
  • ​
    function_start_timeout​
    ​
    function process start timeout: function start timed out (120s) with command `./main --port 3000`, function may not listen on address *:8000, logs: current working directory: /opt/bytefaas Server listen on address: :3000. Please check your log(runtime_log)/code and then retry, or contact oncall​
    ​
  • In the service, you can obtain the specified listening port through the environment variable _FAAS_RUNTIME_PORT.​
  • Avoid background processes​
  • We recommend against starting background processes or threads (where the request has returned a response, but tasks continue executing asynchronously in the background). veFaaS dynamically scales backend instances based on request volume and cannot guarantee the survival of background processes or threads.​
HTTP server code example​
In the Native Runtime, veFaaS forwards the request Method, Path, Body, Query, and Headers to your HTTP Server. You can directly use the incoming request headers and body to implement your business logic.​
The following is a complete code sample of an HTTP server.​
​
package main​
​
import (​
"flag"​
"fmt"​
"log"​
"net/http"​
"os"​
"os/signal"​
"syscall"​
)​
​
func hello(w http.ResponseWriter, r *http.Request) {​
w.WriteHeader(http.StatusOK)​
w.Write([]byte("Hello World from VeFaaS"))​
}​
​
func main() {​
// Obtain the listening port from command-line parameters or environment variables.​
var port string​
flag.StringVar(&port, "port", "", "http server listen port")​
flag.Parse()​
​
if port == "" {​
port = os.Getenv("_FAAS_RUNTIME_PORT")​
}​
​
// Configure the request handler for the HTTP server.​
http.HandleFunc("/v1/hello", hello)​
​
addr := fmt.Sprintf("0.0.0.0:%s", port)​
srv := http.Server{Addr: addr}​
​
// Start the HTTP server.​
go func() {​
log.Printf("Start HTTP Server at: %s", addr)​
if err := srv.ListenAndServe(); err != nil {​
log.Println("ListenAndServe: ", err)​
}​
}()​
​
// Listens for SIGINT and SIGTERM signals.​
quit := make(chan os.Signal, 1)​
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)​
<-quit​
​
// Perform graceful shutdown after receiving the signal.​
log.Println("Shutting down server...")​
// Execute your graceful shutdown logic.​
log.Println("Doing your graceful shutdown logic")​
if err := srv.Shutdown(nil); err != nil {​
log.Fatal("Server shutdown error: ", err)​
}​
​
log.Println("Server gracefully stopped")​
}​
​
Service listening port and startup​
You can pass the port via a command-line parameter or obtain the configured listening port (default: 8000) via the environment variable _FAAS_RUNTIME_PORT. The following code demonstrates the logic for obtaining the port number from the command-line parameter and the environment variable.​
warning​
The listening port of the service must match the one configured in the console.​
​
var port string​
flag.StringVar(&port, "port", "", "http server listen port")​
flag.Parse()​
​
if port == "" {​
port = os.Getenv("_FAAS_RUNTIME_PORT")​
}​
​
After obtaining the listening port, ensure that the started HTTP server listens on 0.0.0.0:port. Otherwise, deployment will fail. The following code demonstrates the logic for concatenating the service listening address and starting the HTTP server.​
​
// Perform concatenation to form the 0.0.0.0:port address.​
addr := fmt.Sprintf("0.0.0.0:%s", port)​
srv := http.Server{Addr: addr}​
​
// Start the HTTP server.​
go func() {​
log.Printf("Start HTTP Server at: %s", addr)​
if err := srv.ListenAndServe(); err != nil {​
log.Println("ListenAndServe: ", err)​
}​
}()​
​
Request processing​
Since you implement your own HTTP server in the Native runtime, you can define custom request processing logic in the HTTP server code. The following code demonstrates a simple request handler interface.​
​
func hello(w http.ResponseWriter, r *http.Request) {​
w.WriteHeader(http.StatusOK)​
w.Write([]byte("Hello World from VeFaaS"))​
}​
​
The following code demonstrates the routing for request processing.​
​
// Configure the request handler for the HTTP server.​
http.HandleFunc("/v1/hello", hello)​
​
Graceful service shutdown​
When a function instance exits, veFaaS sends a SIGINT signal to your HTTP server. You can capture this signal and implement custom graceful shutdown logic. The following code demonstrates the process from signal capture to HTTP server shutdown.​
​
// Listens for SIGINT and SIGTERM signals.​
quit := make(chan os.Signal, 1)​
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)​
<-quit​
​
// Perform graceful shutdown after receiving the signal.​
log.Println("Shutting down server...")​
// Execute your graceful shutdown logic.​
log.Println("Doing your graceful shutdown logic")​
if err := srv.Shutdown(nil); err != nil {​
log.Fatal("Server shutdown error: ", err)​
}​
​
log.Println("Server gracefully stopped")​
​
Configure a startup command​
After code development and compilation, you need to configure a startup command and a listening port when you create the web application in the console. The figure below assumes that the service binary is named http-server and that the listening port is 3000.​
Use environment variables​
You can configure the environment variables required by the runtime of your function in the console and retrieve them in your code to implement function processing logic. veFaaS injects configured environment variables into the container where the function runs, and the program can read them using a built-in library for the programming language. For example, if the key of an environment variable is envKey, you can use the following code sample to read the environment variable.​
​
import "os"​
envValue := os.Getenv("envKey")​
​
​
Last updated: 2026.08.19 14:06:05