@@ -48,31 +48,33 @@ async def acquire(self, timeout: float) -> QuerySessionAsync:
48
48
logger .error ("An attempt to take session from closed session pool." )
49
49
raise RuntimeError ("An attempt to take session from closed session pool." )
50
50
51
+ session = None
51
52
try :
52
53
_ , session = self ._queue .get_nowait ()
54
+ except asyncio .QueueEmpty :
55
+ pass
56
+
57
+ if session is None and self ._current_size == self ._size :
58
+ try :
59
+ self ._waiters += 1
60
+ session = await self ._get_session_with_timeout (timeout )
61
+ except asyncio .TimeoutError :
62
+ raise issues .SessionPoolEmpty ("Timeout on acquire session" )
63
+ finally :
64
+ self ._waiters -= 1
65
+
66
+ if session is not None :
53
67
if session ._state .attached :
54
68
logger .debug (f"Acquired active session from queue: { session ._state .session_id } " )
55
69
return session
56
70
else :
57
71
self ._current_size -= 1
58
72
logger .debug (f"Acquired dead session from queue: { session ._state .session_id } " )
59
- except asyncio .QueueEmpty :
60
- pass
61
-
62
- if self ._current_size < self ._size :
63
- logger .debug (f"Session pool is not large enough: { self ._current_size } < { self ._size } , will create new one." )
64
- session = await self ._create_new_session ()
65
- self ._current_size += 1
66
- return session
67
73
68
- try :
69
- self ._waiters += 1
70
- session = await self ._get_session_with_timeout (timeout )
71
- return session if session ._state .attached else await self ._create_new_session ()
72
- except asyncio .TimeoutError :
73
- raise issues .SessionPoolEmpty ("Timeout on acquire session" )
74
- finally :
75
- self ._waiters -= 1
74
+ logger .debug (f"Session pool is not large enough: { self ._current_size } < { self ._size } , will create new one." )
75
+ session = await self ._create_new_session ()
76
+ self ._current_size += 1
77
+ return session
76
78
77
79
async def _get_session_with_timeout (self , timeout : float ):
78
80
task_wait = asyncio .ensure_future (asyncio .wait_for (self ._queue .get (), timeout = timeout ))
0 commit comments