https://github.com/go-coldbrew/grpcpool
grpcpool is a pool of grpc.ClientConns
https://github.com/go-coldbrew/grpcpool
connection-pool grpc load-balancer
Last synced: about 1 month ago
JSON representation
grpcpool is a pool of grpc.ClientConns
- Host: GitHub
- URL: https://github.com/go-coldbrew/grpcpool
- Owner: go-coldbrew
- License: bsd-3-clause
- Created: 2023-04-14T03:50:19.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-04-19T13:19:25.000Z (about 2 years ago)
- Last Synced: 2025-04-21T09:55:25.261Z (about 1 year ago)
- Topics: connection-pool, grpc, load-balancer
- Language: Go
- Size: 90.8 KB
- Stars: 0
- Watchers: 0
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/go-coldbrew/grpcpool/actions/workflows/go.yml)
[](https://goreportcard.com/report/github.com/go-coldbrew/grpcpool)
[](https://pkg.go.dev/github.com/go-coldbrew/grpcpool)
[](https://opensource.org/licenses/MIT)
# grpcpool
```go
import "github.com/go-coldbrew/grpcpool"
```
grpcpool is a pool of grpc.ClientConns. It implements grpc.ClientConnInterface to enable it to be used directly with generated proto stubs. It is based on https://github.com/googleapis/google-api-go-client/blob/v0.115.0/transport/grpc/pool.go
## Index
- [type ConnPool](<#ConnPool>)
- [func Dial\(target string, num uint, opts ...grpc.DialOption\) \(ConnPool, error\)](<#Dial>)
- [func DialContext\(\_ context.Context, target string, num uint, opts ...grpc.DialOption\) \(ConnPool, error\)](<#DialContext>)
- [func New\(conns \[\]\*grpc.ClientConn\) ConnPool](<#New>)
ConnPool is a pool of grpc.ClientConns.
```go
type ConnPool interface {
// Conn returns a ClientConn from the pool.
//
// Conns aren't returned to the pool.
Conn() *grpc.ClientConn
// Num returns the number of connections in the pool.
//
// It will always return the same value.
Num() int
// Close closes every ClientConn in the pool.
//
// The error returned by Close may be a single error or multiple errors.
Close() error
// ConnPool implements grpc.ClientConnInterface to enable it to be used directly with generated proto stubs.
grpc.ClientConnInterface
}
```
```go
func Dial(target string, num uint, opts ...grpc.DialOption) (ConnPool, error)
```
Dial creates a new ConnPool with num connections to target.
```go
func DialContext(_ context.Context, target string, num uint, opts ...grpc.DialOption) (ConnPool, error)
```
DialContext creates a new ConnPool with num connections to target.
Note: The ctx parameter is retained for backward compatibility but is not used. Cancellation and deadlines in ctx do not affect connection creation.
```go
func New(conns []*grpc.ClientConn) ConnPool
```
New creates a new ConnPool from the given connections.
Example
```go
package main
import (
"fmt"
"github.com/go-coldbrew/grpcpool"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func main() {
// Create individual gRPC connections (NewClient is lazy — no real connection yet)
conn1, err := grpc.NewClient("localhost:9090",
grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
panic(err)
}
conn2, err := grpc.NewClient("localhost:9090",
grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
panic(err)
}
// Create a round-robin pool from existing connections
pool := grpcpool.New([]*grpc.ClientConn{conn1, conn2})
defer pool.Close()
fmt.Println("pool size:", pool.Num())
}
```
Generated by [gomarkdoc]()