Function Compute supports the gRPC protocol. Use an HTTP trigger to invoke a gRPC service, where the function acts as a gRPC server processing client-side streaming and non-streaming requests. This lets you take advantage of the elastic scaling and fully managed capabilities of a serverless architecture.
Constraints
| Parameter | Value |
|---|---|
| Port | 8089 |
| TLS | Required. The online environment accepts only TLS clients. |
| Default execution timeout | 60 seconds |
| Maximum execution timeout | 86,400 seconds |
| Concurrency unit | Each gRPC request consumes one unit of concurrency. |
Invocation method
Invoke gRPC functions using either the fcapp.run subdomain or a custom domain name.
If you use a custom domain name, configure the mapping between the request path and the function. Set the request path to /* to forward all gRPC requests to the function, which then routes them to the gRPC methods defined on the client.Transmission security
The Function Compute online environment accepts only clients that use the Transport Layer Security (TLS) protocol. Without a TLS client, the following error is returned:
rpc error: code = Unavailable desc = connection closed before server preface receivedCustom domain names support custom HTTPS certificates. The Function Compute gateway layer handles TLS validation — the gRPC server does not need to configure TLS certificate validation.
Request timeout
The maximum timeout of a gRPC request cannot exceed the function's Execution Timeout, which acts as the server-side deadline limit. The default Execution Timeout is 60 seconds, with a maximum of 86,400 seconds.
Request concurrency
Function Compute controls gRPC concurrency at the instance level. Each gRPC request consumes one unit of concurrency. Because gRPC is based on HTTP/2, requests allocated to a function instance share the same HTTP/2 connection. The number of concurrent streams on that connection equals the instance concurrency. Set 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 distributes gRPC requests across different function instances.
Billing
gRPC supports four request types:
Common gRPC requests: The client sends a single request and receives a single response.
Client-side streaming: The client sends a stream of messages and receives a single response.
Server-side streaming: The client sends a single request and receives a stream of messages.
Bidirectional streaming: Both sides send a stream of messages concurrently.
Billing formula: Resource usage = Specification × Billable duration
The billable duration varies by request type:
Common gRPC requests
Per-instance concurrency = 1: billable duration runs from the start to the end of the request.
Per-instance concurrency > 1: billable duration runs from the start of the first request to the end of the last request. Overlapping periods are billed only once.
The following diagram shows a function with a per-instance 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 T1 to T4. The period from T2 to T3 is billed once, not twice.
Streaming requests (client-side, server-side, and bidirectional)
Billable duration runs from the start of the first gRPC connection to the end when the last gRPC connection is closed.
Prerequisites
Before you begin, make sure that you have:
A local Go environment. Function Compute supports Go 1.x. Use the latest version.
Serverless Devs installed and configured locally. For more information, see Quick Start.
Deploy and test a gRPC function
Initialize the project.
sudo s init fc-custom-golang-grpc -d fc-custom-golang-grpcGo to the project directory.
cd fc-custom-golang-grpcEdit the
s.yamlfile in the project directory. The following example shows the key configuration: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/targetDeploy the function.
sudo s deploy -yAfter deployment, Function Compute generates a publicly accessible URL for testing.
Create an HTTP trigger and obtain its public URL. For more information, see Create a trigger.

Install the gRPC client dependencies.
go mod vendorRun the gRPC client to test the function. Replace
fc-custang-grpc-*****.cn-shanghai.fcapp.runwith the public URL obtained in step 5.go run ./greeter_client -addr fc-custang-grpc-*****.cn-shanghai.fcapp.run:8089
To use a custom domain name instead, see Custom domain names support the gRPC protocol.
More examples
Custom runtime
Custom container
Troubleshooting
rpc error: code = Internal desc = server closed the stream without sending trailers
The Function Compute server closed the gRPC connection before sending trailers. Common causes:
The function timed out.
The function process exited abnormally.
An out of memory (OOM) error occurred.
Check the function logs to identify the cause. For more information, see View invocation logs.
rpc error: code = Unavailable desc = connection closed before server preface received
The gRPC client is not using a TLS connection. The Function Compute online environment requires TLS. Use TLS credentials when creating the connection. The following Go example shows how to configure TLS:
var opts []grpc.DialOption
cred := credentials.NewTLS(&tls.Config{
InsecureSkipVerify: false,
})
opts = append(opts, grpc.WithTransportCredentials(cred))
conn, err := grpc.Dial(*addr, opts...)Set InsecureSkipVerify to true to skip TLS certificate verification, or false to enforce it.