Conversation
- Implement Session class with IDLE/BUSY/CLOSED states and attachSession keepalive
- Implement SessionPool with acquire/release pattern (default maxSize: 50)
- Integrate SessionPool with Query class and transaction execution
- Add 13 new tests (7 unit + 6 integration)
Sessions are now automatically pooled and reused between queries and transactions,
eliminating the overhead of creating a new session for every operation.
Public API: query(driver, { maxSize?: number })
d257df8 to
68db0d5
Compare
packages/query/src/session.ts
Outdated
| driver: Driver, | ||
| sessionId: string, | ||
| nodeId: bigint, | ||
| attachIterator?: AsyncIterator<SessionStateProto> |
There was a problem hiding this comment.
Итератор лучше создавать внутри класса, плюс аттач нужно делать только тогда, когда сессия в первый раз запросили.
Далее, закрытие стрима лучше делать через signal от AbortController();
There was a problem hiding this comment.
Кажется, поправил
Еще добавил читателя для attach stream и состояние invalidated для обработки ошибок
packages/query/tests/query.test.ts
Outdated
|
|
||
| test('executes simple query', async () => { | ||
| let sql = query(driver) | ||
| sql = query(driver) |
- Add INVALIDATED state and onInvalidated callback - Add attach stream monitoring in background - Lazy attach initialization in acquire() - Support AbortSignal for cancellable attach
packages/query/src/index.ts
Outdated
| export function query(driver: Driver): QueryClient { | ||
| export function query( | ||
| driver: Driver, | ||
| poolOptions?: SessionPoolOptions |
There was a problem hiding this comment.
Лучше сделать отдельный QueryOptions и туда передать poolOptions
packages/query/src/index.ts
Outdated
| ) | ||
| } | ||
| dbg.log('acquiring session from pool for transaction') | ||
| let session = await sessionPool.acquire(signal) |
There was a problem hiding this comment.
по возможности, я бы хотел видеть using session = await sessionPool.acquire(signal) чтобы не потерять где-то ниже release. в го есть удобный defer, тут есть такой механизм.
There was a problem hiding this comment.
вроде как поправил, теперь acquire возвращает обертку SessionLease
packages/query/src/session-pool.ts
Outdated
| * Wait for a session to become available | ||
| */ | ||
| #waitForSession(signal?: AbortSignal): Promise<Session> { | ||
| return new Promise<Session>((resolve, reject) => { |
packages/query/src/session.ts
Outdated
| while (this.#attachIterator) { | ||
| // eslint-disable-next-line no-await-in-loop | ||
| let result = await this.#attachIterator.next() |
There was a problem hiding this comment.
Посмотри на конструкцию for await при работе с асинхронным итераторами
- Add SessionLease class with Symbol.dispose - Use 'using' pattern in query execution and transactions - Use for await...of and Promise.withResolvers() - Add QueryOptions type and update documentation
What
Add session pool for query service to reuse sessions between queries and transactions.
Why
Every query currently creates a new session, which is inefficient and increases load on YDB server. Session pooling eliminates this overhead.
Changes
Testing
Checklist