github.com/jtzjtz/kit@v1.0.2/conn/grpc_pool/README.md (about) 1 ## gRPC 连接池 2 3 #### 配置 4 5 - InitTargets 服务地址 6 - InitCap 初始化容量 7 - MaxCap 最大容量 8 - DialTimeout 连接超时时长 9 - IdleTimeout 空闲超时时长 10 11 #### 导入 12 13 ```go 14 import "github.com/jtzjtz/kit/conn/grpc_pool" 15 ``` 16 #### 初始化 17 18 ```go 19 func main() { 20 options := &grpc_pool.Options{ 21 InitTargets: []string{"127.0.0.1:8001"}, 22 InitCap: 50, 23 MaxCap: 100, 24 DialTimeout: time.Second * 600, 25 IdleTimeout: time.Second * 30, 26 } 27 28 p, err := grpc_pool.NewGRPCPool(options, grpc.WithInsecure()) 29 30 if err != nil { 31 log.Printf("%#v\n", err) 32 return 33 } 34 35 if p == nil { 36 log.Printf("p= %#v\n", p) 37 return 38 } 39 40 defer p.Close() 41 42 conn, err := p.Get() 43 if err != nil { 44 log.Printf("%#v\n", err) 45 return 46 } 47 48 defer p.Put(conn) 49 50 //todo 51 //conn.DoSomething() 52 53 log.Printf("len=%d\n", p.IdleCount()) 54 } 55 ``` 56 57 #### 依赖 58 59 - Goole gRPC:google.golang.org/grpc 60 61 #### 压测 62 63 Docker 环境: 64 65 - 内存 1G 66 - CPU 单核 67 68 wrk 压测工具: 69 70 - -c 跟服务器建立并保持的TCP连接数量 71 - -d 压测时间 72 - -t 使用多少个线程进行压测 73 74 75 调用 helloWorld 服务: 76 77 ``` 78 wrk -c500 -d30s -t4 http://127.0.0.1:8001/test/grpc_pool 79 80 81 Running 30s test @ http://10.70.30.106:8080/hello 82 4 threads and 500 connections 83 Thread Stats Avg Stdev Max +/- Stdev 84 Latency 81.82ms 135.89ms 720.77ms 94.59% 85 Req/Sec 0.99k 452.95 2.18k 79.17% 86 4807 requests in 6.87s, 352.08KB read 87 Socket errors: connect 0, read 500, write 0, timeout 0 88 Requests/sec: 700.04 89 Transfer/sec: 51.27KB 90 ``` 91