Skip to content

Commit 7f405ee

Browse files
add onSubsessionStart/Finish
1 parent 938b74e commit 7f405ee

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ Sessions accept the following options:
129129
A function accepting a baton, function, array of arguments, and a
130130
[`PromiseInspection`][bluebird-inspection] representing the state of the
131131
atomic transaction.
132+
* `onSubsessionStart(parentSession, childSession)`: Useful for copying
133+
information down from parent sessions to child sessions.
134+
* `onSubsessionFinish(parentSession, childSession)`: Useful for cleaning up
135+
information from child sessions.
132136

133137
All functions will default to `noop` if not provided.
134138

db-session.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ const api = module.exports = {
2121
install (domain, getConnection, opts) {
2222
opts = Object.assign({
2323
maxConcurrency: Infinity,
24+
onSubsessionStart: noop,
25+
onSubsessionFinish: noop,
2426
onSessionIdle: noop,
2527
onConnectionRequest: noop,
2628
onConnectionStart: noop,
@@ -86,6 +88,8 @@ class Session {
8688
this.activeConnections = 0
8789
this.maxConcurrency = opts.maxConcurrency || Infinity
8890
this.metrics = {
91+
onSubsessionStart: opts.onSubsessionStart,
92+
onSubsessionFinish: opts.onSubsessionFinish,
8993
onSessionIdle: opts.onSessionIdle,
9094
onConnectionRequest: opts.onConnectionRequest,
9195
onConnectionStart: opts.onConnectionStart,
@@ -239,6 +243,7 @@ function Session$RunWrapped (parent,
239243
return getConnPair.then(pair => {
240244
const subdomain = domain.create()
241245
const session = createSession(pair)
246+
parent.metrics.onSubsessionStart(parent, session)
242247
DOMAIN_TO_SESSION.set(subdomain, session)
243248

244249
const runBefore = new Promise((resolve, reject) => {
@@ -273,7 +278,13 @@ function Session$RunWrapped (parent,
273278
err => err ? reject(err) : resolve()
274279
)
275280
})
276-
})
281+
}).then(
282+
() => parent.metrics.onSubsessionFinish(parent, session),
283+
err => {
284+
parent.metrics.onSubsessionFinish(parent, session)
285+
throw err
286+
}
287+
)
277288
return runCommitStep.return(getResult)
278289
})
279290
})

0 commit comments

Comments
 (0)