forked from vesoft-inc/nebula-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
connection.go
183 lines (161 loc) · 5.45 KB
/
connection.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
*
* Copyright (c) 2020 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License.
*
*/
package nebula_go
import (
"crypto/tls"
"fmt"
"math"
"time"
"github.com/facebook/fbthrift/thrift/lib/go/thrift"
"github.com/vesoft-inc/nebula-go/v3/nebula"
"github.com/vesoft-inc/nebula-go/v3/nebula/graph"
)
type connection struct {
severAddress HostAddress
timeout time.Duration
returnedAt time.Time // the connection was created or returned.
sslConfig *tls.Config
graph *graph.GraphServiceClient
}
func newConnection(severAddress HostAddress) *connection {
return &connection{
severAddress: severAddress,
timeout: 0 * time.Millisecond,
returnedAt: time.Now(),
sslConfig: nil,
graph: nil,
}
}
// open opens a transport for the connection
// if sslConfig is not nil, an SSL transport will be created
func (cn *connection) open(hostAddress HostAddress, timeout time.Duration, sslConfig *tls.Config) error {
ip := hostAddress.Host
port := hostAddress.Port
newAdd := fmt.Sprintf("%s:%d", ip, port)
cn.timeout = timeout
bufferSize := 128 << 10
frameMaxLength := uint32(math.MaxUint32)
var err error
var sock thrift.Transport
if sslConfig != nil {
sock, err = thrift.NewSSLSocketTimeout(newAdd, sslConfig, timeout)
} else {
sock, err = thrift.NewSocket(thrift.SocketAddr(newAdd), thrift.SocketTimeout(timeout))
}
if err != nil {
return fmt.Errorf("failed to create a net.Conn-backed Transport,: %s", err.Error())
}
// Set transport buffer
bufferedTranFactory := thrift.NewBufferedTransportFactory(bufferSize)
transport := thrift.NewFramedTransportMaxLength(bufferedTranFactory.GetTransport(sock), frameMaxLength)
pf := thrift.NewBinaryProtocolFactoryDefault()
cn.graph = graph.NewGraphServiceClientFactory(transport, pf)
if err = cn.graph.Open(); err != nil {
return fmt.Errorf("failed to open transport, error: %s", err.Error())
}
if !cn.graph.IsOpen() {
return fmt.Errorf("transport is off")
}
return cn.verifyClientVersion()
}
func (cn *connection) verifyClientVersion() error {
req := graph.NewVerifyClientVersionReq()
resp, err := cn.graph.VerifyClientVersion(req)
if err != nil {
cn.close()
return fmt.Errorf("failed to verify client version: %s", err.Error())
}
if resp.GetErrorCode() != nebula.ErrorCode_SUCCEEDED {
return fmt.Errorf("incompatible version between client and server: %s", string(resp.GetErrorMsg()))
}
return nil
}
// reopen reopens the current connection.
// Because the code generated by Fbthrift does not handle the seqID,
// the message will be dislocated when the timeout occurs, resulting in unexpected response.
// When the timeout occurs, the connection will be reopened to avoid the impact of the message.
func (cn *connection) reopen() error {
cn.close()
return cn.open(cn.severAddress, cn.timeout, cn.sslConfig)
}
// Authenticate
func (cn *connection) authenticate(username, password string) (*graph.AuthResponse, error) {
resp, err := cn.graph.Authenticate([]byte(username), []byte(password))
if err != nil {
err = fmt.Errorf("authentication fails, %s", err.Error())
if e := cn.graph.Close(); e != nil {
err = fmt.Errorf("fail to close transport, error: %s", e.Error())
}
return nil, err
}
if resp.ErrorCode != nebula.ErrorCode_SUCCEEDED {
return nil, fmt.Errorf("fail to authenticate, error: %s", resp.ErrorMsg)
}
return resp, err
}
func (cn *connection) execute(sessionID int64, stmt string) (*graph.ExecutionResponse, error) {
return cn.executeWithParameter(sessionID, stmt, map[string]*nebula.Value{})
}
func (cn *connection) executeWithParameter(sessionID int64, stmt string, params map[string]*nebula.Value) (*graph.ExecutionResponse, error) {
resp, err := cn.graph.ExecuteWithParameter(sessionID, []byte(stmt), params)
if err != nil {
// reopen the connection if timeout
if _, ok := err.(thrift.TransportException); ok {
if err.(thrift.TransportException).TypeID() == thrift.TIMED_OUT {
reopenErr := cn.reopen()
if reopenErr != nil {
return nil, reopenErr
}
return cn.graph.ExecuteWithParameter(sessionID, []byte(stmt), params)
}
}
}
return resp, err
}
func (cn *connection) executeJson(sessionID int64, stmt string) ([]byte, error) {
return cn.ExecuteJsonWithParameter(sessionID, stmt, map[string]*nebula.Value{})
}
func (cn *connection) ExecuteJsonWithParameter(sessionID int64, stmt string, params map[string]*nebula.Value) ([]byte, error) {
jsonResp, err := cn.graph.ExecuteJsonWithParameter(sessionID, []byte(stmt), params)
if err != nil {
// reopen the connection if timeout
if _, ok := err.(thrift.TransportException); ok {
if err.(thrift.TransportException).TypeID() == thrift.TIMED_OUT {
reopenErr := cn.reopen()
if reopenErr != nil {
return nil, reopenErr
}
return cn.graph.ExecuteJsonWithParameter(sessionID, []byte(stmt), params)
}
}
}
return jsonResp, err
}
// Check connection to host address
func (cn *connection) ping() bool {
_, err := cn.execute(0, "YIELD 1")
return err == nil
}
// Check connection to host address
func (cn *connection) pingWithParameter() bool {
_, err := cn.executeWithParameter(0, "YIELD 1", nil)
return err == nil
}
// Sign out and release seesin ID
func (cn *connection) signOut(sessionID int64) error {
// Release session ID to graphd
return cn.graph.Signout(sessionID)
}
// Update returnedAt for cleaner
func (cn *connection) release() {
cn.returnedAt = time.Now()
}
// Close transport
func (cn *connection) close() {
cn.graph.Close()
}