All Products
Search
Document Center

Function Compute:Custom domain name support for gRPC protocol

Last Updated:Apr 27, 2025

After binding a domain name to a function, you can send gRPC requests through a gRPC client. The function acts as a gRPC server that processes requests and returns results to the caller. This topic uses Golang to demonstrate how to deploy functions using Serverless Devs and trigger functions using gRPC requests.

Usage notes

Invocation method

  • Domain name: You can use a custom domain name to call gRPC functions.

    Note

    When binding a custom domain name to a function, you need to configure the mapping relationship between the request path and the function. We recommend configuring the request path as /*. This allows all gRPC requests to be forwarded to the corresponding gRPC function. The gRPC function routes the requests to the gRPC method defined by the client.

  • Port: The port for gRPC requests is 8089.

Transport security

  • To ensure the security of gRPC requests, the online environment of Function Compute supports only clients that use the TLS protocol. Otherwise, the request returns the error rpc error: code = Unavailable desc = connection closed before server preface received.

  • Custom domain names support custom HTTPS certificates. The gRPC server does not need to define TLS certificate verification because the Function Compute gateway layer performs TLS verification.

Request timeout control

The maximum timeout period for a gRPC request cannot exceed the Execution Timeout of the function. The default Execution Timeout is 60 seconds, and the maximum is 86400 seconds.

Request concurrency control

gRPC requests are subject to the concurrency control of Function Compute. A gRPC request consumes a concurrency quota. The gRPC protocol is based on HTTP/2. In Function Compute, for a function instance, the gRPC requests allocated to this function instance reuse the same HTTP/2 connection. Therefore, the number of concurrent flows on this HTTP/2 connection equals the concurrency of the instance. You can set the concurrency of a function instance to control the maximum concurrent requests per instance. For more information, see Configure instance concurrency.

Load balancing

Function Compute supports distributing gRPC requests to different instances and automatically load balancing gRPC requests.

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.

      image
  • 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

  1. Run the following command locally to initialize the project.

    sudo s init fc-custom-golang-grpc -d fc-custom-golang-grpc
  2. Run the following command to go to the fc-custom-golang-grpc project directory.

    cd fc-custom-golang-grpc
  3. In the current project directory, edit the s.yaml file.

    The following code provides an example:

    edition: 3.0.0
    name: hello-world-app
    # access specifies the key information required by the current application.
    # For more information about how to configure keys, visit https://www.serverless-devs.com/serverless-devs/command/config.
    # For more information about the sequence to use keys, visit https://www.serverless-devs.com/serverless-devs/tool#.
    access: default  #  Key alias. Modify it based on your actual situation.
    
    resources:
      helloworld: # The name of the service or module.
        component: fc3
        actions: # Custom execution logic. For more information about actions, visit 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 based on your actual situation.
          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/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/target
  4. Run sudo s deploy -y to deploy the function.

    After the command is executed, the function is deployed to Function Compute. Additionally, Function Compute generates a directly accessible URL that you can use to call the function for testing.

  5. Run the following command locally to install the dependencies required to run the gRPC client.

    go mod vendor
  6. Bind a custom domain name to the function. For more information, see Configure custom domain names.

  7. Run the following command locally to call the gRPC client, initiate a gRPC call, and test the function.

    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

Golang

Golang

Python

None

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.