项目作者: 0x5010

项目描述 :
grpcp is a Grpc Persistent Connection Pool.
高级语言: Go
项目地址: git://github.com/0x5010/grpcp.git
创建时间: 2018-06-20T11:41:47Z
项目社区:https://github.com/0x5010/grpcp

开源协议:MIT License

下载


grpcp is a Grpc Persistent Connection Pool.

LICENSE
Build Status
codecov
Go Report Card
Godoc

Installation

  1. go get -u github.com/0x5010/grpcp

Usage

default

  1. import (
  2. "context"
  3. "fmt"
  4. "google.golang.org/grpc"
  5. pb "google.golang.org/grpc/examples/helloworld/helloworld"
  6. )
  7. func main() {
  8. var addr, name string
  9. conn, _ := grpc.Dial(addr, grpc.WithInsecure())
  10. defer conn.Close()
  11. client := pb.NewGreeterClient(conn)
  12. r, _ := client.SayHello(context.Background(), &pb.HelloRequest{Name: name})
  13. fmt.Println(r.GetMessage())
  14. }

with grpcp

  1. import (
  2. "context"
  3. "fmt"
  4. "google.golang.org/grpc"
  5. pb "google.golang.org/grpc/examples/helloworld/helloworld"
  6. )
  7. func main() {
  8. var addr, name string
  9. conn, _ := grpcp.GetConn(addr) // get conn with grpcp default pool
  10. // defer conn.Close() // no close, close will disconnect
  11. client := pb.NewGreeterClient(conn)
  12. r, _ := client.SayHello(context.Background(), &pb.HelloRequest{Name: name})
  13. fmt.Println(r.GetMessage())
  14. }

custom dial function

  1. import (
  2. "context"
  3. "fmt"
  4. "github.com/0x5010/grpcp"
  5. "google.golang.org/grpc"
  6. pb "google.golang.org/grpc/examples/helloworld/helloworld"
  7. )
  8. func main() {
  9. var addr, name string
  10. pool := grpcp.New(func(addr string) (*grpc.ClientConn, error) {
  11. return grpc.Dial(
  12. addr,
  13. grpc.WithInsecure(),
  14. )
  15. })
  16. conn, _ := pool.GetConn(addr)
  17. client := pb.NewGreeterClient(conn)
  18. r, _ := client.SayHello(context.Background(), &pb.HelloRequest{Name: name})
  19. fmt.Println(r.GetMessage())
  20. }