Skip to content

perf(spanner): synchronous multiplexed session hand-off for non-stream callers - #9362

Merged
olavloite merged 1 commit into
mainfrom
spanner-sync-mux-handoff
Sep 18, 2026
Merged

olavloite merged 1 commit into
mainfrom
spanner-sync-mux-handoff

Conversation

@olavloite

Copy link
Copy Markdown
Contributor

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.

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`.
@olavloite
olavloite requested a review from a team as a code owner September 17, 2026 07:56

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread handwritten/spanner/src/session-factory.ts
@olavloite

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Comment thread handwritten/spanner/src/database.ts
@olavloite olavloite changed the title perf: synchronous multiplexed session hand-off for non-stream callers perf(spanner): synchronous multiplexed session hand-off for non-stream callers Sep 17, 2026
@olavloite

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread handwritten/spanner/src/session-factory.ts
Comment thread handwritten/spanner/src/database.ts
@olavloite
olavloite merged commit dbbc55c into main Sep 18, 2026
50 checks passed
@olavloite
olavloite deleted the spanner-sync-mux-handoff branch September 18, 2026 07:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants