forked from hidevopsio/hiboot
/
clientconnector.go
51 lines (46 loc) · 1.45 KB
/
clientconnector.go
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package grpc
import (
"google.golang.org/grpc"
"hidevops.io/hiboot/pkg/factory"
"hidevops.io/hiboot/pkg/log"
"hidevops.io/hiboot/pkg/utils/reflector"
)
// ClientConnector interface is response for creating grpc client connection
type ClientConnector interface {
// Connect connect the gRPC client
Connect(name string, cb interface{}, prop *ClientProperties) (gRPCCli interface{}, err error)
}
type clientConnector struct {
instantiateFactory factory.InstantiateFactory
}
func newClientConnector(instantiateFactory factory.InstantiateFactory) ClientConnector {
cc := &clientConnector{
instantiateFactory: instantiateFactory,
}
return cc
}
// Connect connect to grpc server from client
// name: client name
// clientConstructor: client constructor
// properties: properties for configuring
func (c *clientConnector) Connect(name string, clientConstructor interface{}, properties *ClientProperties) (gRPCCli interface{}, err error) {
host := properties.Host
if host == "" {
host = name
}
address := host + ":" + properties.Port
conn := c.instantiateFactory.GetInstance(name)
if conn == nil {
// connect to grpc server
conn, err = grpc.Dial(address, grpc.WithInsecure())
c.instantiateFactory.SetInstance(name, conn)
if err == nil {
log.Infof("gRPC client connected to: %v", address)
}
}
if err == nil && clientConstructor != nil {
// get return type for register instance name
gRPCCli, err = reflector.CallFunc(clientConstructor, conn)
}
return
}