This repository has been archived by the owner on Sep 11, 2019. It is now read-only.
forked from go-gremlin/gremlin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgremlin_stack.go
105 lines (90 loc) · 2.75 KB
/
gremlin_stack.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
package gremlin
import (
"context"
"time"
"github.com/opentracing/opentracing-go"
"github.com/cbinsights/gremlin/lock"
)
type Gremlin_i interface {
ExecQueryF(ctx context.Context, gremlinQuery GremlinQuery) (response string, err error)
StartMonitor(ctx context.Context, interval time.Duration) (err error)
Close(ctx context.Context) (err error)
}
type GremlinStackOptions struct {
MaxCap int
MaxRetries int
VerboseLogging bool
PingInterval int
Logger Logger_i
Tracer opentracing.Tracer
Instr InstrumentationProvider_i
LockClient lock.LockClient_i
}
func NewGremlinStackSimple(urlStr string, maxCap int, maxRetries int, verboseLogging bool, pingInterval int, options ...OptAuth) (Gremlin_i, error) {
var (
err error
g Gremlin_i
)
var lockClient lock.LockClient_i
lockClient = lock.NewLocalLockClient()
g, err = NewGremlinClient(urlStr, maxCap, maxRetries, verboseLogging, lockClient, options...)
if err != nil {
return nil, err
}
_ = g.StartMonitor(context.Background(), time.Duration(pingInterval))
return g, nil
}
func NewGremlinStack(urlStr string, gremlinStackOptions GremlinStackOptions, authOptions ...OptAuth) (Gremlin_i, error) {
var (
err error
g Gremlin_i
)
maxCap := DEFAULT_MAX_CAP
if gremlinStackOptions.MaxCap != 0 {
maxCap = gremlinStackOptions.MaxCap
}
maxRetries := DEFAULT_MAX_GREMLIN_RETRIES
if gremlinStackOptions.MaxRetries != 0 {
maxRetries = gremlinStackOptions.MaxRetries
}
verboseLogging := DEFAULT_VERBOSE_LOGGING
if gremlinStackOptions.VerboseLogging != false {
verboseLogging = gremlinStackOptions.VerboseLogging
}
pingInterval := DEFAULT_PING_INTERVAL
if gremlinStackOptions.PingInterval != 0 {
pingInterval = gremlinStackOptions.PingInterval
}
var lockClient lock.LockClient_i
if gremlinStackOptions.LockClient != nil {
lockClient = gremlinStackOptions.LockClient
} else {
lockClient = lock.NewLocalLockClient()
}
g, err = NewGremlinClient(urlStr, maxCap, maxRetries, verboseLogging, lockClient, authOptions...)
if err != nil {
return nil, err
}
if gremlinStackOptions.Logger != nil {
g = NewGremlinLogger(g, gremlinStackOptions.Logger)
}
if gremlinStackOptions.Tracer != nil {
g = NewGremlinTracer(g, gremlinStackOptions.Tracer)
}
if gremlinStackOptions.Instr != nil {
g = NewGremlinInstr(g, gremlinStackOptions.Instr)
}
_ = g.StartMonitor(context.Background(), time.Duration(pingInterval))
return g, nil
}
// HELPERS
type contextKey struct{}
var operationNameKey = contextKey{}
func ContextWithOpName(ctx context.Context, name string) context.Context {
return context.WithValue(ctx, operationNameKey, name)
}
func OpNameFromContext(ctx context.Context) string {
val := ctx.Value(operationNameKey)
name, _ := val.(string)
return name
}