perf(spanner): synchronous multiplexed session hand-off for non-stream callers - #9362
Conversation
Adds `getSessionSync()` to `MultiplexedSession` and `SessionFactory` to return the cached multiplexed session directly when available, avoiding an extra `process.nextTick` tick on every query. Non-stream callers (`database.run` and `Database.prototype.makePooledRequest_`) now use `getSessionSync()` to start query execution in the same tick on cache hit, falling back to asynchronous `getSession()` on cold start or when multiplexed sessions are disabled. Stream-returning callers (`runStream`, `batchWriteAtLeastOnce`, `makePooledStreamingRequest_`) keep asynchronous session acquisition so callers can attach event listeners before stream events are emitted. Also removes redundant callback wrapping in `SessionFactory.prototype.getSession` and moves `isAFEServerTimingEnabled` to `src/common.ts` to break a module load cycle between `common.ts` and `index.ts`.
There was a problem hiding this comment.
Code Review
This pull request introduces synchronous multiplexed session hand-off to optimize session retrieval by adding a synchronous getSessionSync method to the multiplexed session and session factory interfaces. It refactors the database request and query execution paths to utilize this fast-path when available, while ensuring asynchronous callback execution (preventing Zalgo) via process.nextTick when handling synchronous errors. Additionally, environment variable checks for tracing and AFE server timing are updated to be case-insensitive, accompanied by comprehensive unit tests. Feedback on the changes highlights a missing isMultiplexedEnabledForRW method declaration in SessionFactoryInterface which could lead to TypeScript compilation errors.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces synchronous multiplexed session retrieval (getSessionSync) across the session factory, multiplexed session, and database classes to optimize session hand-off. It also updates environment variable checks in getCommonHeaders to be case-insensitive. The feedback recommends wrapping the synchronous session hand-off in makePooledRequest_ with a try-catch block to defer any synchronous errors using process.nextTick, ensuring consistent asynchronous behavior and preventing synchronous error propagation (Zalgo).
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces synchronous multiplexed session retrieval (getSessionSync) across Database, SessionFactory, and MultiplexedSession to bypass asynchronous overhead when a cached session is already available. It also updates environment variable checks in common.ts to be case-insensitive and adds comprehensive unit tests. The review feedback highlights a missing optional method isMultiplexedEnabledForRW in SessionFactoryInterface that could cause TypeScript compilation errors, and recommends wrapping the synchronous session hand-off in makePooledRequest_ with a try-catch block to defer synchronous errors via process.nextTick and preserve the asynchronous callback contract.
Adds
getSessionSync()toMultiplexedSessionandSessionFactoryto return the cached multiplexed session directly when available, avoiding an extraprocess.nextTicktick on every query.Non-stream callers (
database.runandDatabase.prototype.makePooledRequest_) now usegetSessionSync()to start query execution in the same tick on cache hit, falling back to asynchronousgetSession()on cold start or when multiplexed sessions are disabled. Stream-returning callers (runStream,batchWriteAtLeastOnce,makePooledStreamingRequest_) keep asynchronous session acquisition so callers can attach event listeners before stream events are emitted.Also removes redundant callback wrapping in
SessionFactory.prototype.getSessionand movesisAFEServerTimingEnabledtosrc/common.tsto break a module load cycle betweencommon.tsandindex.ts.