forked from vesoft-inc/nebula-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
130 lines (115 loc) · 3.83 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
/* Copyright (c) 2020 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License,
* attached with Common Clause Condition 1.0, found in the LICENSES directory.
*/
package nebula_go
import (
"fmt"
"math"
"time"
"github.com/facebook/fbthrift/thrift/lib/go/thrift"
"github.com/vesoft-inc/nebula-go/v2/nebula"
"github.com/vesoft-inc/nebula-go/v2/nebula/graph"
)
type connection struct {
severAddress HostAddress
timeout time.Duration
returnedAt time.Time // the connection was created or returned.
graph *graph.GraphServiceClient
}
func newConnection(severAddress HostAddress) *connection {
return &connection{
severAddress: severAddress,
timeout: 0 * time.Millisecond,
returnedAt: time.Now(),
graph: nil,
}
}
func (cn *connection) open(hostAddress HostAddress, timeout time.Duration) error {
ip := hostAddress.Host
port := hostAddress.Port
newAdd := fmt.Sprintf("%s:%d", ip, port)
cn.timeout = timeout
timeoutOption := thrift.SocketTimeout(timeout)
bufferSize := 128 << 10
frameMaxLength := uint32(math.MaxUint32)
addressOption := thrift.SocketAddr(newAdd)
sock, err := thrift.NewSocket(timeoutOption, addressOption)
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 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)
}
// 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) {
resp, err := cn.graph.Execute(sessionID, []byte(stmt))
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.Execute(sessionID, []byte(stmt))
}
}
}
return resp, err
}
// unsupported
// func (client *GraphClient) ExecuteJson((sessionID int64, stmt string) (*graph.ExecutionResponse, error) {
// return cn.graph.ExecuteJson(sessionID, []byte(stmt))
// }
// Check connection to host address
func (cn *connection) ping() bool {
_, err := cn.execute(0, "YIELD 1")
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()
}