-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathsession.go
155 lines (137 loc) · 3.79 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright (c) DataStax, Inc.
//
// 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 proxycore
import (
"context"
"errors"
"sync"
"time"
"github.com/datastax/go-cassandra-native-protocol/frame"
"github.com/datastax/go-cassandra-native-protocol/primitive"
"go.uber.org/zap"
)
var (
NoConnForHost = errors.New("no connection available for host")
)
// PreparedEntry is an entry in the prepared cache.
type PreparedEntry struct {
PreparedFrame *frame.RawFrame
}
// PreparedCache a thread-safe cache for storing prepared queries.
type PreparedCache interface {
// Store add an entry to the cache.
Store(id string, entry *PreparedEntry)
// Load retrieves an entry from the cache. `ok` is true if the entry is present; otherwise it's false.
Load(id string) (entry *PreparedEntry, ok bool)
}
type SessionConfig struct {
ReconnectPolicy ReconnectPolicy
NumConns int
Keyspace string
Version primitive.ProtocolVersion
Auth Authenticator
// PreparedCache a global cache share across sessions for storing previously prepared queries
PreparedCache PreparedCache
ConnectTimeout time.Duration
HeartBeatInterval time.Duration
IdleTimeout time.Duration
Logger *zap.Logger
Compression string
}
type Session struct {
ctx context.Context
config SessionConfig
logger *zap.Logger
pools sync.Map
connected chan struct{}
failed chan error
}
func ConnectSession(ctx context.Context, cluster *Cluster, config SessionConfig) (*Session, error) {
session := &Session{
ctx: ctx,
config: config,
logger: GetOrCreateNopLogger(config.Logger),
pools: sync.Map{},
connected: make(chan struct{}),
failed: make(chan error, 1),
}
err := cluster.Listen(session)
if err != nil {
return nil, err
}
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-session.connected:
return session, nil
case err = <-session.failed:
return nil, err
}
}
func (s *Session) Send(host *Host, request Request) error {
conn := s.leastBusyConn(host)
if conn == nil {
return NoConnForHost
}
return conn.Send(request)
}
func (s *Session) leastBusyConn(host *Host) *ClientConn {
if p, ok := s.pools.Load(host.Key()); ok {
pool := p.(*connPool)
return pool.leastBusyConn()
}
return nil
}
func (s *Session) OnEvent(event Event) {
switch evt := event.(type) {
case *BootstrapEvent:
go func() {
var wg sync.WaitGroup
count := len(evt.Hosts)
wg.Add(count)
for _, host := range evt.Hosts {
go func(host *Host) {
pool, err := connectPool(s.ctx, connPoolConfig{
Endpoint: host.Endpoint,
SessionConfig: s.config,
})
if err != nil {
select {
case s.failed <- err:
default:
}
}
s.pools.Store(host.Key(), pool)
wg.Done()
}(host)
}
wg.Wait()
close(s.connected)
}()
case *AddEvent:
// There's no compute if absent for sync.Map, figure a better way to do this if the pool already exists.
if pool, loaded := s.pools.LoadOrStore(evt.Host.Key(), connectPoolNoFail(s.ctx, connPoolConfig{
Endpoint: evt.Host.Endpoint,
SessionConfig: s.config,
})); loaded {
p := pool.(*connPool)
p.cancel()
}
case *RemoveEvent:
if pool, ok := s.pools.LoadAndDelete(evt.Host.Key()); ok {
p := pool.(*connPool)
p.cancel()
}
}
}