-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnection.go
More file actions
37 lines (32 loc) · 774 Bytes
/
Copy pathConnection.go
File metadata and controls
37 lines (32 loc) · 774 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package GrpcConnectionPool
import (
"context"
"google.golang.org/grpc"
"log"
)
type grpcConnection struct {
service string
address string
connection *grpc.ClientConn
}
//创建
func NewGrpcConnection(address, service string) *grpcConnection {
ctx, _ := context.WithTimeout(context.TODO(), MaxWaitTime)
//采用阻塞连接
conn, err := grpc.DialContext(ctx, address, grpc.WithInsecure(), grpc.WithBlock())
defer func() {
if err := recover(); err != nil {
log.Println(err)
}
}()
if err != nil {
panic(err)
}
return &grpcConnection{connection: conn, service: service, address: address}
}
func (this *grpcConnection) close() {
this.connection.Close()
}
func (this *grpcConnection) GetClientConn() *grpc.ClientConn {
return this.connection
}