/
client.go
53 lines (42 loc) · 1.03 KB
/
client.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
52
53
package interop
import (
fmt "fmt"
"time"
"github.com/tendermint/tendermint/libs/log"
grpc "google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
const (
FlagInteropNodePort = "interop-node-port"
ReconnectInterval = 10 * time.Second
ErrInteropClientNotReady = "interop client not ready"
)
type InteropClientFactory struct {
conn *grpc.ClientConn
}
func NewInteropClientFactory(log log.Logger, port uint16) *InteropClientFactory {
interopClient := &InteropClientFactory{
conn: nil,
}
go func() {
target := fmt.Sprintf("localhost:%d", port)
option := grpc.WithTransportCredentials(insecure.NewCredentials())
for {
conn, err := grpc.Dial(target, option)
if err != nil {
log.Error("failed to connect to interop node", "err", err)
time.Sleep(ReconnectInterval)
continue
}
interopClient.conn = conn
return
}
}()
return interopClient
}
func (w *InteropClientFactory) GetInteropClient() InteropClient {
if w.conn == nil {
return nil
}
return NewInteropClient(w.conn)
}