Function Compute supports the gRPC protocol. You can use an HTTP trigger to invoke a gRPC service, where the function acts as a gRPC server to process client-side streaming and non-streaming requests. This lets you benefit from the elastic scaling and O&M-free capabilities of a serverless architecture.
Usage notes
Invocation Method
Domain name: You can use the
fcapp.runsubdomain or a custom domain name to invoke gRPC functions.NoteIf you use a custom domain name, you must configure the mapping between the request path and the function. We recommend that you set the request path to
/*. This setting forwards all gRPC requests to the corresponding gRPC function, which then routes the requests to the gRPC methods defined on the client.Port: The port for gRPC requests is
8089.
Transmission Security
To ensure the security of gRPC requests, the Function Compute online environment supports only clients that use the Transport Layer Security (TLS) protocol. If you do not use a TLS client, the
rpc error: code = Unavailable desc = connection closed before server preface receivederror is reported.Custom domain names support custom HTTPS certificates. The gRPC server does not need to define TLS certificate validation. The Function Compute gateway layer performs the TLS validation.
Request Timeout Control
The maximum timeout of a gRPC request cannot exceed the function's Execution Timeout. The default Execution Timeout is 60 seconds, with a maximum of 86,400 seconds.
Request Concurrency Control
The concurrency in Function Compute controls gRPC requests. Each gRPC request consumes one unit of concurrency. The gRPC protocol is based on HTTP/2. In Function Compute, gRPC requests that are allocated to a function instance share the same HTTP/2 connection. The number of concurrent streams on this HTTP/2 connection is equal to the instance concurrency. You can set the per-instance concurrency for a function to control the number of concurrent streams on an instance. For more information, see Configure per-instance concurrency.
Load Balancing
Function Compute automatically performs load balancing by distributing gRPC requests across different function instances.
Billing methods
gRPC requests are classified into the following types:
Common gRPC requests
Client-side streaming requests
Server-side streaming requests
Bidirectional streaming requests
In Function Compute, Resource usage = Specification × Billable duration. The billable duration varies between common gRPC requests and streaming gRPC requests, as described below:
Common gRPC requests:
For functions with a concurrency of 1, the billable duration is from the beginning to the end of the request.
For functions with a concurrency greater than 1, the billable duration is from the beginning of the first request to the end of the last request. If an instance processes multiple requests simultaneously, it will not be billed repeatedly.
As shown in the following figure, a function has a concurrency of 2. The first request arrives at T1 and ends at T3. The second request arrives at T2 and ends at T4. The billable duration is from T1 to T4. The period from T2 to T3 is billed only once, not repeatedly.
Client-side streaming, server-side streaming, and bidirectional streaming requests: The billable duration is from the beginning of the first gRPC connection to the end when the last gRPC connection is closed.
Prerequisites
You need to install the Go language environment locally. Function Compute supports Go 1.x versions. We recommend using the latest version.
You need to install and configure the Serverless Devs tool locally. For more information, see Quick Start.
Procedure
Run the following command to initialize the project.
sudo s init fc-custom-golang-grpc -d fc-custom-golang-grpcRun the following command to go to the project directory
fc-custom-golang-grpc.cd fc-custom-golang-grpcIn the current project directory, edit the s.yaml file.
The following is an example:
edition: 3.0.0 name: hello-world-app # Configure the key information required for the current application: # For more information about key configuration, see https://www.serverless-devs.com/serverless-devs/command/config # For more information about the order in which keys are used, see https://www.serverless-devs.com/serverless-devs/tool#密钥使用顺序与规范 access: default # The key alias. Modify it as needed. resources: helloworld: # The business name or module name. component: fc3 actions: # Custom execution logic. For more information about actions, see https://www.serverless-devs.com/serverless-devs/yaml#行为描述 pre-deploy: # Run before deployment. - run: go mod tidy path: ./code - run: GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o target/main main.go path: ./code props: region: cn-shanghai # The region where the application is deployed. Modify it as needed. functionName: "fc-custom-golang-grpc" handler: index.handler role: '' description: 'hello world by serverless devs' timeout: 60 diskSize: 512 internetAccess: true layers: - acs:fc:cn-shanghai:official:layers/Go1/versions/1 customRuntimeConfig: port: 8089 command: - ./main runtime: custom.debian10 cpu: 0.35 instanceConcurrency: 2 memorySize: 512 environmentVariables: PATH: >- /opt/Go1/bin:/usr/local/bin/apache-maven/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/ruby/bin:/opt/bin:/code:/code/bin LD_LIBRARY_PATH: >- /code:/code/lib:/usr/local/lib:/opt/lib:/opt/php8.1/lib:/opt/php8.0/lib:/opt/php7.2/lib code: ./code/targetRun
sudo s deploy -yto deploy the function.After the command runs, the function is deployed to Function Compute. Function Compute then generates a publicly accessible URL that you can use to test the function.
Create an HTTP trigger and obtain its public URL. For more information, see Create a trigger.

Run the following command to install the dependencies required to run the gRPC client.
go mod vendorRun the following command to call the gRPC client. This initiates a gRPC call to test the function.
fc-custang-grpc-*****.cn-shanghai.fcapp.runis the public URL of the trigger that you obtained in Step 5.go run ./greeter_client -addr fc-custang-grpc-*****.cn-shanghai.fcapp.run:8089
You can also configure a custom domain name for the gRPC function and use the custom domain name to send gRPC requests. For more information, see Custom domain names support the gRPC protocol.
More examples
Custom Runtime
Custom Container
FAQ
Function errors
If the client returns the error rpc error: code = Internal desc = server closed the stream without sending trailers, it indicates that the Function Compute server prematurely closed the gRPC request abnormally. This type of error is a function error. For example, the function times out, the function process exists abnormally, or an out of memory (OOM) error occurs. You can check the causes of the error in the function logs and troubleshoot the error. For more information, see View invocation logs.
GRPC Requests Not Using TLS Protocol Clients
You must use a TLS protocol client to call gRPC requests. Otherwise, the error rpc error: code = Internal desc = server closed the stream without sending trailers is returned. For example, for Golang, you can use the following sample code:
var opts []grpc.DialOption
cred := credentials.NewTLS(&tls.Config{
InsecureSkipVerify: false,
})
opts = append(opts, grpc.WithTransportCredentials(cred))
conn, err := grpc.Dial(*addr, opts...)The value of InsecureSkipVerify can be set to true to skip TLS certificate verification or false to not skip TLS certificate verification.