-
Notifications
You must be signed in to change notification settings - Fork 2
/
session.go
132 lines (98 loc) · 2.78 KB
/
session.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
package gocql
import (
"context"
"strings"
"github.com/americanas-go/log"
"github.com/gocql/gocql"
)
// Plugin defines a gocql plugin signature.
type Plugin func(context.Context, *gocql.Session) error
// NewSessionWithConfigPath returns a new session with options from config path.
func NewSessionWithConfigPath(ctx context.Context, path string, plugins ...Plugin) (*gocql.Session, error) {
opts, err := NewOptionsWithPath(path)
if err != nil {
return nil, err
}
return NewSessionWithOptions(ctx, opts, plugins...)
}
// NewSession returns a new session with default options.
func NewSession(ctx context.Context, plugins ...Plugin) (*gocql.Session, error) {
o, err := NewOptions()
if err != nil {
return nil, err
}
return NewSessionWithOptions(ctx, o, plugins...)
}
// NewSessionWithOptions returns a new session with options.
func NewSessionWithOptions(ctx context.Context, o *Options, plugins ...Plugin) (session *gocql.Session, err error) {
logger := log.FromContext(ctx)
cluster := gocql.NewCluster(o.Hosts...)
if o.Port > 0 {
cluster.Port = o.Port
}
if o.CQLVersion != "" {
cluster.CQLVersion = o.CQLVersion
}
if o.ProtoVersion > 0 {
cluster.ProtoVersion = o.ProtoVersion
}
if o.Timeout > 0 {
cluster.Timeout = o.Timeout
}
if o.ConnectTimeout > 0 {
cluster.ConnectTimeout = o.ConnectTimeout
}
if o.Keyspace != "" {
cluster.Keyspace = o.Keyspace
}
if o.NumConns > 0 {
cluster.NumConns = o.NumConns
}
if o.SocketKeepalive > 0 {
cluster.SocketKeepalive = o.SocketKeepalive
}
if o.MaxPreparedStmts > 0 {
cluster.MaxPreparedStmts = o.MaxPreparedStmts
}
if o.MaxRoutingKeyInfo > 0 {
cluster.MaxRoutingKeyInfo = o.MaxRoutingKeyInfo
}
if o.PageSize > 0 {
cluster.PageSize = o.PageSize
}
cluster.DefaultTimestamp = o.DefaultTimestamp
if o.ReconnectInterval > 0 {
cluster.ReconnectInterval = o.ReconnectInterval
}
if o.MaxWaitSchemaAgreement > 0 {
cluster.MaxWaitSchemaAgreement = o.MaxWaitSchemaAgreement
}
cluster.DisableInitialHostLookup = o.DisableInitialHostLookup
if o.WriteCoalesceWaitTime > 0 {
cluster.WriteCoalesceWaitTime = o.WriteCoalesceWaitTime
}
if o.Username != "" || o.Password != "" {
cluster.Authenticator = gocql.PasswordAuthenticator{
Username: o.Username,
Password: o.Password,
}
}
if o.Consistency != "" {
cluster.Consistency = gocql.ParseConsistency(o.Consistency)
}
if o.DC != "" {
cluster.PoolConfig.HostSelectionPolicy = gocql.DCAwareRoundRobinPolicy(o.DC)
cluster.HostFilter = gocql.DataCentreHostFilter(o.DC)
}
session, err = cluster.CreateSession()
if err != nil {
return nil, err
}
for _, plugin := range plugins {
if err := plugin(ctx, session); err != nil {
panic(err)
}
}
logger.Infof("Connected to Cassandra server: %v", strings.Join(o.Hosts, ","))
return session, err
}