This repository has been archived by the owner on Oct 25, 2024. It is now read-only.
forked from schwartzmx/gremtune
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathclient.go
513 lines (422 loc) · 13.4 KB
/
client.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
package gremcos
import (
"fmt"
"io/ioutil"
"log"
"sync"
"sync/atomic"
"time"
"github.com/pkg/errors"
"github.com/supplyon/gremcos/interfaces"
)
// socketClosedByServerError is not really an error since this usually happens when the socket is closed by the peer.
// But in order to support the workflow of message processing as implemented in gremcos we need a error type here.
type socketClosedByServerError struct {
err error
}
func (socketClosedErr socketClosedByServerError) Error() string {
detailErrMsg := ""
if socketClosedErr.err != nil {
detailErrMsg = socketClosedErr.err.Error()
}
return fmt.Sprintf("received msgType == -1 this is no frame, closing the readworker %s", detailErrMsg)
}
// client is a container for the gremcos client.
type client struct {
// conn is the entity that manages the websocket connection
conn interfaces.Dialer
// requests takes any request and delivers it to the WriteWorker for dispatch to Gremlin Server
requests chan []byte
// results is a container for the responses received from the peer.
// The responses are stored per request id.
// For each request (Id) there might be 0..n responses.
// <RequestID string,responses []Response>
results *sync.Map
// responseNotifier notifies the requester that a response has arrived for a specific request
// <RequestID string,errorChannel chan error>
// As notification object error is used.
// In case of an error is sent on the channel.
// !!! In case there is new (unprocessed) data available, nil is sent on the channel.
responseNotifier *sync.Map
// responseStatusNotifier notifies the requester that a response has arrived for a specific request with a specific (http like) status code
// <RequestID string,codeChannel chan int>
responseStatusNotifier *sync.Map
// stores the most recent error
lastError atomic.Value
credentialProvider CredentialProvider
// pingInterval is the interval that is used to check if the connection
// is still alive. The interval to send the ping frame to the peer.
pingInterval time.Duration
wg sync.WaitGroup
mux sync.RWMutex
// quitChannel channel to notify workers that they should stop
quitChannel chan struct{}
// token to ensure that the resources are closed only once
// even if client.Close() is called multiple times
once sync.Once
metrics clientMetrics
}
// clientOption is the struct for defining optional parameters for the Client
type clientOption func(*client)
// SetAuth sets credentials provider for an authenticated connection
func SetAuth(credentialProvider CredentialProvider) clientOption {
return func(c *client) {
c.credentialProvider = credentialProvider
}
}
// PingInterval sets the ping interval, which is the interval to send the ping frame to the peer
func PingInterval(interval time.Duration) clientOption {
return func(c *client) {
c.pingInterval = interval
}
}
// WithMetrics sets the metrics provider
func WithMetrics(metrics clientMetrics) clientOption {
return func(c *client) {
c.metrics = metrics
}
}
func newClient(dialer interfaces.Dialer, options ...clientOption) *client {
client := &client{
conn: dialer,
requests: make(chan []byte, 3),
results: &sync.Map{},
responseNotifier: &sync.Map{},
responseStatusNotifier: &sync.Map{},
pingInterval: 60 * time.Second,
quitChannel: make(chan struct{}),
credentialProvider: noCredentials{},
metrics: &clientMetricsNop{},
}
for _, opt := range options {
opt(client)
}
return client
}
// Dial returns a client for interaction with the Gremlin Server specified in the host IP.
// The client is already connected.
func Dial(conn interfaces.Dialer, errorChannel chan error, options ...clientOption) (*client, error) {
if conn == nil {
return nil, fmt.Errorf("dialer is nil")
}
client := newClient(conn, options...)
err := client.conn.Connect()
if err != nil {
client.metrics.incrementConnectivityErrorCount()
return nil, errors.Wrapf(err, "dialer connecting")
}
// Start all worker (run async)
client.wg.Add(3)
go client.writeWorker(errorChannel, client.quitChannel)
go client.readWorker(errorChannel, client.quitChannel)
go client.pingWorker(errorChannel, client.quitChannel)
return client, nil
}
// errContainer allows to store different error types inside a atomic.Value
type errContainer struct {
err error
}
func (c *client) setLastErr(err error) {
if err == nil {
return
}
previousErr := c.lastError.Load()
if previousErr != nil {
errCont := toErrContainer(previousErr)
err = errors.Wrapf(err, "previous error: %s", errCont.err)
}
c.lastError.Store(errContainer{err: err})
}
// toErrContainer converts the given interface type to an errContainer and panics if the type does not match
func toErrContainer(err interface{}) errContainer {
errCont, ok := err.(errContainer)
if !ok {
panic(fmt.Sprintf("error of wrong type (%T) detected as last error", err))
}
return errCont
}
func (c *client) LastError() error {
err := c.lastError.Load()
if err == nil {
return nil
}
errCont := toErrContainer(err)
return errCont.err
}
func (c *client) IsConnected() bool {
return c.conn.IsConnected()
}
func (c *client) executeRequest(query string, bindings, rebindings *map[string]interface{}) ([]interfaces.Response, error) {
var req request
var id string
var err error
if bindings != nil && rebindings != nil {
req, id, err = prepareRequestWithBindings(query, *bindings, *rebindings)
} else {
req, id, err = prepareRequest(query)
}
if err != nil {
return nil, err
}
msg, err := packageRequest(req)
if err != nil {
return nil, err
}
c.responseNotifier.Store(id, newSafeCloseErrorChannel(1))
c.responseStatusNotifier.Store(id, newSafeCloseIntChannel(1))
c.dispatchRequest(msg)
// this call blocks until the response has been retrieved from the server
resp, err := c.retrieveResponse(id)
if err != nil {
err = errors.Wrapf(err, "query: %s", query)
}
return resp, err
}
func (c *client) executeAsync(query string, bindings, rebindings *map[string]interface{}, responseChannel chan interfaces.AsyncResponse) (err error) {
var req request
var id string
if bindings != nil && rebindings != nil {
req, id, err = prepareRequestWithBindings(query, *bindings, *rebindings)
} else {
req, id, err = prepareRequest(query)
}
if err != nil {
return
}
msg, err := packageRequest(req)
if err != nil {
log.Println(err)
return
}
c.responseNotifier.Store(id, newSafeCloseErrorChannel(1))
c.responseStatusNotifier.Store(id, newSafeCloseIntChannel(1))
c.dispatchRequest(msg)
go c.retrieveResponseAsync(id, responseChannel)
return
}
func validateCredentials(username string, password string) error {
if len(username) == 0 {
return fmt.Errorf("username is missing")
}
if len(password) == 0 {
return fmt.Errorf("password is missing")
}
return nil
}
func (c *client) authenticate(requestID string) error {
username, err := c.credentialProvider.Username()
if err != nil {
return errors.Wrap(err, "obtaining username")
}
password, err := c.credentialProvider.Password()
if err != nil {
return errors.Wrap(err, "obtaining password")
}
if err := validateCredentials(username, password); err != nil {
return err
}
req := prepareAuthRequest(requestID, username, password)
msg, err := packageRequest(req)
if err != nil {
log.Println(err)
return err
}
c.dispatchRequest(msg)
return nil
}
// ExecuteWithBindings formats a raw Gremlin query, sends it to Gremlin Server, and returns the result.
func (c *client) ExecuteWithBindings(query string, bindings, rebindings map[string]interface{}) (resp []interfaces.Response, err error) {
if !c.conn.IsConnected() {
return resp, ErrNoConnection
}
resp, err = c.executeRequest(query, &bindings, &rebindings)
return
}
// Execute formats a raw Gremlin query, sends it to Gremlin Server, and returns the result.
func (c *client) Execute(query string) (resp []interfaces.Response, err error) {
if !c.conn.IsConnected() {
return resp, ErrNoConnection
}
resp, err = c.executeRequest(query, nil, nil)
return
}
// ExecuteAsync formats a raw Gremlin query, sends it to Gremlin Server, and the results are streamed to channel provided in method parameter.
func (c *client) ExecuteAsync(query string, responseChannel chan interfaces.AsyncResponse) (err error) {
if !c.conn.IsConnected() {
return ErrNoConnection
}
err = c.executeAsync(query, nil, nil, responseChannel)
return
}
// ExecuteFileWithBindings takes a file path to a Gremlin script, sends it to Gremlin Server with bindings, and returns the result.
func (c *client) ExecuteFileWithBindings(path string, bindings, rebindings map[string]interface{}) (resp []interfaces.Response, err error) {
if !c.conn.IsConnected() {
return resp, ErrNoConnection
}
d, err := ioutil.ReadFile(path) // Read script from file
if err != nil {
log.Println(err)
return
}
query := string(d)
resp, err = c.executeRequest(query, &bindings, &rebindings)
return
}
// ExecuteFile takes a file path to a Gremlin script, sends it to Gremlin Server, and returns the result.
func (c *client) ExecuteFile(path string) (resp []interfaces.Response, err error) {
if !c.conn.IsConnected() {
return resp, ErrNoConnection
}
d, err := ioutil.ReadFile(path) // Read script from file
if err != nil {
log.Println(err)
return
}
query := string(d)
resp, err = c.executeRequest(query, nil, nil)
return
}
// Close closes the underlying connection and marks the client as closed.
func (c *client) Close() error {
err := c.safeClose()
// wait for cleanup of all started go routines
c.wg.Wait()
return err
}
// safeClose encapsulates the cleanup logic that enables failed workers to clean up after them. It is called by the deferred workerSaveExit
func (c *client) safeClose() error {
var err error
// ensure that the channels are only closed once
c.once.Do(func() {
// notify the workers to stop working
close(c.quitChannel)
c.responseNotifier.Range(func(key, value interface{}) bool {
channel := value.(*safeCloseErrorChannel)
channel.Close()
return true
})
c.responseStatusNotifier.Range(func(key, value interface{}) bool {
channel := value.(*safeCloseIntChannel)
channel.Close()
return true
})
c.mux.Lock()
defer c.mux.Unlock()
if c.conn == nil {
err = fmt.Errorf("connection is nil")
} else {
err = c.conn.Close()
}
})
return err
}
// writeWorker works on a loop and dispatches messages as soon as it receives them
func (c *client) writeWorker(errs chan<- error, quit <-chan struct{}) {
defer c.workerSaveExit("writeWorker", errs, quit)
for {
select {
case msg := <-c.requests:
c.mux.Lock()
err := c.conn.Write(msg)
if err != nil {
c.metrics.incrementConnectionUsageCount(connectionUsageKindWrite, true)
c.postError(errs, err, quit)
c.mux.Unlock()
break
}
c.metrics.incrementConnectionUsageCount(connectionUsageKindWrite, false)
c.mux.Unlock()
case <-quit:
return
}
}
}
// readWorker works on a loop and sorts messages as soon as it receives them
func (c *client) readWorker(errs chan<- error, quit <-chan struct{}) {
defer c.workerSaveExit("readWorker", errs, quit)
for {
msgType, msg, err := c.conn.Read()
if msgType == -1 { // msgType == -1 is noFrame (close connection)
closedErr := socketClosedByServerError{err: err}
c.postError(errs, closedErr, quit)
// to return at this point is safe since we call workerSaveExit() to clean up everything
// when the function is left
return
}
var errorToPost error
if err != nil {
errorToPost = err
} else if msg == nil {
errorToPost = fmt.Errorf("receive message type: %d, but message was nil", msgType)
} else {
// handle the message
errorToPost = c.handleResponse(msg)
}
if errorToPost != nil {
c.metrics.incrementConnectionUsageCount(connectionUsageKindRead, true)
c.postError(errs, errorToPost, quit)
// to return at this point is safe since we call workerSaveExit() to clean up everything
// when the function is left
return
}
c.metrics.incrementConnectionUsageCount(connectionUsageKindRead, false)
// check if we're shutting down
select {
case <-quit:
return
default:
continue
}
}
}
// postError posts the error to the client if no shutdown is already initiated
func (c *client) postError(errs chan<- error, errToPost error, close <-chan struct{}) {
if errToPost == nil {
return
}
select {
case <-close:
return
default:
errs <- errToPost
c.setLastErr(errToPost)
}
}
func (c *client) pingWorker(errs chan<- error, quit <-chan struct{}) {
ticker := time.NewTicker(c.pingInterval)
defer func() {
ticker.Stop()
c.workerSaveExit("pingWorker", errs, quit)
}()
for {
select {
case <-ticker.C:
if err := c.Ping(); err != nil {
c.postError(errs, err, quit)
}
case <-quit:
return
}
}
}
// workerSaveExit can be used as deferred call on leaving a worker routine.
// It ensures that the client is closed and cleaned up appropriately.
func (c *client) workerSaveExit(name string, errs chan<- error, quit <-chan struct{}) {
// call close to ensure that everything is cleaned up appropriately
if err := c.safeClose(); err != nil {
err = errors.Wrapf(err, "error closing client while leaving worker '%s'", name)
c.postError(errs, err, quit)
}
// client exited
c.wg.Done()
}
// Ping sends a ping over the socket to the peer
func (c *client) Ping() error {
wasAnError := false
err := c.conn.Ping()
if err != nil {
wasAnError = true
}
c.metrics.incrementConnectionUsageCount(connectionUsageKindPing, wasAnError)
return err
}