After you bind a custom domain name to a function, you can use a gRPC client to send gRPC requests. The function acts as a gRPC server, processing these requests and returning the results. This topic uses Go as an example to show you how to deploy a function by using Serverless Devs and trigger the function with a gRPC request.
Usage
Invocation method
You must use a custom domain name to invoke gRPC functions.
NoteWhen you bind a custom domain name to a function, you must configure a path mapping between the request path and the function. We recommend that you set the request path to
/*. This configuration forwards all gRPC requests to the target gRPC function, which then routes the requests to the specific gRPC methods defined by the client.Port: gRPC requests are sent to port
8089.
Transport security
To ensure the security of gRPC requests, the Function Compute production environment supports only clients that use the Transport Layer Security (TLS) protocol. Otherwise, requests fail with the error
rpc error: code = Unavailable desc = connection closed before server preface received.Custom domain names support custom HTTPS certificates. You do not need to define TLS certificate verification on the gRPC server side because the Function Compute gateway layer handles the TLS handshake.
Request timeout control
The maximum timeout for a gRPC request cannot exceed the function's execution timeout. By default, the execution timeout is 60 seconds, with a maximum of 86,400 seconds.
Request concurrency control
gRPC requests are subject to the Function Compute concurrency controls. Each gRPC request consumes one unit of concurrency. The gRPC protocol is built on HTTP/2. In Function Compute, gRPC requests that are routed to the same function instance share a single HTTP/2 connection. The number of concurrent streams on this connection is determined by the instance concurrency, which you can configure. For more information, see Configure instance concurrency.
Load balancing
Function Compute provides built-in load balancing by automatically distributing gRPC requests across instances.
Billing
gRPC has the following four types of requests:
unary gRPC request
client streaming request
server streaming request
bidirectional streaming request
In Function Compute, resource usage is calculated as Specifications × Billing Duration. The billing duration differs for unary and streaming gRPC requests:
Unary gRPC request:
For a function with an instance concurrency of 1, the billing duration is from the start of the request to the end of the request.
For a function with an instance concurrency greater than 1, the billing duration starts when the first request begins and ends when the last request finishes. The overlapping period for concurrent requests on a single instance is only billed once.
As shown in the following figure, an instance has its concurrency set to 2. The first request arrives at T1 and ends at T3. The second request arrives at T2 and ends at T4. The total billing duration is from T1 to T4. The overlapping period from T2 to T3 is billed only once.
Client streaming, server streaming, and bidirectional streaming requests: The billing duration starts when the first gRPC connection is established and ends when the last gRPC connection is closed.
Prerequisites
Install the latest version of the Go programming language on your local machine. Function Compute supports Go 1.x.
Install and configure Serverless Devs on your local machine. For more information, see Quick start.
Procedure
To initialize the project, run the following command.
sudo s init fc-custom-golang-grpc -d fc-custom-golang-grpcRun the following command to change to the
fc-custom-golang-grpcproject directory.cd fc-custom-golang-grpcIn the current project directory, edit the s.yaml file.
The following is a sample:
edition: 3.0.0 name: hello-world-app # access: Configures the access credentials for the application. # For more information about how to configure credentials, see https://www.serverless-devs.com/serverless-devs/command/config # For more information about the credential priority, see https://www.serverless-devs.com/serverless-devs/tool#密钥使用顺序与规范 access: default # The alias of the access credential. Modify it based on your actual requirements. resources: helloworld: # The name of the service or module. component: fc3 actions: # Customizes the 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 you want to deploy the application. Modify it based on your actual requirements. 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 finishes, the function is deployed to Function Compute. Function Compute then generates a public URL that you can use for testing.
Run the following command to install the gRPC client's dependencies.
go mod vendorBind a custom domain name to the function. For more information, see Configure a custom domain name.
Run the following command to invoke the gRPC client and send a gRPC request to verify that the function works as expected.
Before you run the command, replace
${your-custom-domain}with your custom domain name.go run ./greeter_client -addr ${your-custom-domain}:8089
More examples
Custom Runtime
Custom Container
FAQ
Function error
If the client reports the error rpc error: code = Internal desc = server closed the stream without sending trailers, it means the Function Compute server unexpectedly terminated the gRPC request. This is a function error, which can be caused by an execution timeout, an unexpected process exit, or an out-of-memory (OOM) error. You can check the function logs to find the specific cause and troubleshoot the issue. For more information, see View invocation logs.
Non-TLS gRPC clients
You must use a TLS client to send gRPC requests. Otherwise, the request fails with the error rpc error: code = Internal desc = server closed the stream without sending trailers. For example, you can use the following sample code in Go:
var opts []grpc.DialOption
cred := credentials.NewTLS(&tls.Config{
InsecureSkipVerify: false,
})
opts = append(opts, grpc.WithTransportCredentials(cred))
conn, err := grpc.Dial(*addr, opts...)You can set InsecureSkipVerify to true to skip TLS certificate verification, or to false to enforce it.