Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Table of contents

  1. Using gRPC with Coldbrew
  2. Client side connection pool

If you are not familiar with gRPC, you can learn more about it at grpc.io.

Using gRPC with Coldbrew

Coldbrew is gRPC first, which means that gRPC APIs are the primary APIs and HTTP/JSON APIs are generated from the gRPC APIs. This approach is different from other frameworks where HTTP/JSON APIs are independent from gRPC APIs.

Best way to get started with gRPC in Coldbrew is to use the Coldbrew cookiecutter to generate a new project. The cookiecutter will generate a project with a sample gRPC service and a sample HTTP/JSON service. You can use the sample gRPC service as a template to create your own gRPC service.

You can than follow the README.md in the project or Building and Configuring APIs how to see how to use the generated service.

Client side connection pool

Coldbrew provides a simple gRPC connection pool implementation called grpcpool. You can use this package to create a connection pool for your gRPC services.

The package provides a grpcpool.Dial function that can be used to create a connection pool for a gRPC service. The function takes a grpc.DialOption as an argument. You can use this option to configure the gRPC client connection. For example, you can use this option to configure TLS, authentication, etc.

The following example shows how to create a connection pool for a gRPC service:


import (
    "github.com/go-coldbrew/grpcpool"
    "google.golang.org/grpc"
)


func main() {
    // Create a connection pool for a gRPC service with 3 connections.
    pool, err := grpcpool.Dial("localhost:50051", 3, grpc.WithInsecure())
    if err != nil {
        // Handle error.
    }

    // Get a connection from the pool.
    conn, err := pool.Get()
    if err != nil {
        // Handle error.
    }

    // Close the connection.
    conn.Close()
}

grpcpool implements grpc.ClientConnInterface to enable it to be used directly with generated proto stubs


import (
    "github.com/go-coldbrew/grpcpool"
    "google.golang.org/grpc"
)

func main() {
    // Create a connection pool for a gRPC service with 2 connections.
    pool, err := grpcpool.Dial("localhost:50051", 2, grpc.WithInsecure())
    if err != nil {
        // Handle error.
    }

    // Use the connection with generated proto stubs.
    client := pb.NewGreeterClient(pool)

    // make the call
    resp, err := client.SayHello(context.Background(), &pb.HelloRequest{Name: "World"})
    if err != nil {
        // Handle error.
    }
    fmt.Println(resp.Message)

    // Close the connection.
    conn.Close()
}

You can also use existing gRPC connections with grpcpool by wrapping it with grpcpool.New function.


import (
    "github.com/go-coldbrew/grpcpool"
    "google.golang.org/grpc"
)

func main() {
    // Create a gRPC connection.
    conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure())
    if err != nil {
        // Handle error.
    }

    // Create a connection pool for the gRPC connection.
    pool := grpcpool.New([]*grpc.ClientConn{conn})

    // Use the connection with generated proto stubs.
    client := pb.NewGreeterClient(pool)

    // make the call
    resp, err := client.SayHello(context.Background(), &pb.HelloRequest{Name: "World"})
    if err != nil {
        // Handle error.
    }
    fmt.Println(resp.Message)

    // Close the connection.
    conn.Close()
}