This repository has been archived by the owner on Aug 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
connection_worker.go
355 lines (279 loc) · 7.22 KB
/
connection_worker.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
* Copyright (c) 2002-2019 "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
import (
"sync/atomic"
"time"
)
type job func()
type workerConnection struct {
connector *workerConnector
pool *workerPool
delegate Connection
active int32
}
var newWorkerConnection = func(connector *workerConnector, mode AccessMode) (*workerConnection, error) {
var err error
var startTime time.Time
var delegate *seaboltConnection
var connection *workerConnection
connection = &workerConnection{
connector: connector,
pool: connector.pool,
delegate: nil,
active: 0,
}
for connection.delegate == nil {
if startTime.IsZero() {
startTime = time.Now()
} else if time.Since(startTime) > connector.config.ConnAcquisitionTimeout {
return nil, newConnectionAcquisitionTimedOutError(connector.delegate.valueSystem)
}
if poolError := connection.queueJob(func() {
delegate, err = newSeaboltConnection(connector.delegate, mode)
}); poolError != nil {
err = poolError
}
if err != nil {
if isPoolFullError(err) && connector.config.ConnAcquisitionTimeout > 0 {
if waitClosed(connection, connector.config.ConnAcquisitionTimeout-time.Since(startTime)) {
continue
} else {
return nil, newConnectionAcquisitionTimedOutError(connector.delegate.valueSystem)
}
}
return nil, err
}
connection.delegate = delegate
}
return connection, nil
}
var waitClosed = func(w *workerConnection, timeout time.Duration) bool {
if w.connector != nil {
select {
case <-w.connector.closeSignal:
return true
case <-time.After(timeout):
}
}
return false
}
var signalClosed = func(w *workerConnection) {
if w.connector != nil {
select {
case w.connector.closeSignal <- signal{}:
default:
}
}
}
func (w *workerConnection) queueJob(item job) error {
if atomic.CompareAndSwapInt32(&w.active, 0, 1) {
defer atomic.StoreInt32(&w.active, 0)
var done = make(chan bool, 1)
defer close(done)
if err := w.pool.submit(func(stopper <-chan signal) {
item()
done <- true
}); err != nil {
return err
}
<-done
return nil
}
return newGenericError("a connection is not thread-safe and thus should not be used concurrently")
}
func (w *workerConnection) Id() (string, error) {
var id string
var err error
if otherErr := w.queueJob(func() {
id, err = w.delegate.Id()
}); otherErr != nil {
err = otherErr
}
return id, err
}
func (w *workerConnection) RemoteAddress() (string, error) {
var remoteAddress string
var err error
if otherErr := w.queueJob(func() {
remoteAddress, err = w.delegate.RemoteAddress()
}); otherErr != nil {
err = otherErr
}
return remoteAddress, err
}
func (w *workerConnection) Server() (string, error) {
var server string
var err error
if otherErr := w.queueJob(func() {
server, err = w.delegate.Server()
}); otherErr != nil {
err = otherErr
}
return server, err
}
func (w *workerConnection) Begin(bookmarks []string, txTimeout time.Duration, txMetadata map[string]interface{}) (RequestHandle, error) {
var handle RequestHandle
var err error
if otherErr := w.queueJob(func() {
handle, err = w.delegate.Begin(bookmarks, txTimeout, txMetadata)
}); otherErr != nil {
err = otherErr
}
return handle, err
}
func (w *workerConnection) Commit() (RequestHandle, error) {
var handle RequestHandle
var err error
if otherErr := w.queueJob(func() {
handle, err = w.delegate.Commit()
}); otherErr != nil {
err = otherErr
}
return handle, err
}
func (w *workerConnection) Rollback() (RequestHandle, error) {
var handle RequestHandle
var err error
if otherErr := w.queueJob(func() {
handle, err = w.delegate.Rollback()
}); otherErr != nil {
err = otherErr
}
return handle, err
}
func (w *workerConnection) Run(cypher string, args map[string]interface{}, bookmarks []string, txTimeout time.Duration, txMetadata map[string]interface{}) (RequestHandle, error) {
var handle RequestHandle
var err error
if otherErr := w.queueJob(func() {
handle, err = w.delegate.Run(cypher, args, bookmarks, txTimeout, txMetadata)
}); otherErr != nil {
err = otherErr
}
return handle, err
}
func (w *workerConnection) PullAll() (RequestHandle, error) {
var handle RequestHandle
var err error
if otherErr := w.queueJob(func() {
handle, err = w.delegate.PullAll()
}); otherErr != nil {
err = otherErr
}
return handle, err
}
func (w *workerConnection) DiscardAll() (RequestHandle, error) {
var handle RequestHandle
var err error
if otherErr := w.queueJob(func() {
handle, err = w.delegate.DiscardAll()
}); otherErr != nil {
err = otherErr
}
return handle, err
}
func (w *workerConnection) Reset() (RequestHandle, error) {
var handle RequestHandle
var err error
if otherErr := w.queueJob(func() {
handle, err = w.delegate.Reset()
}); otherErr != nil {
err = otherErr
}
return handle, err
}
func (w *workerConnection) Flush() error {
var err error
if otherErr := w.queueJob(func() {
err = w.delegate.Flush()
}); otherErr != nil {
err = otherErr
}
return err
}
func (w *workerConnection) Fetch(request RequestHandle) (FetchType, error) {
var fetched FetchType
var err error
if otherErr := w.queueJob(func() {
fetched, err = w.delegate.Fetch(request)
}); otherErr != nil {
err = otherErr
}
return fetched, err
}
func (w *workerConnection) FetchSummary(request RequestHandle) (int, error) {
var fetched int
var err error
if otherErr := w.queueJob(func() {
fetched, err = w.delegate.FetchSummary(request)
}); otherErr != nil {
err = otherErr
}
return fetched, err
}
func (w *workerConnection) LastBookmark() (string, error) {
var bookmark string
var err error
if otherErr := w.queueJob(func() {
bookmark, err = w.delegate.LastBookmark()
}); otherErr != nil {
err = otherErr
}
return bookmark, err
}
func (w *workerConnection) Fields() ([]string, error) {
var fields []string
var err error
if otherErr := w.queueJob(func() {
fields, err = w.delegate.Fields()
}); otherErr != nil {
err = otherErr
}
return fields, err
}
func (w *workerConnection) Metadata() (map[string]interface{}, error) {
var metadata map[string]interface{}
var err error
if otherErr := w.queueJob(func() {
metadata, err = w.delegate.Metadata()
}); otherErr != nil {
err = otherErr
}
return metadata, err
}
func (w *workerConnection) Data() ([]interface{}, error) {
var data []interface{}
var err error
if otherErr := w.queueJob(func() {
data, err = w.delegate.Data()
}); otherErr != nil {
err = otherErr
}
return data, err
}
func (w *workerConnection) Close() error {
var err error
if otherErr := w.queueJob(func() {
err = w.delegate.Close()
}); otherErr != nil {
err = otherErr
}
signalClosed(w)
return err
}