Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cr-mao/grpc-conn-pool
go grpc client conn pool
https://github.com/cr-mao/grpc-conn-pool
go-grpc-conn-pool grpc-client-pool grpc-conn-pool
Last synced: 6 days ago
JSON representation
go grpc client conn pool
- Host: GitHub
- URL: https://github.com/cr-mao/grpc-conn-pool
- Owner: cr-mao
- License: mit
- Created: 2023-08-27T02:15:50.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2023-08-27T02:38:34.000Z (about 1 year ago)
- Last Synced: 2023-09-05T04:26:59.937Z (about 1 year ago)
- Topics: go-grpc-conn-pool, grpc-client-pool, grpc-conn-pool
- Language: Go
- Homepage:
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## go grpc 连接池
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Go Reference](https://pkg.go.dev/badge/github.com/cr-mao/grpc-conn-pool.svg)](https://pkg.go.dev/github.com/cr-mao/grpc-conn-pool)golang grpc client pool
```shell
go get github.com/cr-mao/[email protected]
```### 特点
- 支持多target,pool.
- 连接检测,失效,才进行替换连接
- 获得一次连接耗时 90 op/ns 左右(15年mac pro)### 使用
```go
package grpc_conn_pool_testimport (
"net"
"testing"
"time""github.com/cr-mao/grpc-conn-pool"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)func Test_GrpcConnPool(t *testing.T) {
target := "127.0.0.1:13688"
go func() {
clientBuilder := grpc_conn_pool.NewClientBuilder(&grpc_conn_pool.Options{
PoolSize: 20,
DialOpts: []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
},
})i := 0
begin := time.Now()
for {
conn, err := clientBuilder.GetConn(target)
if err != nil {
t.Errorf("get conn err %v", err)
return
}
err = grpc_conn_pool.CheckState(conn)
if err != nil {
t.Log(conn.GetState())
}
//time.Sleep(time.Millisecond * 2)
//t.Log("ok")
i++
if i > 1000000 {
break
}
}
cost := time.Now().Sub(begin).Nanoseconds()
t.Log(cost)
t.Logf("%d ns/op", cost/1000000)
}()addr := "0.0.0.0:13688"
lis, err := net.Listen("tcp", addr)
if err != nil {
t.Fatalf("Listen err :%v", err)
}
t.Logf("grpc serve at: %s", addr)
server := grpc.NewServer()
_ = server.Serve(lis)}
```