-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support for common interface to get a session
- Loading branch information
1 parent
92248c1
commit 0f73d4c
Showing
6 changed files
with
296 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import {Database, Session, Transaction} from '.'; | ||
import { | ||
MultiplexedSession, | ||
MultiplexedSessionInterface, | ||
} from './multiplexed-session'; | ||
import { | ||
SessionPool, | ||
SessionPoolInterface, | ||
SessionPoolOptions, | ||
} from './session-pool'; | ||
import {SessionPoolConstructor} from './database'; | ||
import {ServiceObjectConfig} from '@google-cloud/common'; | ||
const common = require('./common-grpc/service-object'); | ||
|
||
/** | ||
* @callback GetSessionCallback | ||
* @param {?Error} error Request error, if any. | ||
* @param {Session} session The read-write session. | ||
* @param {Transaction} transaction The transaction object. | ||
*/ | ||
export interface GetSessionCallback { | ||
( | ||
err: Error | null, | ||
session?: Session | null, | ||
transaction?: Transaction | null | ||
): void; | ||
} | ||
|
||
export interface SessionFactoryInterface { | ||
getSession(callback: GetSessionCallback): void; | ||
getPool(): SessionPoolInterface; | ||
getMultiplexedSession(): MultiplexedSessionInterface | undefined; | ||
release(session: Session): void; | ||
} | ||
|
||
export class SessionFactory | ||
extends common.GrpcServiceObject | ||
implements SessionFactoryInterface | ||
{ | ||
multiplexedSession_?: MultiplexedSessionInterface; | ||
pool_: SessionPoolInterface; | ||
constructor( | ||
database: Database, | ||
name: String, | ||
poolOptions?: SessionPoolConstructor | SessionPoolOptions | ||
) { | ||
super({ | ||
parent: database, | ||
id: name, | ||
} as {} as ServiceObjectConfig); | ||
this.pool_ = | ||
typeof poolOptions === 'function' | ||
? new (poolOptions as SessionPoolConstructor)(database, null) | ||
: new SessionPool(database, poolOptions); | ||
this.multiplexedSession_ = new MultiplexedSession(database); | ||
this.pool_.on('error', this.emit.bind(this, 'error')); | ||
this.pool_.open(); | ||
// multiplexed session should only get created if the env varaible is enabled | ||
if (process.env.GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS === 'true') { | ||
this.multiplexedSession_.on('error', this.emit.bind(this, 'error')); | ||
this.multiplexedSession_.createSession(); | ||
} | ||
} | ||
|
||
getSession(callback: GetSessionCallback): void { | ||
if (process.env.GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS === 'true') { | ||
this.multiplexedSession_?.getSession((err, session) => { | ||
err ? callback(err, null) : callback(null, session); | ||
}); | ||
} else { | ||
this.pool_?.getSession((err, session) => { | ||
err ? callback(err, null) : callback(null, session); | ||
}); | ||
} | ||
} | ||
|
||
getPool(): SessionPoolInterface { | ||
return this.pool_; | ||
} | ||
|
||
getMultiplexedSession(): MultiplexedSessionInterface | undefined { | ||
return this.multiplexedSession_; | ||
} | ||
|
||
release(session: Session): void { | ||
this.pool_.release(session); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.