-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathConnectionPool.py
313 lines (272 loc) · 11 KB
/
ConnectionPool.py
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
# --coding:utf-8--
#
# Copyright (c) 2021 vesoft inc. All rights reserved.
#
# This source code is licensed under Apache 2.0 License.
import contextlib
import socket
from collections import deque
from threading import RLock, Timer
from nebula3.Exception import NotValidConnectionException, InValidHostname
from nebula3.gclient.net.Session import Session
from nebula3.gclient.net.Connection import Connection
from nebula3.logger import logger
class ConnectionPool(object):
S_OK = 0
S_BAD = 1
def __init__(self):
# all addresses of servers
self._addresses = list()
# server's status
self._addresses_status = dict()
# all connections
self._connections = dict()
self._configs = None
self._ssl_configs = None
self._lock = RLock()
self._pos = -1
self._close = False
def __del__(self):
self.close()
def init(self, addresses, configs, ssl_conf=None):
"""init the connection pool
:param addresses: the graphd servers' addresses
:param configs: the config of the pool
:param ssl_conf: the config of SSL socket
:return: if all addresses are ok, return True else return False.
"""
if self._close:
logger.error('The pool has init or closed.')
raise RuntimeError('The pool has init or closed.')
self._configs = configs
self._ssl_configs = ssl_conf
for address in addresses:
if address not in self._addresses:
try:
ip = socket.gethostbyname(address[0])
except Exception:
raise InValidHostname(str(address[0]))
ip_port = (ip, address[1])
self._addresses.append(ip_port)
self._addresses_status[ip_port] = self.S_BAD
self._connections[ip_port] = deque()
self.update_servers_status()
# detect the services
self._period_detect()
# init min connections
ok_num = self.get_ok_servers_num()
if ok_num < len(self._addresses):
raise RuntimeError(
'The services status exception: {}'.format(self._get_services_status())
)
conns_per_address = int(self._configs.min_connection_pool_size / ok_num)
if self._ssl_configs is None:
for addr in self._addresses:
for i in range(0, conns_per_address):
connection = Connection()
connection.open(addr[0], addr[1], self._configs.timeout)
self._connections[addr].append(connection)
else:
for addr in self._addresses:
for i in range(0, conns_per_address):
connection = Connection()
connection.open_SSL(
addr[0], addr[1], self._configs.timeout, self._ssl_configs
)
self._connections[addr].append(connection)
return True
def get_session(self, user_name, password, retry_connect=True):
"""get session
:param user_name: the user name to authenticate graphd
:param password: the password to authenticate graphd
:param retry_connect:
:return: Session
"""
connection = self.get_connection()
if connection is None:
raise NotValidConnectionException()
try:
auth_result = connection.authenticate(user_name, password)
return Session(connection, auth_result, self, retry_connect)
except Exception:
raise
@contextlib.contextmanager
def session_context(self, *args, **kwargs):
"""
session_context is to be used with a contextlib.contextmanager.
It returns a connection session from the pool, with same params
as the method get_session().
When session_context is exited, the connection will be released.
:param user_name: the user name to authenticate graphd
:param password: the password to authenticate graphd
:param retry_connect: if auto retry connect
:return: contextlib._GeneratorContextManager
"""
session = None
try:
session = self.get_session(*args, **kwargs)
yield session
except Exception:
raise
finally:
if session:
session.release()
def get_connection(self):
"""get available connection
:return: Connection
"""
with self._lock:
if self._close:
logger.error('The pool is closed')
raise NotValidConnectionException()
try:
ok_num = self.get_ok_servers_num()
if ok_num == 0:
return None
max_con_per_address = int(
self._configs.max_connection_pool_size / ok_num
)
try_count = 0
while try_count <= len(self._addresses):
self._pos = (self._pos + 1) % len(self._addresses)
addr = self._addresses[self._pos]
if self._addresses_status[addr] == self.S_OK:
for connection in self._connections[addr]:
if not connection.is_used:
if connection.ping():
connection.is_used = True
logger.info('Get connection to {}'.format(addr))
return connection
if len(self._connections[addr]) < max_con_per_address:
connection = Connection()
if self._ssl_configs is None:
connection.open(addr[0], addr[1], self._configs.timeout)
else:
connection.open_SSL(
addr[0],
addr[1],
self._configs.timeout,
self._ssl_configs,
)
connection.is_used = True
self._connections[addr].append(connection)
logger.info('Get connection to {}'.format(addr))
return connection
else:
for connection in list(self._connections[addr]):
if not connection.is_used:
self._connections[addr].remove(connection)
try_count = try_count + 1
return None
except Exception as ex:
logger.error('Get connection failed: {}'.format(ex))
return None
def ping(self, address):
"""check the server is ok
:param address: the server address want to connect
:return: True or False
"""
try:
conn = Connection()
if self._ssl_configs is None:
conn.open(address[0], address[1], 1000)
else:
conn.open_SSL(address[0], address[1], 1000, self._ssl_configs)
conn.close()
return True
except Exception as ex:
logger.warning(
'Connect {}:{} failed: {}'.format(address[0], address[1], ex)
)
return False
def close(self):
"""close all connections in pool
:return: void
"""
with self._lock:
for addr in self._connections.keys():
for connection in self._connections[addr]:
if connection.is_used:
logger.error(
'The connection using by someone, but now want to close it'
)
connection.close()
self._close = True
def connects(self):
"""get the number of existing connections
:return: the number of connections
"""
with self._lock:
count = 0
for addr in self._connections.keys():
count = count + len(self._connections[addr])
return count
def in_used_connects(self):
"""get the number of the used connections
:return: int
"""
with self._lock:
count = 0
for addr in self._connections.keys():
for connection in self._connections[addr]:
if connection.is_used:
count = count + 1
return count
def get_ok_servers_num(self):
"""get the number of the ok servers
:return: int
"""
count = 0
for addr in self._addresses_status.keys():
if self._addresses_status[addr] == self.S_OK:
count = count + 1
return count
def _get_services_status(self):
msg_list = []
for addr in self._addresses_status.keys():
status = 'OK'
if self._addresses_status[addr] != self.S_OK:
status = 'BAD'
msg_list.append('[services: {}, status: {}]'.format(addr, status))
return ', '.join(msg_list)
def update_servers_status(self):
"""update the servers' status"""
for address in self._addresses:
if self.ping(address):
self._addresses_status[address] = self.S_OK
else:
self._addresses_status[address] = self.S_BAD
def _remove_idle_unusable_connection(self):
if self._configs.idle_time == 0:
return
with self._lock:
for addr in self._connections.keys():
conns = self._connections[addr]
for connection in list(conns):
if not connection.is_used:
if not connection.ping():
logger.debug(
'Remove the not unusable connection to {}'.format(
connection.get_address()
)
)
conns.remove(connection)
continue
if (
self._configs.idle_time != 0
and connection.idle_time() > self._configs.idle_time
):
logger.debug(
'Remove the idle connection to {}'.format(
connection.get_address()
)
)
conns.remove(connection)
def _period_detect(self):
if self._close or self._configs.interval_check < 0:
return
self.update_servers_status()
self._remove_idle_unusable_connection()
timer = Timer(self._configs.interval_check, self._period_detect)
timer.setDaemon(True)
timer.start()