-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy path_session_pool.py
More file actions
289 lines (259 loc) · 10.3 KB
/
_session_pool.py
File metadata and controls
289 lines (259 loc) · 10.3 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
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
# Copyright 2025 vesoft-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.
import logging
import threading
from contextlib import asynccontextmanager, contextmanager
from dataclasses import dataclass, field
from typing import Any, Dict, Optional, Set
from anyio import Lock, Semaphore, fail_after
from nebulagraph_python.client._connection import AsyncConnection, Connection
from nebulagraph_python.client._session import (
AsyncSession,
Session,
SessionConfig,
)
from nebulagraph_python.client.constants import (
DEFAULT_SESSION_POOL_SIZE,
DEFAULT_SESSION_POOL_WAIT_TIMEOUT,
)
from nebulagraph_python.error import PoolError
logger = logging.getLogger(__name__)
@dataclass
class SessionPoolConfig:
"""Configuration for the SessionPool.
Args:
size: The number of sessions to be managed by the SessionPool.
wait_timeout: The maximum time to wait for a session to be available. If None, wait indefinitely.
"""
size: int = field(default=DEFAULT_SESSION_POOL_SIZE)
wait_timeout: float | None = field(default=DEFAULT_SESSION_POOL_WAIT_TIMEOUT)
def __post_init__(self):
if self.size <= 0:
raise ValueError(
f"SessionPoolConfig.size must be greater than 0, but got {self.size}"
)
if self.wait_timeout is not None and self.wait_timeout <= 0:
self.wait_timeout = None
class AsyncSessionPool:
"""Manage a pool of sessions. It is built upon anyio Lock and is async/coroutine-level safe but not thread-safe."""
free_sessions_queue: Set[AsyncSession]
busy_sessions_queue: Set[AsyncSession]
queue_lock: Lock
queue_count: Semaphore
config: SessionPoolConfig
@classmethod
async def connect(
cls,
conn: AsyncConnection,
username: str,
password: Optional[str] = None,
auth_options: Optional[Dict[str, Any]] = None,
session_config: Optional[SessionConfig] = None,
pool_config: Optional[SessionPoolConfig] = None,
):
pool_config = pool_config or SessionPoolConfig()
sessions: Set[AsyncSession] = set()
try:
for _ in range(pool_config.size):
sessions.add(
AsyncSession(
conn,
username=username,
password=password,
session_config=session_config,
auth_options=auth_options,
)
)
return cls(sessions, pool_config)
except Exception:
# Clean up any sessions that were successfully created
for session in sessions:
await session._close()
raise
def __init__(
self,
sessions: Set[AsyncSession],
config: SessionPoolConfig,
):
"""Initialize the SessionPool
Args:
sessions: The sessions to be managed by the SessionPool.
config: Configuration for the SessionPool.
"""
if len(sessions) != config.size:
raise ValueError(
f"The number of sessions ({len(sessions)}) does not match the size of the pool ({config.size})"
)
self.free_sessions_queue = sessions
self.busy_sessions_queue = set()
self.queue_lock = Lock()
self.queue_count = Semaphore(len(sessions))
self.config = config
@asynccontextmanager
async def borrow(self):
got_session: Optional[AsyncSession] = None
# Event-based loop (wait for free session to be available)
while True:
if self.config.wait_timeout is not None:
try:
with fail_after(self.config.wait_timeout):
await self.queue_count.acquire()
except TimeoutError:
break
else:
await self.queue_count.acquire()
async with self.queue_lock:
if not self.free_sessions_queue:
logger.error(
"No free sessions available after acquired semaphore, which indicates a bug in the AsyncSessionPool"
)
# Release semaphore and retry if no sessions available
self.queue_count.release()
continue
session = self.free_sessions_queue.pop()
self.busy_sessions_queue.add(session)
got_session = session
break
if got_session is None:
raise PoolError(
f"No session available in the SessionPool after waiting {self.config.wait_timeout} seconds"
)
try:
yield got_session
finally:
# Ensure session is returned to pool even if exception occurs
async with self.queue_lock:
if got_session in self.busy_sessions_queue:
self.free_sessions_queue.add(got_session)
self.busy_sessions_queue.remove(got_session)
self.queue_count.release()
async def _close(self):
# Acquire all semaphore permits to prevent new borrows
for _ in range(self.config.size):
await self.queue_count.acquire()
async with self.queue_lock:
# Close all free sessions
for session in self.free_sessions_queue:
await session._close()
# Close all busy sessions (if any remain)
for session in self.busy_sessions_queue:
logger.error(
"Busy sessions remain after acquire all semaphore permits, which indicates a bug in the AsyncSessionPool"
)
await session._close()
class SessionPool:
"""Manage a pool of sessions. It is built upon threading.Lock and is thread-safe."""
free_sessions_queue: Set[Session]
busy_sessions_queue: Set[Session]
queue_lock: threading.Lock
queue_count: threading.Semaphore
config: SessionPoolConfig
@classmethod
def connect(
cls,
conn: Connection,
username: str,
password: Optional[str] = None,
auth_options: Optional[Dict[str, Any]] = None,
session_config: Optional[SessionConfig] = None,
pool_config: Optional[SessionPoolConfig] = None,
):
pool_config = pool_config or SessionPoolConfig()
sessions: Set[Session] = set()
try:
for _ in range(pool_config.size):
sessions.add(
Session(
conn,
username=username,
password=password,
session_config=session_config,
auth_options=auth_options,
)
)
return cls(sessions, pool_config)
except Exception:
# Clean up any sessions that were successfully created
for session in sessions:
session._close()
raise
def __init__(
self,
sessions: Set[Session],
config: SessionPoolConfig,
):
"""Initialize the SessionPool
Args:
sessions: The sessions to be managed by the SessionPool.
config: Configuration for the SessionPool.
"""
if len(sessions) != config.size:
raise ValueError(
f"The number of sessions ({len(sessions)}) does not match the size of the pool ({config.size})"
)
self.free_sessions_queue = sessions
self.busy_sessions_queue = set()
self.queue_lock = threading.Lock()
self.queue_count = threading.Semaphore(len(sessions))
self.config = config
@contextmanager
def borrow(self):
got_session: Optional[Session] = None
# Event-based loop (wait for free session to be available)
while True:
if self.config.wait_timeout is not None:
acquired = self.queue_count.acquire(timeout=self.config.wait_timeout)
if not acquired:
break
else:
self.queue_count.acquire()
with self.queue_lock:
if not self.free_sessions_queue:
logger.error(
"No free sessions available after acquired semaphore, which indicates a bug in the SessionPool"
)
# Release semaphore and retry if no sessions available
self.queue_count.release()
continue
session = self.free_sessions_queue.pop()
self.busy_sessions_queue.add(session)
got_session = session
break
if got_session is None:
raise PoolError(
f"No session available in the SessionPool after waiting {self.config.wait_timeout} seconds"
)
try:
yield got_session
finally:
# Ensure session is returned to pool even if exception occurs
with self.queue_lock:
if got_session in self.busy_sessions_queue:
self.free_sessions_queue.add(got_session)
self.busy_sessions_queue.remove(got_session)
self.queue_count.release()
def _close(self):
# Acquire all semaphore permits to prevent new borrows
for _ in range(self.config.size):
self.queue_count.acquire()
with self.queue_lock:
# Close all free sessions
for session in self.free_sessions_queue:
session._close()
# Close all busy sessions (if any remain)
for session in self.busy_sessions_queue:
logger.error(
"Busy sessions remain after acquire all semaphore permits, which indicates a bug in the SessionPool"
)
session._close()