forked from CovenantSQL/CovenantSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrawcaller.go
More file actions
130 lines (111 loc) · 2.93 KB
/
rawcaller.go
File metadata and controls
130 lines (111 loc) · 2.93 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Copyright 2019 The CovenantSQL Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rpc
import (
"io"
"net"
"net/rpc"
"strings"
"sync"
"github.com/pkg/errors"
)
// PCaller defines generic interface shared with PersistentCaller and RawCaller.
type PCaller interface {
Call(method string, request interface{}, reply interface{}) (err error)
Close()
Target() string
New() PCaller // returns new instance of current caller
}
// RawCaller defines a raw rpc caller without any encryption.
type RawCaller struct {
targetAddr string
client *Client
sync.RWMutex
}
// NewRawCaller creates the raw rpc caller to target node.
func NewRawCaller(targetAddr string) *RawCaller {
return &RawCaller{
targetAddr: targetAddr,
}
}
func (c *RawCaller) isClientValid() bool {
c.RLock()
defer c.RUnlock()
return c.client != nil
}
func (c *RawCaller) resetClient() (err error) {
c.Lock()
defer c.Unlock()
if c.client != nil {
c.client.Close()
c.client = nil
}
var conn net.Conn
if conn, err = net.Dial("tcp", c.targetAddr); err != nil {
err = errors.Wrapf(err, "dial to target %s failed", c.targetAddr)
return
}
if c.client, err = InitClientConn(conn); err != nil {
c.client = nil
err = errors.Wrapf(err, "init client to target %s failed", c.targetAddr)
return
}
return
}
// Call issues client rpc call.
func (c *RawCaller) Call(method string, args interface{}, reply interface{}) (err error) {
if !c.isClientValid() {
if err = c.resetClient(); err != nil {
return
}
}
c.RLock()
err = c.client.Call(method, args, reply)
c.RUnlock()
if err != nil {
if err == io.EOF ||
err == io.ErrUnexpectedEOF ||
err == io.ErrClosedPipe ||
err == rpc.ErrShutdown ||
strings.Contains(strings.ToLower(err.Error()), "shut down") ||
strings.Contains(strings.ToLower(err.Error()), "broken pipe") {
// if got EOF, retry once
reconnectErr := c.resetClient()
if reconnectErr != nil {
err = errors.Wrap(reconnectErr, "reconnect failed")
}
}
err = errors.Wrapf(err, "call %s failed", method)
}
return
}
// Close release underlying connection resources.
func (c *RawCaller) Close() {
c.Lock()
defer c.Unlock()
if c.client != nil {
c.client.Close()
c.client = nil
}
}
// Target returns the request target for logging purpose.
func (c *RawCaller) Target() string {
return c.targetAddr
}
// New returns brand new caller.
func (c *RawCaller) New() PCaller {
return NewRawCaller(c.targetAddr)
}