-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
113 lines (94 loc) · 2.16 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package rpctry
import (
"context"
"crypto/tls"
"net"
"net/rpc"
"sync"
"time"
"github.com/gwaylib/errors"
)
type Client interface {
Close() error
Call(ctx context.Context, serviceMethod string, args interface{}, reply interface{}) error
// Call rpc server timeout with 30s
TryCall(serviceMethod string, args interface{}, reply interface{}) error
}
type rpcClient struct {
addr string
tlsCfg *tls.Config
mux sync.Mutex
connected bool
client *rpc.Client
}
func NewClient(addr string) Client {
return &rpcClient{
addr: addr,
}
}
func NewTlsClient(addr string, config *tls.Config) Client {
return &rpcClient{
addr: addr,
tlsCfg: config,
}
}
func (rc *rpcClient) conn() (*rpc.Client, error) {
rc.mux.Lock()
defer rc.mux.Unlock()
if rc.connected {
return rc.client, nil
}
conn, err := net.DialTimeout("tcp", rc.addr, 10*time.Second)
if err != nil {
return nil, errors.As(err, rc.addr)
}
if rc.tlsCfg != nil {
rc.client = rpc.NewClient(tls.Client(conn, rc.tlsCfg))
} else {
rc.client = rpc.NewClient(conn)
}
rc.connected = true
return rc.client, nil
}
func (rc *rpcClient) disconn() error {
rc.mux.Lock()
defer rc.mux.Unlock()
if rc.connected {
rc.connected = false
return rc.client.Close()
}
return nil
}
func (rc *rpcClient) call(ctx context.Context, method string, args interface{}, reply interface{}) error {
retried := false
_retry:
client, err := rc.conn()
if err != nil {
return err
}
select {
case call := <-client.Go(method, args, reply, make(chan *rpc.Call, 1)).Done:
err := call.Error
if errors.Equal(rpc.ErrShutdown, err) {
rc.disconn()
if !retried {
retried = true
goto _retry
}
}
return errors.As(err)
case <-ctx.Done():
return ctx.Err()
}
}
func (rc *rpcClient) Close() error {
return rc.disconn()
}
func (rc *rpcClient) Call(ctx context.Context, method string, args interface{}, reply interface{}) error {
return rc.call(ctx, method, args, reply)
}
func (rc *rpcClient) TryCall(method string, args interface{}, reply interface{}) error {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
return rc.call(ctx, method, args, reply)
}