forked from neo4j-drivers/gobolt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connector.go
306 lines (254 loc) · 8.74 KB
/
connector.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* 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 gobolt
/*
#include <stdlib.h>
#include "bolt/bolt.h"
*/
import "C"
import (
"errors"
"net/url"
"reflect"
"sync"
"sync/atomic"
"time"
"unsafe"
)
// AccessMode is used by the routing driver to decide if a transaction should be routed to a write server
// or a read server in a cluster. When running a transaction, a write transaction requires a server that
// supports writes. A read transaction, on the other hand, requires a server that supports read operations.
// This classification is key for routing driver to route transactions to a cluster correctly.
type AccessMode int
const (
// AccessModeWrite makes the driver return a session towards a write server
AccessModeWrite AccessMode = 0
// AccessModeRead makes the driver return a session towards a follower or a read-replica
AccessModeRead AccessMode = 1
)
// Connector represents an initialised seabolt connector
type Connector interface {
Acquire(mode AccessMode) (Connection, error)
Close() error
}
// RequestHandle identifies an individual request sent to server
type RequestHandle int64
// FetchType identifies the type of the result fetched via Fetch() call
type FetchType int
const (
// FetchTypeRecord tells that fetched data is record
FetchTypeRecord FetchType = 1
// FetchTypeMetadata tells that fetched data is metadata
FetchTypeMetadata = 0
// FetchTypeError tells that fetch was not successful
FetchTypeError = -1
)
var initCounter int32
type neo4jConnector struct {
sync.Mutex
key int
uri *url.URL
authToken map[string]interface{}
config Config
cAddress *C.BoltAddress
cInstance *C.BoltConnector
cLogger *C.struct_BoltLog
cResolver *C.struct_BoltAddressResolver
valueSystem *boltValueSystem
}
func (conn *neo4jConnector) Close() error {
if conn.cInstance != nil {
C.BoltConnector_destroy(conn.cInstance)
conn.cInstance = nil
}
if conn.cLogger != nil {
unregisterLogging(conn.key)
C.BoltLog_destroy(conn.cLogger)
conn.cLogger = nil
}
if conn.cResolver != nil {
unregisterResolver(conn.key)
C.BoltAddressResolver_destroy(conn.cResolver)
conn.cResolver = nil
}
if conn.cAddress != nil {
C.BoltAddress_destroy(conn.cAddress)
conn.cAddress = nil
}
shutdownLibrary()
return nil
}
func (conn *neo4jConnector) Acquire(mode AccessMode) (Connection, error) {
var cMode uint32 = C.BOLT_ACCESS_MODE_WRITE
if mode == AccessModeRead {
cMode = C.BOLT_ACCESS_MODE_READ
}
cStatus := C.BoltStatus_create()
defer C.BoltStatus_destroy(cStatus)
cConnection := C.BoltConnector_acquire(conn.cInstance, C.BoltAccessMode(cMode), cStatus)
if cConnection == nil {
state := C.BoltStatus_get_state(cStatus)
code := C.BoltStatus_get_error(cStatus)
codeText := C.GoString(C.BoltError_get_string(code))
context := C.GoString(C.BoltStatus_get_error_context(cStatus))
return nil, newConnectorError(int(state), int(code), codeText, context, "unable to acquire connection from connector")
}
return &neo4jConnection{connector: conn, cInstance: cConnection, valueSystem: conn.valueSystem}, nil
}
func (conn *neo4jConnector) release(connection *neo4jConnection) error {
C.BoltConnector_release(conn.cInstance, connection.cInstance)
return nil
}
// GetAllocationStats returns statistics about seabolt (C) allocations
func GetAllocationStats() (int64, int64, int64) {
current := C.BoltStat_memory_allocation_current()
peak := C.BoltStat_memory_allocation_peak()
events := C.BoltStat_memory_allocation_events()
return int64(current), int64(peak), int64(events)
}
// NewConnector returns a new connector instance with given parameters
func NewConnector(uri *url.URL, authToken map[string]interface{}, config *Config) (connector Connector, err error) {
if uri == nil {
return nil, errors.New("provided uri should not be nil")
}
if config == nil {
config = &Config{
Encryption: true,
MaxPoolSize: 100,
}
}
cTrust := C.BoltTrust_create()
C.BoltTrust_set_certs(cTrust, nil, 0)
C.BoltTrust_set_skip_verify(cTrust, 0)
C.BoltTrust_set_skip_verify_hostname(cTrust, 0)
certsBuf, err := pemEncodeCerts(config.TLSCertificates)
if err != nil {
return nil, err
}
if certsBuf != nil {
certsBytes := certsBuf.String()
C.BoltTrust_set_certs(cTrust, C.CString(certsBytes), C.uint64_t(certsBuf.Len()))
}
if config.TLSSkipVerify {
C.BoltTrust_set_skip_verify(cTrust, 1)
}
if config.TLSSkipVerifyHostname {
C.BoltTrust_set_skip_verify_hostname(cTrust, 1)
}
cSocketOpts := C.BoltSocketOptions_create()
C.BoltSocketOptions_set_connect_timeout(cSocketOpts, C.int(config.SockConnectTimeout/time.Millisecond))
C.BoltSocketOptions_set_keep_alive(cSocketOpts, 1)
if !config.SockKeepalive {
C.BoltSocketOptions_set_keep_alive(cSocketOpts, 0)
}
valueSystem := createValueSystem(config)
var mode uint32 = C.BOLT_MODE_DIRECT
if uri.Scheme == "bolt+routing" {
mode = C.BOLT_MODE_ROUTING
}
var transport uint32 = C.BOLT_TRANSPORT_PLAINTEXT
if config.Encryption {
transport = C.BOLT_TRANSPORT_ENCRYPTED
}
userAgentStr := C.CString("Go Driver/1.7")
routingContextValue := valueSystem.valueToConnector(uri.Query())
hostnameStr, portStr := C.CString(uri.Hostname()), C.CString(uri.Port())
address := C.BoltAddress_create(hostnameStr, portStr)
authTokenValue := valueSystem.valueToConnector(authToken)
key := startupLibrary()
cLogger := registerLogging(key, config.Log)
cResolver := registerResolver(key, config.AddressResolver)
cConfig := C.BoltConfig_create()
C.BoltConfig_set_mode(cConfig, C.BoltMode(mode))
C.BoltConfig_set_transport(cConfig, C.BoltTransport(transport))
C.BoltConfig_set_trust(cConfig, cTrust)
C.BoltConfig_set_user_agent(cConfig, userAgentStr)
C.BoltConfig_set_routing_context(cConfig, routingContextValue)
C.BoltConfig_set_address_resolver(cConfig, cResolver)
C.BoltConfig_set_log(cConfig, cLogger)
C.BoltConfig_set_max_pool_size(cConfig, C.int(config.MaxPoolSize))
C.BoltConfig_set_max_connection_life_time(cConfig, C.int(config.MaxConnLifetime/time.Millisecond))
C.BoltConfig_set_max_connection_acquisition_time(cConfig, C.int(config.ConnAcquisitionTimeout/time.Millisecond))
C.BoltConfig_set_socket_options(cConfig, cSocketOpts)
cInstance := C.BoltConnector_create(address, authTokenValue, cConfig)
conn := &neo4jConnector{
key: key,
uri: uri,
authToken: authToken,
config: *config,
cAddress: address,
valueSystem: valueSystem,
cInstance: cInstance,
cLogger: cLogger,
}
// do cleanup
C.free(unsafe.Pointer(userAgentStr))
C.free(unsafe.Pointer(hostnameStr))
C.free(unsafe.Pointer(portStr))
C.BoltValue_destroy(routingContextValue)
C.BoltValue_destroy(authTokenValue)
C.BoltTrust_destroy(cTrust)
C.BoltSocketOptions_destroy(cSocketOpts)
C.BoltConfig_destroy(cConfig)
return conn, nil
}
func createValueSystem(config *Config) *boltValueSystem {
valueHandlersBySignature := make(map[int16]ValueHandler, len(config.ValueHandlers))
valueHandlersByType := make(map[reflect.Type]ValueHandler, len(config.ValueHandlers))
for _, handler := range config.ValueHandlers {
for _, readSignature := range handler.ReadableStructs() {
valueHandlersBySignature[readSignature] = handler
}
for _, writeType := range handler.WritableTypes() {
valueHandlersByType[writeType] = handler
}
}
databaseErrorFactory := newDatabaseError
connectorErrorFactory := newConnectorError
genericErrorFactory := newGenericError
if config.DatabaseErrorFactory != nil {
databaseErrorFactory = config.DatabaseErrorFactory
}
if config.ConnectorErrorFactory != nil {
connectorErrorFactory = config.ConnectorErrorFactory
}
if config.GenericErrorFactory != nil {
genericErrorFactory = config.GenericErrorFactory
}
return &boltValueSystem{
valueHandlers: config.ValueHandlers,
valueHandlersBySignature: valueHandlersBySignature,
valueHandlersByType: valueHandlersByType,
connectorErrorFactory: connectorErrorFactory,
databaseErrorFactory: databaseErrorFactory,
genericErrorFactory: genericErrorFactory,
}
}
func startupLibrary() int {
counter := atomic.AddInt32(&initCounter, 1)
if counter == 1 {
C.Bolt_startup()
}
return int(counter)
}
func shutdownLibrary() {
if atomic.AddInt32(&initCounter, -1) == 0 {
C.Bolt_shutdown()
}
}