Skip to content

AKA-bingo/grpcpool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

grpcpool

GoDoc Go Report Card Build Status LICENSE

grpcpool is used to manage and reuse gRPc connections. You can limit the Usage amount for per connections.

Install

go get -u github.com/AKA-bingo/grpcpool

Usage

import "github.com/AKA-bingo/grpcpool"

Example

func main()  {
	
	// Create a new pool
	pool, err := grpcpool.New(func() (*grpc.ClientConn, error) {
		return grpc.Dial(addr, grpc.WithInsecure())
	}, 2, 5, 1000, time.Hour, 0)

	// Error handle
	if err != nil{
		log.Printf("Create gRPC pool:%v\n", err)
	}

	// Release pool
	defer pool.Close()

	// Print the pool info
	log.Println(pool.Print())

	// Get connection from the pool
	conn, err := pool.Get(context.Background())
	if err != nil {
		log.Printf("Get connection fail:%v", err)
	}

	// Get connection from the pool with timeout
	ctx, _ := context.WithDeadline(context.Background(), time.Now().Add(time.Second))
	conn, err := pool.Get(ctx)
	if err != grpcpool.ErrTimeout {
		log.Printf("Time out")
	}

	// Create a gRPC server client by conn
	client := pb.NewgRPCServerClient(conn.ClientConn)
	
	// Put back the Conn
	conn.Put()
  
}

About

A gRPC pool which support HTTP/2 Multiplexing

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages