forked from CovenantSQL/CovenantSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpool.go
More file actions
207 lines (180 loc) · 4.32 KB
/
pool.go
File metadata and controls
207 lines (180 loc) · 4.32 KB
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
/*
* Copyright 2018 The CovenantSQL Authors.
*
* 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 rpc
import (
"net"
"sync"
"github.com/pkg/errors"
mux "github.com/xtaci/smux"
"github.com/CovenantSQL/CovenantSQL/conf"
"github.com/CovenantSQL/CovenantSQL/proto"
)
// SessPool is the session pool interface.
type SessPool interface {
Get(proto.NodeID) (net.Conn, error)
Close()
Len() int
}
// NodeDialer is the dialer handler.
type NodeDialer func(nodeID proto.NodeID) (net.Conn, error)
// SessionMap is the map from node id to session.
type SessionMap map[proto.NodeID]*Session
// Session is the Session type of SessionPool.
type Session struct {
sync.RWMutex
nodeDialer NodeDialer
target proto.NodeID
sess []*mux.Session
offset int
}
// SessionPool is the struct type of session pool.
type SessionPool struct {
sync.RWMutex
sessions SessionMap
nodeDialer NodeDialer
}
var (
instance *SessionPool
once sync.Once
)
// Close closes the session.
func (s *Session) Close() {
s.Lock()
defer s.Unlock()
for _, s := range s.sess {
_ = s.Close()
}
s.sess = s.sess[:0]
}
// Get returns new connection from session.
func (s *Session) Get() (conn net.Conn, err error) {
s.Lock()
defer s.Unlock()
s.offset++
s.offset %= conf.MaxRPCPoolPhysicalConnection
var (
sess *mux.Session
stream *mux.Stream
sessions []*mux.Session
)
for {
if len(s.sess) <= s.offset {
// open new session
sess, err = s.newSession()
if err != nil {
return
}
s.sess = append(s.sess, sess)
s.offset = len(s.sess) - 1
} else {
sess = s.sess[s.offset]
}
// open connection
stream, err = sess.OpenStream()
if err != nil {
// invalidate session
sessions = nil
sessions = append(sessions, s.sess[0:s.offset]...)
sessions = append(sessions, s.sess[s.offset+1:]...)
s.sess = sessions
continue
}
conn = stream
return
}
}
// Len returns physical connection count.
func (s *Session) Len() int {
s.RLock()
defer s.RUnlock()
return len(s.sess)
}
func (s *Session) newSession() (sess *mux.Session, err error) {
var conn net.Conn
conn, err = s.nodeDialer(s.target)
if err != nil {
err = errors.Wrap(err, "dialing new session connection failed")
return
}
return mux.Client(conn, MuxConfig)
}
// newSessionPool creates a new SessionPool.
func newSessionPool(nd NodeDialer) *SessionPool {
return &SessionPool{
sessions: make(SessionMap),
nodeDialer: nd,
}
}
// GetSessionPoolInstance return default SessionPool instance with rpc.DefaultDialer.
func GetSessionPoolInstance() *SessionPool {
once.Do(func() {
instance = newSessionPool(DefaultDialer)
})
return instance
}
func (p *SessionPool) getSession(id proto.NodeID) (sess *Session, loaded bool) {
// NO Blocking operation in this function
p.Lock()
defer p.Unlock()
sess, exist := p.sessions[id]
if exist {
//log.WithField("node", id).Debug("load session for target node")
loaded = true
} else {
// new session
sess = &Session{
nodeDialer: p.nodeDialer,
target: id,
}
p.sessions[id] = sess
}
return
}
// Get returns existing session to the node, if not exist try best to create one.
func (p *SessionPool) Get(id proto.NodeID) (conn net.Conn, err error) {
var sess *Session
sess, _ = p.getSession(id)
return sess.Get()
}
// Remove the node sessions in the pool.
func (p *SessionPool) Remove(id proto.NodeID) {
p.Lock()
defer p.Unlock()
sess, exist := p.sessions[id]
if exist {
sess.Close()
delete(p.sessions, id)
}
return
}
// Close closes all sessions in the pool.
func (p *SessionPool) Close() {
p.Lock()
defer p.Unlock()
for _, s := range p.sessions {
s.Close()
}
p.sessions = make(SessionMap)
}
// Len returns the session counts in the pool.
func (p *SessionPool) Len() (total int) {
p.RLock()
defer p.RUnlock()
for _, s := range p.sessions {
total += s.Len()
}
return
}