19
19
20
20
import mariadb
21
21
import _thread
22
- import time
23
22
24
23
MAX_POOL_SIZE = 64
25
24
POOL_IDLE_TIMEOUT = 1800
@@ -63,7 +62,8 @@ def __init__(self, *args, **kwargs):
63
62
Will reset the connection before returning it to the pool.
64
63
Default value is True.
65
64
"""
66
- self ._connections = []
65
+ self ._connections_free = []
66
+ self ._connections_used = []
67
67
self ._pool_args = {}
68
68
self ._conn_args = {}
69
69
self ._lock_pool = _thread .RLock ()
@@ -107,15 +107,15 @@ def __init__(self, *args, **kwargs):
107
107
except mariadb .Error :
108
108
# if an error occurred, close all connections
109
109
# and raise exception
110
- for j in range (0 , len (self ._connections )):
110
+ for j in range (0 , len (self ._connections_free )):
111
111
try :
112
- self ._connections [j ].close ()
112
+ self ._connections_free [j ].close ()
113
113
except mariadb .Error :
114
114
# connect failed, so we are not
115
115
# interested in errors
116
116
# from close() method
117
117
pass
118
- del self ._connections [j ]
118
+ del self ._connections_free [j ]
119
119
raise
120
120
self .add_connection (connection )
121
121
@@ -151,46 +151,41 @@ def add_connection(self, connection=None):
151
151
raise mariadb .PoolError ("Can't get configuration for pool %s" %
152
152
self ._pool_args ["name" ])
153
153
154
- if len (self ._connections ) >= self ._pool_args ["size" ]:
154
+ total = len (self ._connections_free ) + len (self ._connections_used )
155
+ if total >= self ._pool_args ["size" ]:
155
156
raise mariadb .PoolError ("Can't add connection to pool %s: "
156
157
"No free slot available (%s)." %
157
158
(self ._pool_args ["name" ],
158
- len ( self . _connections ) ))
159
+ total ))
159
160
160
161
with self ._lock_pool :
161
162
if connection is None :
162
163
connection = mariadb .Connection (** self ._conn_args )
163
164
164
165
connection ._Connection__pool = self
165
- connection ._Connection__in_use = 0
166
- connection ._Connection__last_used = time .monotonic ()
167
- self ._connections .append (connection )
166
+ self ._connections_free .append (connection )
168
167
169
168
def get_connection (self ):
170
169
"""
171
170
Returns a connection from the connection pool or raises a PoolError
172
171
exception if a connection is not available.
173
172
"""
174
173
175
- now = time .monotonic ()
176
174
conn = None
177
- timediff = - 1
178
175
179
176
with self ._lock_pool :
180
- for i in range (0 , len (self ._connections )):
181
- if not self ._connections [i ]._Connection__in_use :
182
- try :
183
- self ._connections [i ].ping ()
184
- except mariadb .Error :
185
- continue
186
- t = now - self ._connections [i ]._Connection__last_used
187
- if t > timediff :
188
- conn = self ._connections [i ]
189
- timediff = t
190
-
191
- if conn :
192
- conn ._Connection__in_use = 1
193
- return conn
177
+ for i in range (0 , len (self ._connections_free )):
178
+ try :
179
+ self ._connections_free [i ].ping ()
180
+ except mariadb .Error :
181
+ continue
182
+ conn = self ._connections_free [i ]
183
+ conn ._used += 1
184
+ self ._connections_used .append (conn )
185
+ del self ._connections_free [i ]
186
+ return conn
187
+
188
+ return None
194
189
195
190
def _close_connection (self , connection ):
196
191
"""
@@ -201,8 +196,12 @@ def _close_connection(self, connection):
201
196
with self ._lock_pool :
202
197
if self ._pool_args ["reset_connection" ]:
203
198
connection .reset ()
204
- connection ._Connection__in_use = 0
205
- connection ._Connection__last_used = time .monotonic ()
199
+
200
+ for i in range (0 , len (self ._connections_used )):
201
+ if self ._connections_used [i ] == connection :
202
+ del self ._connections_used [i ]
203
+ self ._connections_free .append (connection )
204
+ return
206
205
207
206
def set_config (self , ** kwargs ):
208
207
"""
@@ -218,11 +217,15 @@ def set_config(self, **kwargs):
218
217
def close (self ):
219
218
"""Closes connection pool and all connections."""
220
219
try :
221
- for c in self ._connections :
220
+ for c in self ._connections_free :
221
+ c ._Connection__pool = None
222
+ c .close ()
223
+ for c in self ._connections_used :
222
224
c ._Connection__pool = None
223
225
c .close ()
224
226
finally :
225
- self ._connections = None
227
+ self ._connections_free = None
228
+ self ._connections_used = None
226
229
del mariadb ._CONNECTION_POOLS [self ._pool_args ["name" ]]
227
230
228
231
@property
0 commit comments